HI everyone
i have created a form in c#. This form is connected with the database. i want to add data with the help of button. for example if i click on add button the data should add in my database.
any one please help me.
thanks

In Form design mode, double click your Add button. You will be in a Add button click event handler. Now write whatever code you want to store your data in your database.

In Form design mode, double click your Add button. You will be in a Add button click event handler. Now write whatever code you want to store your data in your database.

in click event i don't know what code need to enter. if i get the add data code through button then i will be able to add data. please reply me with the code

You will need to use a ConnectionString and SqlCommand.

Create a new connection string in the project properties, under Settings. Give it a name, type = string and Value something like this:

server=YourServerName;database=DatabaseName; Integrated Security=SSPI;

or

server=YourServerName;database=DatabaseName; Username=username; Password=password;

Once you have the connection string, include it in the class by using the following:

static SqlConnection cn = new SqlConnection(MyProject.Properties.Settings.Default.ConnectionString);

Now you can use this connection string as many times as you want.
In the button click event you can do something like this:

SqlCommand cmd = new SqlCommand("Your command text (stored procedure or SQL Query goes here", cn);
cmd.CommandType = CommandType.StoredProcedure; // if its a stored procedure or
cmd.CommandType = CommandType.Text; // if its a simple query.

cn.Open();
cn.ExecuteNonQuery();
cn.Close();

You can do much more, you can pass parameters to stored procedures if you are using them, return values etc.
Search on google, you will find lots of resources. This will get u started.

commented: Good explanation. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.