Sponsored Ad

Wednesday, March 23, 2011

How to Save Data in Database From Web Form Using Asp.Net With C#

 

Double Click on Save Button then write code on  Default.aspx.cs


protected void btnSave_Click(object sender, EventArgs e){
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "Data Source=ServerName;Initial                                             Catalog=DataBaseName;Integrated Security=true";
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"]=txtID.Text; //ID is a table field
drow["Name"] =txtName.Text;
drow["Designation"] =txtDesignation.Text;
drow["Location"]= txtLocation.Text;
ds.Tables["TableName "].Rows.Add(drow);
da.Update(ds, " TableName");
string script = @"<script language=""javascript"">
alert('Your Information Is Save Successfully.......!!!!!.');
</script>;";
Page.ClientScript.RegisterStartupScript(this.GetType(),        "myJScript1", script);

}

No comments:

Post a Comment

Related Software Testing Articles



Website List