Click here to Skip to main content
15,884,697 members
Please Sign up or sign in to vote.
1.00/5 (1 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;
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
XML
<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

1 solution

Would you please attache the error screen shot?

Thanks,
Siju.
 
Share this answer
 
Comments
Member 10009798 24-Apr-13 6:31am    
why do use SqlCommandBuilder & SqlDataAdapter??
Thanks7872 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900