注册界面出现问题
本人是菜鸟,各位大侠谁能帮忙解决一下此问题(由于没有积分了,也没积分给了)出现问题地方:int i=Convert.ToInt32(com.ExecuteNonQuery());[在此处出现异常](源码中红色部分)
.aspx源码:
<html xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 688px; height: 15px">
<tr>
<td style="height: 2px" align="right">
<asp:Label ID="Label1" runat="server" Text="会员登录名:"></asp:Label></td>
<td style="width: 341px; height: 2px">
<asp:TextBox ID="TextName" runat="server"></asp:TextBox>
<asp:Button ID="btmIsName" runat="server" OnClick="btmIsName_Click" Text="检测会员员" />
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ErrorMessage="必须填写会员名" ControlToValidate="TextName"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label2" runat="server" Text="密 码:"></asp:Label></td>
<td style="width: 341px">
<asp:TextBox ID="TextPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPwd" runat="server" ControlToValidate="TextPassword"
ErrorMessage="密码不能为空"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="height: 14px" align="right">
<asp:Label ID="Label3" runat="server" Text="确认密码:"></asp:Label></td>
<td style="width: 341px; height: 14px">
<asp:TextBox ID="TextQrPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorPass" runat="server" ControlToCompare="TextPassword"
ControlToValidate="TextQrPassword" ErrorMessage="两次密码不一致"></asp:CompareValidator></td>
</tr>
<tr>
<td style="height: 14px" align="right">
<asp:Label ID="Label4" runat="server" Text="性 别:"></asp:Label></td>
<td style="width: 341px; height: 14px">
<asp:RadioButton ID="RadioButtonMan" runat="server" GroupName="groupSex" Text="男" Checked="True" />
<asp:RadioButton ID="RadioButtonWoman" runat="server" GroupName="groupSex" Text="女" /></td>
</tr> <tr>
<td style="height: 14px" align="right">
<asp:Label ID="Label5" runat="server" Text="真实姓名:"></asp:Label></td>
<td style="width: 341px; height: 14px">
<asp:TextBox ID="TextRelName" runat="server"></asp:TextBox></td>
</tr> <tr>
<td style="height: 14px" align="right">
<asp:Label ID="Label6" runat="server" Text="电 话:"></asp:Label></td>
<td style="width: 341px; height: 14px">
<asp:TextBox ID="TextPhone" runat="server"></asp:TextBox></td>
</tr> <tr>
<td style="height: 14px" align="right">
<asp:Label ID="Label7" runat="server" Text="Email:"></asp:Label></td>
<td style="width: 341px; height: 14px">
<asp:TextBox ID="TextEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorTextEmail" runat="server" ErrorMessage="Email不能为空" ControlToValidate="TextEmail"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="TextEmail" ErrorMessage="Email格式不正确" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td>
</tr> <tr>
<td style="height: 14px" align="right">
<asp:Label ID="Label8" runat="server" Text="地 址:"></asp:Label></td>
<td style="width: 341px; height: 14px">
<asp:TextBox ID="TextAddress" runat="server"></asp:TextBox></td>
</tr> <tr>
<td style="height: 14px" align="right">
<asp:Button ID="btnLogin" runat="server" Font-Size="Small" Text="注 册" OnClick="btmLogin_Click" /></td>
<td style="width: 341px; height: 14px">
<asp:Button ID="btnCancel" runat="server" Font-Size="Small" Text="取 消" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
其中类源码:
public class DB
{
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection creatSqlCon()
{
SqlConnection con = new SqlConnection("Data Source=.;database='userDB';uid='sa';pwd='123' ");
return con;
}
}
.aspx.cs源码:
public partial class _Default : System.Web.UI.Page
{
protected int isName()
{
int i;
string name = TextName.Text;
if (name.Trim() != "")
{
SqlConnection con =DB.creatSqlCon();
con.Open();
SqlCommand com = new SqlCommand("select count(*) from User_Table where Name='" + name + "'", con);
return i = Convert.ToInt32(com.ExecuteScalar());
con.Close();
}
else
return i = 1;
}
public bool add()
{
string name=TextName.Text;
string pass=FormsAuthentication.HashPasswordForStoringInConfigFile(TextPassword.Text,"MD5");
string sex;
if(RadioButtonMan.Checked)
{
sex="男";
}
else
{
sex="女";
}
string trueName=TextRelName.Text;
string phone=TextPhone.Text;
string address=TextAddress.Text;
string email=TextEmail.Text;
string sql="insert into User_Table values('"+name+"','"+pass+"','"+sex+"','"+trueName+"','"+phone+"','"+email+"','"+address+"')";
SqlConnection con=DB.creatSqlCon();
con.Open();
SqlCommand com=new SqlCommand(sql,con);
int i=Convert.ToInt32(com.ExecuteNonQuery());[在此处出现异常]
if(i>0)
{
return true;
}
else
{
return false;
}
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btmIsName_Click(object sender, EventArgs e)
{
int i=isName();
if (i >0)
{
RegisterStartupScript("yes", "<script>alart('会员名已经存在!')</script>");
}
else
{
RegisterStartupScript("no", "<script>alart('可以注册')</script>");
}
}
protected void btmLogin_Click(object sender, EventArgs e)
{
int i=isName();
if (i<=0)
{
if (add())
{
SqlConnection con = DB.creatSqlCon();
con.Open();
SqlCommand com = new SqlCommand("insert User_Longin values('" + TextName.Text + "','" + DateTime.Now.ToString() + "')", con);
com.ExecuteNonQuery();
Response.Write("<script>alert('注册成功!单击确定返回登陆页面');location='landing.aspx';</script>");
con.Close();
}
else
Response.Write("<script>alert('注册失败!')</script>");
}
else
{
Response.Write("<script>alert('会员名已经存在!')</script>");
}
}
}