Minh

Minh

  • NA
  • 47
  • 134.4k

Sqltransaction error in c#

Jun 10 2010 10:45 PM
Hi friends , Can you show me what is wrong with my code , Compile codes below will lead to error "Can not connect database " , otherwise , when i remove codes related to sqltransaction , everything will be ok . Please help me for this . Thanks .
 sqlconnection cnnt = new sqlconnection();
 cnnt = ConnectDatabase.CreateConnection();                
                if (cnnt == null)
                    MessageBox.Show(Class.ThongBao.CanNotConnect());
                else
                {
                    SqlTransaction trx = cnnt.BeginTransaction();
                    try
                    {
                        SqlCommand cmd = new SqlCommand();
                        cmd = new SqlCommand(sl, cnnt);
                        cmd.ExecuteNonQuery();
                        for (int i = 0; i < _gridview.RowCount; i++)
                            if (_gridview["_txt_vnd", i].Value.ToString() != "0")
                            {
                                _Diengiai = _gridview["_txt_diengiai", i].Value.ToString();
                                _Dvtt = _gridview["_txt_dvtt", i].Value.ToString();
                                _tkn = _gridview["_txt_tkn", i].Value.ToString();
                                _tkc = _gridview["_txt_tkc", i].Value.ToString();
                                _st = _gridview["_txt_st", i].Value.ToString();
                                _vnd = _gridview["_txt_vnd", i].Value.ToString();
                                _Madt = _gridview["_txt_madt", i].Value.ToString();
                                sl2 = "Insert into tb_Sphieuketoan(sophieu,diengiai,ten_dvtt,tkn,tkc,st_NT,st_VND,madt)" +
                                " Values('" + _Sophieu + "','" + _Diengiai + "','" + _Dvtt + "','" + _tkn + "','" + _tkc + "','" + _st + "','" + _vnd + "','" + _Madt + "')";
                                cmd = new SqlCommand(sl2, cnnt);
                                cmd.ExecuteNonQuery();
                            }
                          trx.Commit();
                        Save();
                        MessageBox.Show("Hoàn thành phi?u chi", "Thông báo");
                    }
                    catch
                    {
                         trx.Rollback();
                        MessageBox.Show(Class.ThongBao.CanNotConnect());
                    }
                }

Answers (3)