Insert Data Into The Database In Windows Form Using C#

Initial chamber

Step 1: Open Visual Studio 2010. Go to File, New, Projects and under Visual C# select Windows.

window form

You can change the name of the project and  browse your project to different location too. And then press OK

Step 2: In Solution Explorer you will get your Project Add Service Based Database by going to your Project. Right click and Add New Item, then select Service-based Database.

Service Based Database

Database chamber

Step 3: Go to your Database Database.mdf and we will create a table tbl_save. Go to the database.mdf, then Table and Add New table. Design your table like the following screenshot:

Tbl_save:


Table

Design chamber

Step 4: Now open your Form1.cs file, where we create our design for inserting data into the database. We will drag three labels, three textbox and one button from the tool box to Form1.cs. You will see your Form look like the following screenshot:
Form1.cs [design]:

form

Code chamber

Right click on the blank part of Form1.cs, then click View Code. You can see you are entered in the code part of the form. Write the following code and then Press F5 to run the project.

Form1.cs:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. using System.Data.SqlClient;  
  11.   
  12. namespace Insert_data_into_table  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void button1_Click(object sender, EventArgs e)  
  22.         {  
  23.             //SqlConnection con = new SqlConnection("Data Source=NiluNilesh;Integrated Security=True");  
  24.             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");  
  25.             SqlCommand cmd = new SqlCommand("sp_insert", con);  
  26.             cmd.CommandType = CommandType.StoredProcedure;  
  27.             cmd.Parameters.AddWithValue("@name", textBox1.Text);  
  28.             cmd.Parameters.AddWithValue("@email", textBox2.Text);  
  29.             cmd.Parameters.AddWithValue("@phone", textBox3.Text);  
  30.             cmd.Parameters.AddWithValue("@address", textBox4.Text);  
  31.             con.Open();  
  32.             int i = cmd.ExecuteNonQuery();  
  33.   
  34.             con.Close();  
  35.   
  36.             if (i!=0)  
  37.             {  
  38.                 MessageBox.Show(i + "Data Saved");   
  39.             }  
  40.              
  41.               
  42.              
  43.   
  44.         }  
  45.   
  46.         public static void main(string[] args)  
  47.         {  
  48.   
  49.             Application.Run(new Form1());  
  50.           
  51.         }  
  52.     }  
  53. }  
Output chamber

Output

Data in the Database:

Data in the Database

 

Up Next
    Ebook Download
    View all
    Learn
    View all