提交后,数据不会插入表中

时间:2016-06-11 20:40:40

标签: c# sql asp.net visual-studio

我有一个看起来像这样的注册表: signup.aspx.cs -

protected void submit_Click(object sender, EventArgs e)
{
    string name = Request.Form["name"];
    string email = Request.Form["email"];
    string password = Request.Form["password"];
    string fileName = "Database.mdf";
    string sql = "INSERT INTO UserInfo VALUES('" + name + "','" + email + "','" + password + "')";
    MyAdoHelper.DoQuery(fileName,sql);
}

myAdoHelper.DoQuery -

 public static void DoQuery(string fileName, string sql)
{

    SqlConnection conn = ConnectToDb(fileName);
    conn.Open();
    SqlCommand com = new SqlCommand(sql, conn);
    com.ExecuteNonQuery();
    com.Dispose();
    conn.Close();

}

MyAdoHelper.ConnectToDb

public static SqlConnection ConnectToDb(string fileName)
{
    string path = HttpContext.Current.Server.MapPath("App_Data/");
    path += fileName;
    //string path = HttpContext.Current.Server.MapPath("App_Data/" + fileName);
    string connString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\User\Documents\Visual Studio 2015\WebSites\WebSite6\App_Data\Database.mdf; Integrated Security = True";
    SqlConnection conn = new SqlConnection(connString);
    return conn;

}

这是我的表 -

CREATE TABLE [dbo].[UserInfo] (
[Name]     NVARCHAR (50) NOT NULL,
[Email]    NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL

);

点击注册后,它看起来已经提交但是当我转到表格数据时,它只在表格中创建一个没有数据的新行 这是2个寄存器后的表 Table

signup.aspx -

    


      

      <div style="margin:auto;width:40%;border:solid;background-color:white">
    <div style="padding-bottom:6px;background-color:gray;width:100%;">
        <h2 style="color:black;text-align:center">Sign-Up</h2>
        <p style="color:black;text-align:center">Already Have An Accout? <a href="login.aspx">Login</a></p>
    </div> 
          <br />
          <table>
              <tr>
                  <td><asp:TextBox ID="name" cssclass="textboxfocus" placeholder="Name" name="name" required="required" runat="server"></asp:TextBox> </td>
                  <td><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="name" Display="Dynamic" ErrorMessage="Name Must be At least 3 chars" ForeColor="red" ValidationExpression="^[\s\S]{3,1000}$"></asp:RegularExpressionValidator></td>
             </tr>
              <tr>
                  <td> <asp:TextBox ID="email" TextMode="Email" placeholder="Email" name="email" required="required" runat="server"></asp:TextBox></td>
                  <td><asp:regularexpressionvalidator runat="server" errormessage="Please enter a valid email" Display="Dynamic" ControlToValidate="email" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:regularexpressionvalidator></td>

             </tr>
              <tr>
                  <td><asp:TextBox ID="password" TextMode="Password" placeholder="Password" name="password" required="required" runat="server"></asp:TextBox></td>
                  <td>
                  <asp:regularexpressionvalidator ValidationExpression = "^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$" runat="server" ForeColor="red" ControlToValidate="password" errormessage="Minimum 8 characters required, at least 1 Alphabet and 1 Number"></asp:regularexpressionvalidator></td>
              </tr>
              <tr>
                  <td><asp:TextBox ID="repassword" TextMode="Password" placeholder="Repeat Password" name="repassword" required="required" runat="server"></asp:TextBox></td>
                  <td> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="password" ControlToValidate="repassword" ErrorMessage="Passwords Dont matched" ForeColor="Red" Display="Dynamic"></asp:CompareValidator></td>
              </tr>
          </table>     
            <br />
            <asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click" />
            <input type="reset" value="Reset" />

           </div>

0 个答案:

没有答案