Minh

Minh

  • NA
  • 47
  • 133.6k

Can not update datasouce for datagridview

Jun 10 2010 11:18 AM
Hi friend , Please help me .
I try to update datasource for datagridview  but Errors founded as "Can not connect database" from my class  when the procedure is called at the second time despite that it is ok at the first time .

Below is my codes :
string table, sl,sl_child,dk;  // Table and Select statement to have data from database   
        DataSet ds = new DataSet();
        SqlDataAdapter data = new SqlDataAdapter();
        SqlConnection cnnt = new SqlConnection();
private void Update_(string sl_chid)
        {
            switch (FormToCall)
            {
                case "frm_chitienmat":
                    sl = "Select sophieu as [S? phi?u],ngaylap as [Ngày l?p],ngayghiso as [Ngày h?ch toán],noidung as [Di?n gi?i],nguoilap as [Ngu?i l?p],nguoinhan as [Ngu?i nh?n],maquy as [Ch? t?i qu?] from tb_Mtienmat";
                    table = "tb_Mtienmat";
                    break;
            }
            SqlConnection cnnt = ConnectDatabase.CreateConnection();
            if (cnnt == null)
                MessageBox.Show(Class.ThongBao.CanNotConnect());
            else
            {
                try
                {
                    table = "tb_Mtienmat";
                    data = new SqlDataAdapter();
                    ds = new DataSet();                    
                   // foreach (DataGridViewRow  row in _grid_master.Rows)
                     //   this._grid_master.Rows.Remove(row);
                    data = new SqlDataAdapter(sl, cnnt);
                    data.Fill(ds, table);                    
                    _grid_master.DataSource = ds;
                    _grid_master.DataMember = table ;
                    _grid_master.Columns[3].Width = 400;
                    table = "tb_Sphieuketoan";
                    data = new SqlDataAdapter(sl_chid, cnnt);
                    data.Fill(ds, table);
                    _grid_child.DataSource = ds;
                    _grid_child.DataMember = table ;
                    _grid_child.Columns[1].Width = 280;
                    _grid_master.Refresh();
                    _grid_child.Refresh();                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Class.ThongBao.CanNotConnect());
                }                
            }
        }


 private void frm_Grid_Data_Load(object sender, EventArgs e)
        {
           
            sl_child = "Select buttoan as [Bút toán],diengiai as [Di?n gi?i],mahang as [Mã hàng],tenhang as [Tên hàng],dvt as [ÐVT],ten_dvtt as [ÐVTT],dg_NT as [Ðon giá],dg_VND as [Ðon giá (VND)],tkn as [TK N?],tkc as [TK Có]," +
                     "st_NT as [Thành ti?n],st_VND as [Thành ti?n (VND)],phantram_ck as [% Chi?t kh?u] from tb_Sphieuketoan";
            Update_(sl_child);  

Answers (2)