Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to update my data in access bank , but i have this error:
Index was out of range. Must be non-negative and less than the size of the collection.
here is my code:
C#
var table = new DataTable();
            OleDbConnection coldb = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + bankaddrss);
            coldb.Open();
            OleDbCommand com = new OleDbCommand();
            com.Connection = coldb;
            com.CommandText = " UPDATE Test_Add SET[FirstName]=@param1,[LastName]=@param2,[Salary]=@param3 WHERE ID="+dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            com.Parameters.AddWithValue(@"param1", maskedTextBox_FirstName.Text);
            com.Parameters.AddWithValue(@"param2", maskedTextBoxLastName.Text);
            com.Parameters.AddWithValue(@"param3", maskedTextBox_Salary.Text);
            com.Parameters.AddWithValue(@"param4",DateTime.Now);
            com.CommandType = CommandType.Text;
            var DA = new OleDbDataAdapter(com);
            
            DA.Fill(table);
            dataGridView1.DataSource = table;
            coldb.Close();


where is the mistake?
Posted
Updated 26-Mar-13 19:29pm
v2
Comments
Kenneth Haugland 27-Mar-13 1:30am    
Seems like your datagridview is empty...
dataGridView1.SelectedRows[0].Cells[0].Value.ToString
PIEBALDconsult 27-Mar-13 1:40am    
Yeah, probably no data. But I wonder why you don't use a parameter for the ID. I'm guessing you tried to put it in param0 and found that it doesn't work that way -- what database are you using?
eli551 27-Mar-13 2:27am    
i use access db. i put in in param()? what do you mean of param()? what should i do?
PIEBALDconsult 27-Mar-13 11:08am    
Not param(), param0 -- but maybe you're a VB type who doesn't start lists with 0. :shrug:

C#
dataGridView1.SelectedRows[0].Cells[0].Value.ToString();


Either there is no selected rows (likely), or no cells.
 
Share this answer
 
Comments
eli551 27-Mar-13 2:22am    
thank Nelly you're right , but when i set cells for dataGridView i see the data for 2 times.
Nelly2k 27-Mar-13 2:58am    
you probably should turn off auto column generation, somewhere in the datagridview properties
use [e.RowIndex] instead oh [0]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900