SqlTransaction has completed; it is no longer usable...
This is the code:
if (mfnSaveForm() == true)
{
// (Before commintting record this error had come.)
GblDeclarations.gblSQLTrans.Commit();
MessageBox.Show("Records Updated Successfully", "RECORD SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSave.Enabled = false;
GblDeclarations.gblTransBegin = false;
}
Please Give me the solution....Thank you
Loading
Zoran HorvatPosted Jul 8, 2011, 7:52 AM
Now to rectify the problem, you should decide which method will be in charge of the transaction btnSave_Click or UpdateRecord. Looking at btnSave_Click method, it doesn't really do anything with database. So I would suggest that you completely remove transaction-related statements from btnSave_Click and leave UpdateRecord method to control the transaction.
In that case btnSave_Click method would become quite simple:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
string StrMsg = null;
StrMsg = mfnValidateSave();
boolSave = true;
if (mfnSaveForm() == true)
{
MessageBox.Show("Records Updated Successfully", "RECORD SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSave.Enabled = false;
}
else
{
boolSave = false;
}
}
catch (Exception ex)
{
GV.gprocErrorHandler(ex.Source, ex.Message, "btnSave_Click", this.Name);
}
}
Zoran
Sutha ShaliniPosted Jul 21, 2011, 1:32 AM
Sutha ShaliniPosted Jul 21, 2011, 1:26 AM
Sutha ShaliniPosted Jul 21, 2011, 1:22 AM
I got again such kind of problem...
I dont know how to solve this.. Plz any one help me
code:
debug="true"
/>
/>
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>
responseEncoding="utf-8"
/>
Dinesh BeniwalPosted Jul 8, 2011, 8:08 AM
Sutha ShaliniPosted Jul 8, 2011, 8:05 AM
Fine.. I do agree..
Fine i'll continue with this code only..
Thank you so much.. really thanks..
Zoran HorvatPosted Jul 8, 2011, 8:01 AM
General rules regarding transactions are these:
1. Low level data component should not control transactions and should not know about them.
2. GUI components should not do that either.
3. Component which controls data flow should be in the middle and that should control the transactions.
4. Data component performs complete operations on database, which may include number of calls to low level data component; all those calls should be wrapped in a single transaction.
Zoran
Sutha ShaliniPosted Jul 8, 2011, 7:57 AM
see this,
But i dont know its correct or not, But record is saving properly.
GblDeclarations.gblSQLTrans.Commit();
This line i've given in first step itself.
But that line has to be remove from the second step
Thank you.
Plz any one find the solution plz Reply me
the same ans. but i've mentioned as Step 1 and step2.
Anyway
Zoran. Thank you so much...
U know one thing this solution will create many problems in future coding...
Thank you so much..
Sutha ShaliniPosted Jul 8, 2011, 7:47 AM
Sutha ShaliniPosted Jul 8, 2011, 7:35 AM
-> Transaction begins and GblDeclarations.gblTransBegin=true;
-> and Its goes to mfnSaveForm() method;
-> after that it goes to UpdateRecord() method like given below code
//UpdateRecord
public long UpdateRecord(string strWhereCondition, SqlConnection dbCon, string SQLUpdate)
{
long RowCount = 0;
try
{
SqlCommand dbCom = new SqlCommand();
dbCom.CommandType = CommandType.Text;
//GD.gblSQLTrans = dbCon.BeginTransaction();
//GD.gblTransBegin = true;
if (!string.IsNullOrEmpty(SQLUpdate))
{
dbCom.CommandText = SQLUpdate;
}
else
{
dbCom.CommandText = mfnGenerateUpdateStatement() + " WHERE " + strWhereCondition;
}
dbCom.Connection = dbCon;
if (GblDeclarations.gblTransBegin)
dbCom.Transaction = GblDeclarations.gblSQLTrans;
//dbCon.Close();
//dbCon.Open();
RowCount = dbCom.ExecuteNonQuery();
dbCon.Close();
}
catch
{
}
return RowCount;
}
//
-> and the RowCount is returning the value =1
-> then
if (mfnSaveForm() == true)
{
// (Before commintting record this error had come.)
GblDeclarations.gblSQLTrans.Commit();
MessageBox.Show("Records Updated Successfully", "RECORD SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSave.Enabled = false;
GblDeclarations.gblTransBegin = false;
}
-> mfnSaveForm() method ==True
fine its happening
but once its reach GblDeclarations.gblSQLTrans.Commit();
method giving an error.
this is the flow
Zoran HorvatPosted Jul 8, 2011, 7:28 AM
Zoran HorvatPosted Jul 8, 2011, 7:27 AM
Zoran
Sutha ShaliniPosted Jul 8, 2011, 7:26 AM
No. In catch block also
gblTransBegin =true only.
Eventhough i'mgetting same error.
Help me out
Sutha ShaliniPosted Jul 8, 2011, 7:23 AM
Anyway , Thank you very much... i'll checkout that now
Zoran HorvatPosted Jul 8, 2011, 7:19 AM
Zoran
Sutha ShaliniPosted Jul 8, 2011, 7:17 AM
Sutha ShaliniPosted Jul 8, 2011, 6:57 AM
But i dont know its correct or not, But record is saving properly.
GblDeclarations.gblSQLTrans.Commit();
This line i've given in first step itself.
But that line has to be remove from the second step
Thank you.
Plz any one find the solution plz Reply me
Sutha ShaliniPosted Jul 8, 2011, 6:48 AM
Overrides the save form:
step 1:
public override bool mfnSaveForm()
{
bool functionReturnValue = false;
try
{
long lngPID = 0;
ColSaveData CSave = new ColSaveData();
CSave.Table_Name = "tblPen_PensionerDetails";
CSave.ID_Field = "PensionerID";
//Personal Details
CSave.Add("PensionClass", cmbClassOfPension.Text,false,true,"");
if (cmbPensionerTitle.SelectedIndex > -1)
{
CSave.Add("TitleOfRespect", cmbPensionerTitle.Text, false, true, "");
}
if (cmbPensionerGender.SelectedIndex > -1)
{
CSave.Add("Gender", cmbPensionerGender.Text, false, true, "");
}
CSave.Add("PensionerName", txtPensionerName.Text, false, true, "");
CSave.Add("PPONo", txtPPONo.Text, false, true, "");
CSave.Add("PensionerCode", txtPensionerCode.Text, false, true,"");
CSave.Add("DateOfBirth", GV.gfnFormatSQLDate(udtPensionerDOB.Value), false, true, "");
if (udtPensionerDOR.Value != null)
{
CSave.Add("DateOfRetirement", GV.gfnFormatSQLDate(udtPensionerDOR.Value), false, true, "");
}
CSave.Add("PPOwef", GV.gfnFormatSQLDate(udtPPO_WEF.Value),false,true,"");
//Office Details
// string DESIGName = GV.gfnGetName("SELECT DesignationName FROM tblMast_DesignationMaster where DesignationID='" + Convert.ToInt32(cmbDesignation.SelectedValue) + "'");
CSave.Add("DesignationID", cmbDesignation.SelectedValue.ToString(), false, true, "");
CSave.Add("EmployeeGroup", cmbGroup.Text, false, true, "");
CSave.Add("DAType", cmbPensionerDAType.Text, false, true, "");
CSave.Add("PayScaleID",Convert.ToString(cmbPayScale.SelectedValue), false, true, "");
//string strDivisionName = GV.gfnGetName("SELECT DivisionName FROM tblMast_DivisionMaster where DivisionID='" + Convert.ToInt32(cmbDivision.SelectedValue) + "'");
CSave.Add("DivisionID", cmbDivision.SelectedValue.ToString(), false, true, "");
//string strUnitName = GV.gfnGetName("SELECT SSAUnitName,SSAUnitID FROM tblMast_SSAUnitMaster where SSAUnitid='" + Convert.ToInt32(cmbUnit.SelectedValue) + "'");
CSave.Add("SSAUnitID", cmbUnit.SelectedValue.ToString(), false, true, "");
//CSave.Add("PensionThrough", cmbPaythrough.Text,false,true,"");
//Contact Details
CSave.Add("ResidencePhone", txtPensionerContact.Text, false, true, "");
CSave.Add("MobileNo", txtPensionerMobileNo.Text, false, true, "");
CSave.Add("EmailID", txtPensionerEmail.Text, false, true, "");
CSave.Add("ContactAddress", rtbPensionerAddress.Text, false, true, "");
//Other Details
if (cmbMilitaryPension.Text == "Yes")
{
CSave.Add("MilitaryPensioner","True",false, true, "");
}
else
{
CSave.Add("MilitaryPensioner", "False", false, true, "");
}
if (chkCeased.CheckState == CheckState.Checked)
{
CSave.Add("Ceased", chkCeased.Checked.ToString(), false, true, "");
CSave.Add("CeasedDate", udtCeaseDt.Value.ToString(),false,true,"");
}
else
{
CSave.Add("Ceased", chkCeased.Checked.ToString(), false, true, "");
}
//-------
PensionType = "Superannuation Pension";
CSave.Add("PensionType", PensionType,false,true,"");
CSave.Add("PreparedBy", StrPreparedBy,false,true,"");
CSave.Add("ModifiedBy", StrModifiedBy,false,true,"");
lblStatus.Text = "UnAuthorised";
if (StrAuthorisedBy != null && mfnEnable_Authorise())
{
if (MessageBox.Show("Do you want to Authorise this document?", "AUTHORISE", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) {
CSave.Add("VerifiedBy", StrAuthorisedBy, false, true,"");
boolVerified = true;
lblStatus.Text = "Authorised";
}
}
if (mlngPID == 0)
{
lngPID = CSave.InsertRecord(GV.GetConnection(),"");
mlngPID = lngPID;
}
else
{
lngPID = CSave.UpdateRecord("PensionerID=" + mlngPID, GV.GetConnection(),"");
}
CSave = null;
if (lngPID > 0)
{
this.Tag = true;
functionReturnValue = true;
btnPrint.Enabled = true;
}
else
{
this.Tag = false;
functionReturnValue = false;
}
}
catch (Exception ex)
{
GV.gprocErrorHandler(ex.Source, ex.Message, "mfnSaveForm", this.Name);
}
return functionReturnValue;
}
step 2:
save:
private void btnSave_Click(object sender, EventArgs e)
{
try
{
string StrMsg = null;
StrMsg = mfnValidateSave();
//if (string.IsNullOrEmpty(StrMsg))
//{
// //MessageBox.Show(StrMsg, "REPEATED VALUE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
// //Exit Sub
//}
boolSave = true;
GblDeclarations.gcnERMS = GV.GetConnection();
GblDeclarations.gblSQLTrans = GblDeclarations.gcnERMS.BeginTransaction();
GblDeclarations.gblTransBegin = true;
if (mfnSaveForm() == true)
{
//GblDeclarations.gblSQLTrans.IsolationLevel;
GblDeclarations.gblSQLTrans.Commit();
MessageBox.Show("Records Updated Successfully", "RECORD SAVED", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnSave.Enabled = false;
GblDeclarations.gblTransBegin = false;
}
else
{
boolSave = false;
try
{
if (GblDeclarations.gblTransBegin)
{
GblDeclarations.gblSQLTrans.Rollback();
GblDeclarations.gblTransBegin = false;
}
}
catch (Exception extrans)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail.
GV.gprocErrorHandler(extrans.Source, extrans.Message, "btnSave_ClickTry", this.Name);
}
}
}
catch (Exception ex)
{
GV.gprocErrorHandler(ex.Source, ex.Message, "btnSave_Click", this.Name);
}
}
Plz help me out
Zoran HorvatPosted Jul 8, 2011, 6:41 AM
Try to discover program flow which leads to such result. Note that transactions may sometimes get terminated implicitly, e.g. due to some using block, or result of an exception where it's rolled back in the catch block, etc.
Zoran