EDN Admin
Well-known member
Dear All,
Currently I have an application with 2 grids that is one gridSales and gridPayment. So based on the list of item in both of this grid I generate the query and insert them accordingly. So I have put all the sql statements under one 1 transaction. Currently I am using my own method based on a variable. Based on the rollbackBoolean variable if any of the queries value is equal =1 then whole transaction will be roll back.But some article says the "using" statement is much efficient but my problem I dont know how to convert to it from my code ? Any idea please ? Below is the snippet of my code.
<font color="#0000ff" size=2>
int</font><font size=2> rollbackBoolean = 0; </font>
<font color="#008080" size=2>
MySqlTransaction</font><font size=2> transactionLocal = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#008080" size=2>MySqlConnection</font><font size=2> connectionLocal = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#008080" size=2>transactionConnectionLocal1</font><font size=2> callTransactionConnectionLocal1 = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#0000ff" size=2>try </font><font size=2>
{
callTransactionConnectionLocal1 = </font><font color="#0000ff" size=2>new</font> <font color="#008080" size=2>transactionConnectionLocal1</font><font size=2>();
connectionLocal = callTransactionConnectionLocal1.localConnection1;
connectionLocal.Open();
transactionLocal = connectionLocal.BeginTransaction();
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error From Database Connection (Local Server Is Down) "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (System.Net.Sockets.</font><font color="#008080" size=2>SocketException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error Sockets From Database Connection (Local Server Is Down) "</font><font size=2> + ex.Message);
}
<font color="#0000ff" size=2>
for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> j = 0; j < gridSales.RowCount; j++)
{
<font color="#008080" size=2>String</font><font size=2> myInsertQuery1 = </font><font color="#800000" size=2>"Insert into tblReceiptDetails "</font><font size=2> +
<blockquote dir=ltr style="margin-right:0px
</font><font color="#800000" size=2>"Set receiptID="</font><font size=2> + receiptID + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>"receiptDetailsID = "</font><font size=2> + receiptDetailsID + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"productID= "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToInt32(</font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[0, j].Value.ToString()) + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"productQuantity = "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToInt16(</font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[16, j].Value.ToString()) + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"sellingPrice = "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToDouble(</font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[18, j].Value.ToString()) + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"profit = "</font><font size=2> + </font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[5, j].Value.ToString() + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>"stockStatus ="</font><font size=2> + stockStatus + </font><font color="#800000" size=2>""</font><font size=2>;
</font><font color="#008000" size=2>// MessageBox.Show("myInsertQuery2 : "+myInsertQuery2);
</font><font size=2>
</font><font color="#008080" size=2>MySqlCommand</font><font size=2> myCommandLocal1 = </font><font color="#0000ff" size=2>new</font> <font color="#008080" size=2>MySqlCommand</font><font size=2>(myInsertQuery1);
myCommandLocal1.Connection = connectionLocal;
myCommandLocal1.Transaction = transactionLocal;
</font><font color="#0000ff" size=2>try
</font><font size=2>
{
myCommandLocal1.ExecuteNonQuery();
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error From myCommandLocal1 myInsertQuery1 "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (System.Net.Sockets.</font><font color="#008080" size=2>SocketException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error Sockets From myCommandLocal1 myInsertQuery1 "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>finally
</font><font size=2>
{
myCommandLocal1.Dispose(); </blockquote>
<p dir=ltr style="margin-right:0px }</font>
}
<font color="#0000ff" size=2>
for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> p = 0; p < gridPayment.RowCount; p++)
{ </font><font size=2>
<blockquote dir=ltr style="margin-right:0px
<font color="#008080 String</font> myInsertQuery2 = </font><font color="#800000" size=2>"Insert into tblPaymentDetails "</font><font size=2> +
</font><font color="#800000" size=2>"Set paymentDetailsID="</font><font size=2> + paymentDetailsID + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>receiptID = "</font><font size=2> + receiptID + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>"paymentTypeID= 1, "</font><font size=2> +
</font><font color="#800000" size=2>"amount = "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToDouble(</font><font color="#0000ff" size=2>this</font><font size=2>.gridPayment[11, p].Value.ToString()); </blockquote></font><font color="#008080" size=2>
<blockquote dir=ltr style="margin-right:0px
MySqlCommand</font><font size=2> myCommandLocal2 = </font><font color="#0000ff" size=2>new</font> <font color="#008080" size=2>MySqlCommand</font><font size=2>(myInsertQuery2);
myCommandLocal2.Connection = connectionLocal;
myCommandLocal2.Transaction = transactionLocal;
</font><font color="#0000ff" size=2>try </font><font size=2>
{
myCommandLocal2.ExecuteNonQuery();
} </blockquote>
<blockquote dir=ltr style="margin-right:0px
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error From Payment Details myCommandLocal2 myInsertQuery2 "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (System.Net.Sockets.</font><font color="#008080" size=2>SocketException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error Sockets From Payment Details <font color="#800000 myCommandLocal2 myInsertQuery2</font> "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>finally </font><font size=2>
{
myCommandLocal2.Dispose();
} </blockquote></font>}
<font color="#0000ff" size=2>
if</font><font size=2> (rollbackBoolean == 1)
{
transactionLocal.Rollback(); </font><font size=2>
}
</font><font color="#0000ff" size=2>else </font><font size=2>
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#0000ff" size=2>try </font><font size=2>
{
transactionLocal.Commit(); </font><font size=2></font><font size=2>
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
</font><font color="#0000ff" size=2>try </font><font size=2>
{
transactionLocal.Rollback();</font><font size=2>
} </blockquote></font><font size=2>
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#008080" size=2>MySqlException</font><font size=2> ex1)
{
</font><font color="#0000ff" size=2>if</font><font size=2> (transactionLocal.Connection != </font><font color="#0000ff" size=2>null</font><font size=2>)
{
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"An exception of type "</font><font size=2> + ex1.GetType() +
</font><font color="#800000" size=2>" was encountered while attempting to roll back the transaction."</font><font size=2>);
}
}
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"An exception of type "</font><font size=2> + ex.GetType() +
</font><font color="#800000" size=2>" was encountered while inserting the data."</font><font size=2>);
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Neither record was written to database."</font><font size=2>);
}
</font><font color="#0000ff" size=2>finally </font><font size=2>
{
connectionLocal.Close(); </font><font size=2>
}
} </font></font></font>
View the full article
Currently I have an application with 2 grids that is one gridSales and gridPayment. So based on the list of item in both of this grid I generate the query and insert them accordingly. So I have put all the sql statements under one 1 transaction. Currently I am using my own method based on a variable. Based on the rollbackBoolean variable if any of the queries value is equal =1 then whole transaction will be roll back.But some article says the "using" statement is much efficient but my problem I dont know how to convert to it from my code ? Any idea please ? Below is the snippet of my code.
<font color="#0000ff" size=2>
int</font><font size=2> rollbackBoolean = 0; </font>
<font color="#008080" size=2>
MySqlTransaction</font><font size=2> transactionLocal = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#008080" size=2>MySqlConnection</font><font size=2> connectionLocal = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#008080" size=2>transactionConnectionLocal1</font><font size=2> callTransactionConnectionLocal1 = </font><font color="#0000ff" size=2>null</font><font size=2>;
</font><font color="#0000ff" size=2>try </font><font size=2>
{
callTransactionConnectionLocal1 = </font><font color="#0000ff" size=2>new</font> <font color="#008080" size=2>transactionConnectionLocal1</font><font size=2>();
connectionLocal = callTransactionConnectionLocal1.localConnection1;
connectionLocal.Open();
transactionLocal = connectionLocal.BeginTransaction();
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error From Database Connection (Local Server Is Down) "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (System.Net.Sockets.</font><font color="#008080" size=2>SocketException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error Sockets From Database Connection (Local Server Is Down) "</font><font size=2> + ex.Message);
}
<font color="#0000ff" size=2>
for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> j = 0; j < gridSales.RowCount; j++)
{
<font color="#008080" size=2>String</font><font size=2> myInsertQuery1 = </font><font color="#800000" size=2>"Insert into tblReceiptDetails "</font><font size=2> +
<blockquote dir=ltr style="margin-right:0px
</font><font color="#800000" size=2>"Set receiptID="</font><font size=2> + receiptID + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>"receiptDetailsID = "</font><font size=2> + receiptDetailsID + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"productID= "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToInt32(</font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[0, j].Value.ToString()) + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"productQuantity = "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToInt16(</font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[16, j].Value.ToString()) + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"sellingPrice = "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToDouble(</font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[18, j].Value.ToString()) + </font><font color="#800000" size=2>", "</font><font size=2> + </font><font size=2>
</font><font color="#800000" size=2>"profit = "</font><font size=2> + </font><font color="#0000ff" size=2>this</font><font size=2>.gridSales[5, j].Value.ToString() + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>"stockStatus ="</font><font size=2> + stockStatus + </font><font color="#800000" size=2>""</font><font size=2>;
</font><font color="#008000" size=2>// MessageBox.Show("myInsertQuery2 : "+myInsertQuery2);
</font><font size=2>
</font><font color="#008080" size=2>MySqlCommand</font><font size=2> myCommandLocal1 = </font><font color="#0000ff" size=2>new</font> <font color="#008080" size=2>MySqlCommand</font><font size=2>(myInsertQuery1);
myCommandLocal1.Connection = connectionLocal;
myCommandLocal1.Transaction = transactionLocal;
</font><font color="#0000ff" size=2>try
</font><font size=2>
{
myCommandLocal1.ExecuteNonQuery();
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error From myCommandLocal1 myInsertQuery1 "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (System.Net.Sockets.</font><font color="#008080" size=2>SocketException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error Sockets From myCommandLocal1 myInsertQuery1 "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>finally
</font><font size=2>
{
myCommandLocal1.Dispose(); </blockquote>
<p dir=ltr style="margin-right:0px }</font>
}
<font color="#0000ff" size=2>
for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> p = 0; p < gridPayment.RowCount; p++)
{ </font><font size=2>
<blockquote dir=ltr style="margin-right:0px
<font color="#008080 String</font> myInsertQuery2 = </font><font color="#800000" size=2>"Insert into tblPaymentDetails "</font><font size=2> +
</font><font color="#800000" size=2>"Set paymentDetailsID="</font><font size=2> + paymentDetailsID + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>receiptID = "</font><font size=2> + receiptID + </font><font color="#800000" size=2>", "</font><font size=2> +
</font><font color="#800000" size=2>"paymentTypeID= 1, "</font><font size=2> +
</font><font color="#800000" size=2>"amount = "</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToDouble(</font><font color="#0000ff" size=2>this</font><font size=2>.gridPayment[11, p].Value.ToString()); </blockquote></font><font color="#008080" size=2>
<blockquote dir=ltr style="margin-right:0px
MySqlCommand</font><font size=2> myCommandLocal2 = </font><font color="#0000ff" size=2>new</font> <font color="#008080" size=2>MySqlCommand</font><font size=2>(myInsertQuery2);
myCommandLocal2.Connection = connectionLocal;
myCommandLocal2.Transaction = transactionLocal;
</font><font color="#0000ff" size=2>try </font><font size=2>
{
myCommandLocal2.ExecuteNonQuery();
} </blockquote>
<blockquote dir=ltr style="margin-right:0px
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error From Payment Details myCommandLocal2 myInsertQuery2 "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (System.Net.Sockets.</font><font color="#008080" size=2>SocketException</font><font size=2> ex)
{
rollbackBoolean = 1;
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Error Sockets From Payment Details <font color="#800000 myCommandLocal2 myInsertQuery2</font> "</font><font size=2> + ex.Message);
}
</font><font color="#0000ff" size=2>finally </font><font size=2>
{
myCommandLocal2.Dispose();
} </blockquote></font>}
<font color="#0000ff" size=2>
if</font><font size=2> (rollbackBoolean == 1)
{
transactionLocal.Rollback(); </font><font size=2>
}
</font><font color="#0000ff" size=2>else </font><font size=2>
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#0000ff" size=2>try </font><font size=2>
{
transactionLocal.Commit(); </font><font size=2></font><font size=2>
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (MySql.Data.MySqlClient.</font><font color="#008080" size=2>MySqlException</font><font size=2> ex)
{
</font><font color="#0000ff" size=2>try </font><font size=2>
{
transactionLocal.Rollback();</font><font size=2>
} </blockquote></font><font size=2>
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#008080" size=2>MySqlException</font><font size=2> ex1)
{
</font><font color="#0000ff" size=2>if</font><font size=2> (transactionLocal.Connection != </font><font color="#0000ff" size=2>null</font><font size=2>)
{
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"An exception of type "</font><font size=2> + ex1.GetType() +
</font><font color="#800000" size=2>" was encountered while attempting to roll back the transaction."</font><font size=2>);
}
}
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"An exception of type "</font><font size=2> + ex.GetType() +
</font><font color="#800000" size=2>" was encountered while inserting the data."</font><font size=2>);
</font><font color="#008080" size=2>MessageBox</font><font size=2>.Show(</font><font color="#800000" size=2>"Neither record was written to database."</font><font size=2>);
}
</font><font color="#0000ff" size=2>finally </font><font size=2>
{
connectionLocal.Close(); </font><font size=2>
}
} </font></font></font>
View the full article