dimanche 12 juin 2016

Data wont be inserted in the table after submiting

I have a register form that looks like this: 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:UsersUserDocumentsVisual Studio 2015WebSitesWebSite6App_DataDatabase.mdf; Integrated Security = True";
    SqlConnection conn = new SqlConnection(connString);
    return conn;

}

This is my table-

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

);

After i click register it looks like it has been submitted but when i go to the table data, it only make a new row in the table with no data inside Here is the table after 2 registers 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="^[sS]{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-zd]{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>

Aucun commentaire:

Enregistrer un commentaire