How can I connect input form in HTML to Database Table via ASP.Net

I have a button in my HTML code in an ASP project. The method Button1_Click() in aspx.cs where I actually define my connection and insert values to a table.

This is a snippet from aspx where I call Button1_Click():

<form id="form">
    <input type="button"
           onclick="checking(); Button1_Click();"
           value="Check aviability"
           id="buttoncheck">
</form>

When I type something in my form on the website, and then click the button, no data gets inserted to my SQL Server table.

Code for Button1_Click():

protected void Button1_Click(object sender, EventArgs e)
{
    {
        string SQLInsert = "INSERT INTO[dbo].[User_search] (
            [Name],[Surname],[In_date],[Out_date]," +
            "[People_num],[In_business],[Email],[Phone_number])" +
            "Values(@Name, @Surname, @In_date, @Out_date, @People_num," +
            "@In_business, @Email, @Phone_number)";

        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand(SQLInsert, con);
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

The values aren’t added to DB table. Any idea how to solve this problem?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

  1. In ASP.NET WebForm Project, Button_Click() is a code-behind function, so in your HTML code (.aspx), the button should be declared in this form:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
  1. In your code-behind class (xxxx.aspx.cs), protected void Button1_Click(object sender, EventArgs e) method will be triggered when the Button1 is clicked. And your function is to execute an insert SQL, but it misses some parameters…. I have no idea what the datatype is, so I assume the parameters are string:
protected void Button1_Click(object sender, EventArgs e)
{
    string SQLInsert = "INSERT INTO[dbo].[User_search] (
            [Name],[Surname],[In_date],[Out_date]," +
            "[People_num],[In_business],[Email],[Phone_number])" +
            "Values(@Name, @Surname, @In_date, @Out_date, @People_num," +
            "@In_business, @Email, @Phone_number)";
            
    using (SqlConnection con = new SqlConnection(constr))
    {
        SqlCommand cmd = new SqlCommand(SQLInsert, con);
        cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
        cmd.Parameters["@Name"].Value = "test1";
        
        cmd.Parameters.Add("@Surname", SqlDbType.NVarChar);
        cmd.Parameters["@Surname"].Value = "test2";
        
        cmd.Parameters.Add("@In_date", SqlDbType.NVarChar);
        cmd.Parameters["@In_date"].Value = "test3";
        
        cmd.Parameters.Add("@Out_date", SqlDbType.NVarChar);
        cmd.Parameters["@Out_date"].Value = "test4";
        
        cmd.Parameters.Add("@People_num", SqlDbType.NVarChar);
        cmd.Parameters["@People_num"].Value = "test5";
        
        cmd.Parameters.Add("@In_business", SqlDbType.NVarChar);
        cmd.Parameters["@In_business"].Value = "test6";
        
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
        cmd.Parameters["@Email"].Value = "test7";
        
        cmd.Parameters.Add("@Phone_number", SqlDbType.NVarChar);
        cmd.Parameters["@Phone_number"].Value = "test8";

        try
        {
            con.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x