Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL Server
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;
cnn.Open();
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.......!!!!!.');
</script>;";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script);
}
 
//In web.config
<connectionStrings>
<add name="sqlconnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
Please Help me....
Posted 24-Apr-13 1:00am

1 solution

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

Solution 1

Would you please attache the error screen shot?
 
Thanks,
Siju.
  Permalink  
Comments
Member 10009798 at 24-Apr-13 6:31am
   
why do use SqlCommandBuilder & SqlDataAdapter??
Rohan Leuva at 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
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
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