【求助】vs20008调试时出现“插入错误: 列名或所提供值的数目与表定义不匹配。”
初学中……做一个注册页面,注册时点击“注册”,出现如下所示的错误!~插入错误: 列名或所提供值的数目与表定义不匹配。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 插入错误: 列名或所提供值的数目与表定义不匹配。
源错误:
行 70: SqlCommand com = new SqlCommand(sqlIns, con);
行 71: //判断ExecuteNonQuery方法返回的参数是否大于0,大于0表示注册成功
行 72: if (com.ExecuteNonQuery() > 0)
行 73: {
行 74: RegisterStartupScript("", "<script>alert('注册成功!')</script>");
源文件: f:\dengyiweb\myweb\Register.aspx.cs 行: 72
堆栈跟踪:
[SqlException (0x80131904): 插入错误: 列名或所提供值的数目与表定义不匹配。]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950954
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846939
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +317
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
Register.btnRegister_Click(Object sender, EventArgs e) in f:\dengyiweb\myweb\Register.aspx.cs:72
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
其前台部分代码如下:
<table width="902" height="369" border="0" cellpadding="0" cellspacing="0" background="images/zc_06.jpg">
<tr>
<td align="center" valign="top">
<table width="80%" height="352" border="0" cellpadding="0" cellspacing="1" bgcolor="#c0c4c3">
<tr>
<td height="30" colspan="2" align="left" bgcolor="#30536f" class="a">
用户注册
</td>
</tr>
<tr>
<td width="71%" height="320" align="center" bgcolor="#faf6f1">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table border="0" cellpadding="0" cellspacing="0" style="width: 477px; height: 48px;">
<tr>
<td style="width: 88px; height: 24px; text-align: right">
<span style="font-size: 10pt">会 员 名: </span>
</td>
<td style="height: 24px; text-align: left; width: 509px;">
<label>
<asp:TextBox ID="txtName" runat="server" AutoPostBack="True" onFocus="tName();"
OnTextChanged="txtName_TextChanged" Width="128px"></asp:TextBox>
<span class="STYLE1">**</span>
<asp:Label ID="labUser" runat="server" Font-Size="12px" Text="只能输入数字、字母、下划线"
Width="159px"></asp:Label>
<asp:Label ID="labIsName" runat="server" Font-Size="12px"></asp:Label>
</label>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="19%" height="25" align="right">
</td>
<td width="38%">
<label>
</label>
</td>
<td width="43%" align="left">
</td>
</tr>
<tr>
<td height="25" align="right">
密 码:
</td>
<td>
<label>
<asp:TextBox ID="txtPass" runat="server" onFocus="tPass();" onchange="passHint()"
TextMode="Password" Width="128px"></asp:TextBox> </label>
</td>
<td align="left">
<span class="STYLE1">*<table id="tab" border="0" cellpadding="0" cellspacing="0"
style="width: 151px">
<tr>
<td style="width: 276px">
<span style="font-size: 10pt">密码强度:</span>
</td>
<td id="r" style="width: 100px" align="center">
<asp:Label ID="labEbb" runat="server" Text="弱" Width="18px" Font-Size="12px"
ForeColor="#F2EFE7"></asp:Label>
</td>
<td style="width: 92px" align="center">
<asp:Label ID="labStrong" runat="server" Text="强" Width="18px" Font-Size="12px"
ForeColor="#F2EFE7"></asp:Label>
</td>
<td style="width: 106px">
</td>
</tr>
</table>
</span>
</td>
</tr>
<tr>
<td align="right" class="style1" colspan="3">
确认密码:
<label>
<asp:TextBox ID="txtQpass" runat="server" TextMode="Password" Width="128px"></asp:TextBox> </label>
<span class="STYLE1">*<asp:CompareValidator ID="covPass" runat="server" ControlToCompare="txtPass"
ControlToValidate="txtQpass" ErrorMessage="两次密码不一致" Font-Size="12px"></asp:CompareValidator></span>
</td>
</tr>
<tr>
<td height="25" align="right">
昵 称:
</td>
<td>
<label>
<asp:TextBox ID="txtNickname" runat="server" onFocus="tNickName();" Width="128px"></asp:TextBox> </label>
</td>
<td align="left">
<span class="STYLE1">*</span>
</td>
</tr>
<tr>
<td height="25" align="right">
性 别:
</td>
<td valign="middle">
<asp:RadioButtonList ID="radlistSex" runat="server" RepeatDirection="Horizontal"
Width="95px" Font-Size="12px">
<asp:ListItem Selected="True"> 男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
</td>
</tr>
<tr>
<td height="25" align="right">
电 话:
</td>
<td>
<label>
<asp:TextBox onFocus="tPhone();" ID="txtPhone" runat="server" Width="127px"></asp:TextBox> </label>
</td>
<td>
</td>
</tr>
<tr>
<td height="25" align="right">
E - mail :
</td>
<td>
<label>
<asp:TextBox ID="txtEmail" onFocus="tEmail();" runat="server" Width="128px"></asp:TextBox> </label>
</td>
<td align="left">
<span class="STYLE1">*<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail" ErrorMessage="邮件格式不正确" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Font-Size="12px"></asp:RegularExpressionValidator></span>
</td>
</tr>
<tr>
<td height="25" align="right">
所在城市:
</td>
<td>
<label>
<asp:TextBox ID="txtCity" onFocus="tCity();" runat="server" Width="128px"></asp:TextBox> </label>
</td>
<td>
</td>
</tr>
<tr>
<td height="40" colspan="2" align="right" valign="bottom">
<label>
<asp:Button ID="btnRegister" runat="server" OnClick="btnRegister_Click" Text="注 册" />
<asp:Button ID="btnReturn" runat="server" Text="返 回" CausesValidation="False"
PostBackUrl="~/Default.aspx" /> </label>
</td>
<td>
</td>
</tr>
</table>
后台部分代码如下:
protected void btnRegister_Click(object sender, EventArgs e)
{
if (isNameFormar())
{
if (isName())
{
labIsName.Text = "用户名已存在!";
labIsName.ForeColor = System.Drawing.Color.Red;
RegisterStartupScript("", "<script>alert('请正确填写信息!')</script>");
}
else
{
string userName = txtName.Text;
string userPass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPass.Text, "MD5");
string nickname = txtNickname.Text;
string sex = "";
if (radlistSex.SelectedValue.Trim() == "男")
{
sex = "男";
}
else
{
sex = "女";
}
string phone = txtPhone.Text;
string email = txtEmail.Text;
string city = txtCity.Text;
string university = txtUniversity.Text ;
string sqlIns = "insert into tb_userInfo values('" + userName + "','" + userPass + "','" + nickname + "','" + sex + "','" + phone + "','" + email + "','" + city + "','" + university +"')";
SqlConnection con = new SqlConnection("server=.;database=db_xiGeYue;uid=sa;pwd=;");
con.Open();
SqlCommand com = new SqlCommand(sqlIns, con);
if (com.ExecuteNonQuery() > 0)
{
RegisterStartupScript("", "<script>alert('注册成功!')</script>");
txtName.Text =txtPass.Text = txtNickname.Text = txtPhone.Text = txtEmail.Text = txtCity.Text = txtUniversity.Text ="";
labIsName.Text = "";
}
else
{
RegisterStartupScript("", "<script>alert('请正确填写信息!')</script>");
}
}
}
else
{
RegisterStartupScript("", "<script>alert('请正确填写信息!')</script>");
}
}
我个人感觉,有可能是使用RadioButtonList控件作为性别选择时出现问题(比如链接数据库时有问题等),就是不知道怎么改,或时其它问题,请各位老师及朋友帮帮忙,看看是怎么回事!谢谢O(∩_∩)O谢谢!
哎~~~如果说初学困难的话,那么再在初学前面加上自学,那就是难上加难再加难!