Hi Guys,
How can we insert the data from ASP.NET page to fill the database table(SQL SERVER 2005).
suppose wanted to insert
Empid
EmpName
EmpDesignation.
Please help me ...........
Insert data in to database in asp.net.......?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

ajay rajuRank #4,639 · 333 reputationPosted Mar 31, 2010, 4:56 AM EDT
if u insert data from asp.net to database simply u can do 2 ways
one is using stored procedures and other is take a newrow method
using newrow: code under insert button
DataRow dr = ds.Tables(0).NewRow()
dr(0) = txtEmpID.Test
dr(1) = txtEname.Text
dr(2) = txtDesignation.Text
ds.Tables(0).Rows.Add(dr)
SqlCommandBuilder cb = new SqlCommandBuilder (da) // da is SqlDataAdapter variable and ds as dataset variable
da.update(ds,"emp")
first u create the object of SqlDataAdapter and DataSet
I hope it helps u.
Thanks
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 31, 2010, 10:09 PM EDT
using System.Data.SqlClient;
using System.Data;
protected void page_load(object sender, EventArgs e)
{
SqlConnection cn=new SqlConnection();
cn.Open();
string qry="insert into Employee (Empid,EmpName,EmpDesignation) values ('"+txtEmpid.Text+"','"+txtEmpName.Text+"','"+txtEmpDesignation.Text+"' )";
SqlCommand cmd=new SqlCommand(qry,cn);
int res= cmd.ExecuteNonQuery();
if(res==1)
Label1.Text="Successfully saved";
else
Label1.Text="Couldn't be saved";
Please mark as answer if it helps
}