Click here to Skip to main content
12,997,531 members (58,928 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am using this code to store data into a table in sql server. But it shows me an error:"C# code to insert data in database".

Here is my code:
using System.Data.SqlClient;

protected void Button1_Click(object sender, EventArgs e)
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnection "].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from TableName";
cmd.Connection = cnn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, " TableName ");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataRow drow = ds.Tables["TableName"].NewRow();
drow["id"] = TextBox1.Text;
drow["Name"] = TextBox2.Text;
drow["Designation"] = TextBox3.Text;
drow["Mobile_No"] = TextBox4.Text;
drow["Address"] = TextBox5.Text;

ds.Tables["TableName "].Rows.Add(drow);
da.Update(ds, " TableName ");
string script = @"<script language=""javascript"">
alert('Information have been Saved Successfully.......!!!!!.');
Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script);

//In web.config
<add name="sqlconnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
Please Help me....
Posted 24-Apr-13 0:00am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Would you please attache the error screen shot?

Member 10009798 24-Apr-13 6:31am
why do use SqlCommandBuilder & SqlDataAdapter??
Rohan Leuva 24-Apr-13 6:35am
you should post this as comment not as a answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100