Use of String Builder in C#

Lakhwinder.ghuman
Posted by Lakhwinder.ghuman under C# category on | Points: 40 | Views : 3425
#region Submit-Record-using-string-builder
if (con.State == ConnectionState.Closed)
con.Open();
SqlTransaction trans;
trans = con.BeginTransaction();
try
{

StringBuilder sb = new StringBuilder();
foreach (GridItem item in RadGridItems.MasterTableView.Items)
{
sb.Append("Update RequisitionSlipDetail Set IsApproved='");
sb.Append((item.FindControl("chkapprovequantity") as CheckBox).Checked);
sb.Append("', ApprovedQuantity=");
sb.Append((item.FindControl("txtapprovedquantity") as TextBox).Text);
sb.Append(" where SerialNo=");
sb.Append((item.FindControl("SerialNoLabel") as Label).Text);
sb.Append(" and ItemCode='");
sb.Append((item.FindControl("ItemCodeLabel") as Label).Text);
sb.Append("'");
}
cmd.Connection = con;
cmd.CommandText = sb.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
trans.Commit();
cmd.Dispose();
con.Close();
}
catch(Exception ex)
{
trans.Rollback();
lblmsg.Text = ex.Message.ToString();
lblmsg.Visible = true;

}
finally
{
trans.Dispose();
con.Close();
}
#endregion

Comments or Responses

Posted by: Lakn2 on: 6/29/2011 Level:Starter | Status: [Member] | Points: 10
hi good one
but before writing code give some explanation where can we use and what are the benefits ,then users can easily understands more clearly.

Login to post response