Insert the data into database through the query string in c#



In this post, we have explained the example to insert the data into the database.

Important Namespaces Used
--------------------------

using System.Data;
using System.Data.SqlClient;


---------------------------


Comment Button Click Event Coding
--------------------------------

String query = "insert into commentdetail(name,email,comment) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text +  "')";
        String mycon = "Data Source=HP-PC\\SQLEXPRESS; Initial Catalog=CommentDatabase; Integrated Security=true";
        SqlConnection con = new SqlConnection(mycon);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = query;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        Label3.Text = "Comment Has Been Saved Successfully";
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        DataList1.DataBind();


---------------------------------

0 Comment's

Comment Form

Submit Comment