problem with sqltransction commit in c# file

Posted by Vinay_moduga under ASP.NET on 11/1/2009 | Views : 1230 | Status : [Member] | Replies : 5
I am using sqltransaction in my code behind file. It has structure like this

try
{
if (cn.State == ConnectionState.Closed)
cn.Open();
tran = cn.BeginTransaction();
bl.SpecialStudentDetails(admisionnumber, stdname, surname, dateofbirth, dateofjoin, clas, section, schoolname, prevdatejoin, prevyearsstudied, skooladdress, reasonfordiscont, fathername, mothername, fatheroccupation, motheroccupation, address, paymnttype, modeofpaymnt, donation, annualsalary,discount,feamountpaid, emailid, contactnumber, istcsubmitted, dateofbirthproof, lastclasspercentage, permntaddress, image, phonenumber);

for (int i = 1; i <= numbofinstlments; i++)
{

string gvndat=tb[tempcount].Text;
tempcount++;
double amnt = Convert.ToDouble(tb[tempcount].Text);
tempcount += 2;
bl.SpecialStudentPaymentDetails(admisionnumber, gvndat, amnt, clas, section,i);
cnt++;
}
tran.Commit();
}


catch (Exception ex)
{
Response.Write( ex.Message);
tran.Rollback();
}


the above for loop rotates based on the numbofinstlments i had given .

my problem is even though there is a problem in numbofinstlments loop

the transaction is getting commited with out rollback

please tell me the solution for this




Responses

Posted by: Lakhangarg on: 11/2/2009 [Member] [Moderator] Silver

Up
0
Down
HI-

Move these lines : if (cn.State == ConnectionState.Closed)
cn.Open();
tran = cn.BeginTransaction();

before the start of try block. then check..
i hope this will resolve your issue.


Thanks & Regards
Lakhan Pal Garg

Vinay_moduga, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay_moduga on: 11/2/2009 [Member] Starter

Up
0
Down
thnk you for your reply

even keeping outside the try block its not working . the same problem is
persisting.

Vinay_moduga, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakhangarg on: 11/2/2009 [Member] [Moderator] Silver

Up
0
Down
Hi-

Where are you passing tran (Transaction Object). means to create connection with database ..

if possible send me the complete code so that i can send the answer accordingly.

Thanks & Regards
Lakhan Pal Garg

Vinay_moduga, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay_moduga on: 11/2/2009 [Member] Starter

Up
0
Down
here default is my class name and here I had declared my tran



public partial class Default : System.Web.UI.Page
{
businesslogiclayer bl = new businesslogiclayer();
SqlDataReader dr;
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["skoolConnectionString"].ConnectionString);
SqlTransaction tran;

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

}
}
---------------------------------------------------------------------------------
if (numbofinstlments > 0)
{
if (cn.State == ConnectionState.Closed)
cn.Open();
tran = cn.BeginTransaction();
try
{
bl.SpecialStudentDetails(admisionnumber, stdname, surname, dateofbirth, dateofjoin, clas, section, schoolname, prevdatejoin, prevyearsstudied, skooladdress, reasonfordiscont, fathername, mothername, fatheroccupation, motheroccupation, address, paymnttype, modeofpaymnt, donation, annualsalary,discount,feamountpaid, emailid, contactnumber, istcsubmitted, dateofbirthproof, lastclasspercentage, permntaddress, image, phonenumber);

for (int i = 1; i <= numbofinstlments; i++)
{

string gvndat=tb[tempcount].Text;
tempcount++;
double amnt = Convert.ToDouble(tb[tempcount].Text);
tempcount += 2;
bl.SpecialStudentPaymentDetails(admisionnumber, gvndat, amnt, clas, section,i);
cnt++;
}
tran.Commit();
}


catch (Exception ex)
{
Response.Write( ex.Message);
tran.Rollback();
}

}
else
{
r = bl.InsertStudentDetails(admisionnumber, stdname, surname, dateofbirth, dateofjoin, clas, section, schoolname, prevdatejoin, prevyearsstudied, skooladdress, reasonfordiscont, fathername, mothername, fatheroccupation, motheroccupation, address, paymnttype, modeofpaymnt, donation, annualsalary, discount, feamountpaid, emailid, contactnumber, istcsubmitted, dateofbirthproof, lastclasspercentage, permntaddress, image, phonenumber);
}
if (r > 0 || numbofinstlments==cnt)
{
Label1.Visible = true;
Label1.Text = stdname + " With AdmissionNumber " + admisionnumber + " Is Successfully Inserted ";
}

Vinay_moduga, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay_moduga on: 11/2/2009 [Member] Starter

Up
0
Down
please tell me how to copy data from multiple tables present in one database to a single table which is present in another database.

here i am extracting the data present in the three tables based on some
condition.

i want sql query for this


Vinay_moduga, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response