S
S.Kartikeyan
I have MSSQL SERVER
I have the following C# function
But the database entries are not being created why
Do i need to change any database settings?????
If i remove the transaction it is working
public static bool CreateNewStudent(string userid,string passwd,string
emailid,string roll,string title,string fullname,string batch,string
program,string department)
{
bool success = false;
SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SQL_CONNECTION_STRING"]);
con.Open();
SqlCommand cmd = con.CreateCommand();
SqlTransaction createStudentTrans =
con.BeginTransaction(IsolationLevel.Serializable);
cmd.Connection = con;
cmd.Transaction = createStudentTrans;
try
{
//order of commands is important due to foreign key contraints
//command 1 of transaction
cmd.CommandText = "INSERT INTO
ulsusers(userid,passwd,role,lastlogin,createdon,emailid)
VALUES(@userid,@passwd,@role,@lastlogin,@createdon,@emailid)";
cmd.Parameters.Add("@userid",userid);
cmd.Parameters.Add("@passwd",FormsAuthentication.HashPasswordForStoringInConfigFile(passwd,"MD5"));
cmd.Parameters.Add("@role","student");
cmd.Parameters.Add("@lastlogin",DateTime.Now);
cmd.Parameters.Add("@createdon",DateTime.Now);
cmd.Parameters.Add("@emailid",emailid);
int rows = cmd.ExecuteNonQuery();
//command 2 of transaction
cmd.CommandText = "INSERT INTO
ulsstudents(userid,roll,title,fullname,batch,program,department) "+
" VALUES(@userid,@roll,@title,@fullname,@batch,@program,@department)";
cmd.Parameters.Add("@userid",userid);
cmd.Parameters.Add("@roll",roll);
cmd.Parameters.Add("@title",title);
cmd.Parameters.Add("@fullname",fullname);
cmd.Parameters.Add("@batch",batch);
cmd.Parameters.Add("@program",program);
cmd.Parameters.Add("@department",department);
rows += cmd.ExecuteNonQuery();
//commit if both successfull
createStudentTrans.Commit();
success = (rows==2);
}
catch(Exception e)
{
try
{
createStudentTrans.Rollback();
}
catch(Exception ex)
{
}
}
con.Close();
return success;
}
I have the following C# function
But the database entries are not being created why
Do i need to change any database settings?????
If i remove the transaction it is working
public static bool CreateNewStudent(string userid,string passwd,string
emailid,string roll,string title,string fullname,string batch,string
program,string department)
{
bool success = false;
SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SQL_CONNECTION_STRING"]);
con.Open();
SqlCommand cmd = con.CreateCommand();
SqlTransaction createStudentTrans =
con.BeginTransaction(IsolationLevel.Serializable);
cmd.Connection = con;
cmd.Transaction = createStudentTrans;
try
{
//order of commands is important due to foreign key contraints
//command 1 of transaction
cmd.CommandText = "INSERT INTO
ulsusers(userid,passwd,role,lastlogin,createdon,emailid)
VALUES(@userid,@passwd,@role,@lastlogin,@createdon,@emailid)";
cmd.Parameters.Add("@userid",userid);
cmd.Parameters.Add("@passwd",FormsAuthentication.HashPasswordForStoringInConfigFile(passwd,"MD5"));
cmd.Parameters.Add("@role","student");
cmd.Parameters.Add("@lastlogin",DateTime.Now);
cmd.Parameters.Add("@createdon",DateTime.Now);
cmd.Parameters.Add("@emailid",emailid);
int rows = cmd.ExecuteNonQuery();
//command 2 of transaction
cmd.CommandText = "INSERT INTO
ulsstudents(userid,roll,title,fullname,batch,program,department) "+
" VALUES(@userid,@roll,@title,@fullname,@batch,@program,@department)";
cmd.Parameters.Add("@userid",userid);
cmd.Parameters.Add("@roll",roll);
cmd.Parameters.Add("@title",title);
cmd.Parameters.Add("@fullname",fullname);
cmd.Parameters.Add("@batch",batch);
cmd.Parameters.Add("@program",program);
cmd.Parameters.Add("@department",department);
rows += cmd.ExecuteNonQuery();
//commit if both successfull
createStudentTrans.Commit();
success = (rows==2);
}
catch(Exception e)
{
try
{
createStudentTrans.Rollback();
}
catch(Exception ex)
{
}
}
con.Close();
return success;
}