为什么这种方法会不断将空值添加到我的数据库中?

时间:2017-04-13 17:20:40

标签: c# sql asp.net database insert-statement

以下是注册表单的代码。当我运行它并输入值时,我检查我的数据库并添加了一条记录,但每个字段都为空。我发现我的代码没有任何问题会导致这种情况。

<script runat="server">
         private void InsertUser (object source, EventArgs e) {
             SqlDataSource2.Insert();
      }</script>

...

<asp:Label ID="Label1" runat="server" Text="Register with us!"></asp:Label>
   <br />
       <asp:Label ID="Label2" runat="server" Text="Email:"></asp:Label>
       <asp:TextBox ID="email_txt" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="email_txt" ErrorMessage="Required!"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="email_txt" ErrorMessage="Not a valid email!" ValidationExpression="^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$"></asp:RegularExpressionValidator>
    <br />
    <asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
    <asp:TextBox ID="password_txt" runat="server" TextMode="Password"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="password_txt" ErrorMessage="Required!"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="Label4" runat="server" Text="Repeat Password:"></asp:Label>
    <asp:TextBox ID="password2_txt" runat="server" TextMode="Password"></asp:TextBox>
    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="password_txt" ControlToValidate="password2_txt" ErrorMessage="Not the same!"></asp:CompareValidator>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="password2_txt" ErrorMessage="Required!"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="Label5" runat="server" Text="Address Line One:"></asp:Label>
    <asp:TextBox ID="address1_txt" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="address1_txt" ErrorMessage="Required!"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="Label6" runat="server" Text="Address Line Two:"></asp:Label>
    <asp:TextBox ID="address2_txt" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label7" runat="server" Text="Mobile Number:"></asp:Label>
    <asp:TextBox ID="mobileno_txt" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="mobileno_txt" ErrorMessage="Required!"></asp:RequiredFieldValidator>
    <br />
    <asp:Button ID="register_btn" runat="server" OnClick="InsertUser" Text="Register" />
    <br />
    <asp:sqldatasource
        id="SqlDataSource2"
        runat="server"
        connectionstring="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT Users.* FROM Users"
        insertcommand="INSERT INTO Users (username,password,address_line_one,address_line_two,mobile_number) VALUES (@username,@password,@address1,@address2,@mobileno)">
          <insertparameters>
            <asp:formparameter name="username" formfield="email_txt" />
            <asp:formparameter name="password"  formfield="password_txt" />
            <asp:formparameter name="address1"  formfield="address1_txt" />
            <asp:formparameter name="address2"  formfield="address2_txt" />
            <asp:formparameter name="mobileno"  formfield="mobileno_txt" />
          </insertparameters>
      </asp:sqldatasource>

我在https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insert(v=vs.110).aspx#Version信息中找到了这个信息作为一些文档,如果这有帮助!

0 个答案:

没有答案