Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Wednesday, July 28, 2010

Transaction in .Net(Insert Data through Stored Procedure)

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master"].ConnectionString);
SqlTransaction tran=null ;
SqlCommand cmd;
SqlDataAdapter adp;
DataSet ds;

try
{
con.Open();
tran=con.BeginTransaction();



cmd = new SqlCommand("InserData", con);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("Name", txtName.Text.Trim().Replace("'", "''"));
cmd.Parameters.AddWithValue("dept_id", ddldept.SelectedValue);
cmd.Parameters.AddWithValue("Contact", txtContact.Text.Trim().Replace("'", "''"));
cmd.Transaction = tran;
cmd.ExecuteNonQuery();
tran.Commit();
}
catch
{

tran.Rollback();
con.Close();
}
finally { con.Close(); }

No comments:

Post a Comment