Click here to Skip to main content
12,250,675 members (47,677 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
index is out of range.must be non-negative and less than the size of the collection
parameter name:index


i have this error plz solve this error any one
Posted 5-Feb-12 19:08pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You are trying to access a member of an collection where this member does not exist.
For e.g. an array has a size of 3. You are trying to access the fifth item arr[4].
This will throw an error.

Try stepping through your code and you will be able to figure out which collection is throwing this error.
  Permalink  
Comments
rockpune 6-Feb-12 1:19am
   
private void Form1_Load(object sender, EventArgs e)
{
try
{
sqlCon.Open();

loaddata();


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void loaddata()
{
try
{
sqlAdapter = new SqlDataAdapter("SELECT *, 'Delete' AS [Delete] FROM Employees", sqlCon);
sqlCommand = new SqlCommandBuilder(sqlAdapter);

sqlAdapter.InsertCommand = sqlCommand.GetInsertCommand();
sqlAdapter.UpdateCommand = sqlCommand.GetUpdateCommand();
sqlAdapter.DeleteCommand = sqlCommand.GetDeleteCommand();

dataset = new DataSet();
sqlAdapter.Fill(dataset, "Employees");
dataGridView1.DataSource = null;
dataGridView1.DataSource = dataset.Tables["Employees"];

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
dataGridView1[6, i] = linkCell;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
try
{
int lastRow = dataGridView1.Rows.Count - 2;
DataGridViewRow nRow = dataGridView1.Rows[lastRow];
DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
dataGridView1[6, lastRow] = linkCell;
nRow.Cells["Delete"].Value = "Insert";
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//try
//{
if (e.ColumnIndex == 6)
{
string Task = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
if (Task == "Delete")
{
if (MessageBox.Show("Are you sure to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int rowIndex = e.RowIndex;
dataGridView1.Rows.RemoveAt(rowIndex);
dataset.Tables["Employees"].Rows[rowIndex].Delete();
sqlAdapter.Update(dataset, "Employees");
}
}
else if (Task == "Insert")
{
int row = dataGridView1.Rows.Count - 2;
DataRow dr = dataset.Tables["Employees"].NewRow();
dr["LastName"] = dataGridView1.Rows[row].Cells["LastName"].Value;
dr["FirstName"] = dataGridView1.Rows[row].Cells["FirstName"].Value;
dr["Title"] = dataGridView1.Rows[row].Cells["Title"].Value;
dr["HireDate"] = dataGridView1.Rows[row].Cells["HireDate"].Value;
dr["PostalCode"] = dataGridView1.Rows[row].Cells["PostalCode"].Value;

dataset.Tables["Employees"].Rows.Add(dr);
dataset.Tables["Employees"].Rows.RemoveAt(dataset.Tables["Employees"].Rows.Count - 1);
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 2);
dataGridView1.Rows[e.RowIndex].Cells[6].Value = "Delete";
sqlAdapter.Update(dataset, "Employees");
}
else if (Task == "Update")
{
int r = e.RowIndex;
dataset.Tables["Employees"].Rows[r]["LastName
That's Aragon 6-Feb-12 1:46am
   
Can you debug the source code and let us know that at which line you got this issue ?
Espen Harlinn 6-Feb-12 8:49am
   
5'ed!
Abhinav S 6-Feb-12 10:36am
   
Thank you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 6 Feb 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100