Saturday, August 5, 2017

// // Leave a Comment

How to insert data to table using button click C#



In this article I am going to explain How to insert data to table using button click event with C#

First you have to create a new web project as we discussed earlier article. How to create new project with visual studio

Watch Video

I am going to user how to bind data with dropdownlist for this project also.

First we have to design screen layout, Text box and Button controller as the previous projects, you have to drag and drop controllers from toolbox to designing form.

Next you can change the ID from properties and double click on the Button controller
then button Click even coding screen will display.



You have to type the coding as displayed below.

using System.Data.SqlClient;

Coding for Button Click event

protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection sqlConnection1 = new SqlConnection("Data Source=SERVER;Initial Catalog=SUPERMARKET;Integrated Security=True");

        SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "INSERT Banks (b_name) VALUES ('" + txtBname.Text + "')";
        cmd.Connection = sqlConnection1;

        sqlConnection1.Open();
        cmd.ExecuteNonQuery();
        sqlConnection1.Close();

        GridView1.DataBind();
    }

Finally run the application and you can input name for text box and press Save button. Then bank name with save to SQL table.


For details Watch Video



0 comments:

Post a Comment