basim salih

basim salih

  • NA
  • 84
  • 8.9k

Please what's causing and solving of this error message

Oct 26 2017 7:39 PM
Please what's causing and solving of this error message in C# (Index was out of range Must be non-negative and less thanthe size of the collection
 
Parameter name:index)?
 
I have a procedure in C# that fill data table in datagridview control… I update a record in d data and after that calling a procedure (ldata() ), when the compiler arrive to dt.clear() the error message above is appeared.
 
as below :
 
.
.
……….(insertbrn_click event)
cmd = new SqlCommand("insert into finance_dep.acount_type (id_cnt, name_cnt, notes_col) values ('" + txt1.Text + "' , '" + txt2.Text + "' , '" + txt3.Text + "') ", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
ldata();
……
 
When I insert new record is work correctly… but when perform update as below and call a procedure (ldata() )
 
When the compiler arrive to command: dt.clear() an error message was appeared. Since after error message appear the table is updated … why this error appear and solve it?
 
(update_click event)
cmd = new SqlCommand("select * from finance_dep.acount_type", cn);
cn.Open();
dr = cmd.ExecuteReader();
dt.Clear(); // error here
dt.Load(dr);
dgv1.DataSource = dt;
dgv1.Columns[0].HeaderText = "??? ??????";
dgv1.Columns[1].HeaderText = "??? ??????";
dgv1.Columns[2].HeaderText = "?????????";
cn.Close();
void ldata()
{
try
{
//int i = 0;
cmd = new SqlCommand("select * from finance_dep.acount_type", cn);
cn.Open();
dr = cmd.ExecuteReader();
dt.Clear(); // error here
dt.Load(dr);
dgv1.DataSource = dt;
dgv1.Columns[0].HeaderText = "??? ??????";
dgv1.Columns[1].HeaderText = "??? ??????";
dgv1.Columns[2].HeaderText = "?????????";
cn.Close();

Answers (2)