下面是注册页面后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserControls_Register : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOK_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
protected void btnOK1_Click(object sender, EventArgs e)
{
SqlData da = new SqlData();
SqlDataReader read = da.ExceRead("select * from tb_UserLW where Uid='" + this.txtUid.Text + "'");
read.Read();
if (read.HasRows)
{
if (this.txtUid.Text == read["Uid"].ToString())
{
Response.Write("<script language=javascript>alert('对不起,该用户已经注册!');location='Register.aspx'</script>");
return;
}
}
read.Close();
string cmdtxt = "INSERT INTO tb_UserLW(Uid,Pwd,Sex,WebSite,Email,QQ,IP)";
cmdtxt += " VALUES('" + this.txtUid.Text + "','" + this.txtPwd.Text + "','" + this.radSex.SelectedValue+ "'";
cmdtxt += ",'" + this.txtWebSite.Text + "','" + this.txtEmail.Text + "','" + this.txtQQ.Text + "'";
cmdtxt += ",'" + Request.UserHostAddress + "')";
bool add = da.ExceSQL(cmdtxt);
if (add == true)
{
Response.Write("<script language=javascript>alert('注册成功!');location='Register.aspx'</script>");
}
else
{
Response.Write("<script language=javascript>alert('注册失败!');location='javascript:history.go(-1)'</script>");
}
}
protected void btnNoOK_Click(object sender, EventArgs e)
{
Response.Redirect("Index.aspx");
}
}
下面是公共类的代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// SqlData 的摘要说明
/// </summary>
public class SqlData
{
private SqlConnection sqlcon;
private SqlCommand sqlcom;
private SqlDataAdapter sqldata;
public SqlData()
{
sqlcon = new SqlConnection(ConfigurationManager.AppSettings["strcon"]);
sqlcon.Open();
}
public bool ExceSQL(string SqlCom)
{
sqlcom = new SqlCommand(SqlCom, sqlcon);
try
{
sqlcom.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
sqlcon.Close();
}
}
public DataSet ExceDS(string SqlCom)
{
try
{
sqlcom = new SqlCommand(SqlCom, sqlcon);
sqldata = new SqlDataAdapter();
sqldata.SelectCommand = sqlcom;
DataSet ds = new DataSet();
sqldata.Fill(ds);
return ds;
}
finally
{
sqlcon.Close();
}
}
public SqlDataReader ExceRead(string SqlCom)
{
sqlcom = new SqlCommand(SqlCom, sqlcon);
SqlDataReader read = sqlcom.ExecuteReader();
return read;
}
}
当我填好注册信息后提交 并单步运行时出现异常!
运行到“红色”那一行后马上运行到 catch
{
return false;
}
最后提示“注册失败”对话框