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

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.

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:

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]:

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:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace Insert_data_into_table
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //SqlConnection con = new SqlConnection("Data Source=NiluNilesh;Integrated Security=True");
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("sp_insert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@name", textBox1.Text);
- cmd.Parameters.AddWithValue("@email", textBox2.Text);
- cmd.Parameters.AddWithValue("@phone", textBox3.Text);
- cmd.Parameters.AddWithValue("@address", textBox4.Text);
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
- if (i!=0)
- {
- MessageBox.Show(i + "Data Saved");
- }
- }
- public static void main(string[] args)
- {
- Application.Run(new Form1());
- }
- }
- }
Data in the Database:


JustA StudentPosted Dec 13, 2021, 10:29 PM
If you're as confused as I was here is a video he made. https://youtu.be/1BO-HP3rP9Y If you're not using a stored procedure get rid of cmd.CommandType=CommandType.StoredProcedure; . on his Line 26
SOLO BEATINGOPosted Aug 15, 2019, 8:30 PM
What is( "sp_insert",) stands for? can you tell me?
שלומי אסוסPosted Sep 2, 2018, 1:47 AM
Hy..I want to make a combo box that will show options from another table and save what the user chosed to a different table... How can it be done? (it's a local access data base)
sumit kumarPosted Jul 5, 2018, 6:07 AM
Sorry the give code is not working and no any service based database attached in the code. its totally useless.
Kernel Hard StonePosted Jan 18, 2018, 9:15 PM
Nilesh i need help
Adnan DelicPosted Oct 5, 2017, 4:24 PM
Problem here is that you didnt specify(write) code of stored procedure what is done on database level. Name of stored procedure in database is sp_insert but in your article nobody can see that code.
Handy TamponPosted Sep 30, 2017, 9:14 AM
Are s this included? public static void main(string[] args) { Application.Run(new Form1()); }
Darshan SinghPosted Jun 14, 2017, 7:07 AM
SqlCommand cmd = new SqlCommand("sp_insert", con);What is "sp_insert" is here(line no 25)...
Vijay KumarPosted Mar 29, 2017, 12:38 PM
Given Code is not working and both the code (given here and for download (WindowsFormsApplication4.zip) ) are different
Manvendra PatelPosted Apr 29, 2016, 7:53 AM
i used ur code but data not inserting nor getting any error .why?
Divya SindhuPosted Nov 24, 2015, 2:55 AM
nice article. :-)
Arul RPosted Oct 28, 2015, 5:29 AM
Good one !!!
Santhakumar MunuswamyPosted Oct 18, 2015, 4:03 AM
Good one
Muhammad Aqib ShehzadPosted Oct 12, 2015, 7:12 AM
nice one
Sandeep KumarPosted Oct 12, 2015, 12:10 AM
Good Explanation
Priyaranjan K SPosted Oct 10, 2015, 10:10 PM
Good One . . .
Sujeet SumanPosted Oct 10, 2015, 2:19 PM
Nice Article.............