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 ...........
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit ChoudharyPosted Apr 1, 2010, 2:09 AM
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
}
ajay rajuPosted Mar 31, 2010, 8:56 AM
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