Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello Every One,

I am working in Window application in asp.net. I have a GUI in which user enter a product name and quantity in text boxes. On Add button click i am adding a new row in Datagridview and set the value of productname and quantity in datagridview columns. I am not inserting record in Database and I am only save record in Datatable as well add record in Datagridview.

Problem is that when I select a last row from datagridview and press delete button from keyboard then it generate an error
SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Following is my code snippet.. Please you can check where i am doing wrong
C#
static public DataTable gdt;
       private void btnAdd_Click(object sender, EventArgs e)
       {
           try
           {
               if (txtItemCode.Text.Trim() == "")
               {
                   MessageBox.Show("Enter Item Code");
                   txtItemCode.Focus();
                   return;
               }
               if (txtQty.Text.Trim() == "")
               {
                   MessageBox.Show("Enter Qty");
                   txtQty.Focus();
                   return;
               }
               if (Convert.ToInt32(txtQty.Text.Trim()) <= 0)
               {
                   MessageBox.Show("Qty must be greater than 0");
                   txtQty.Focus();
                   return;
               }

               if (btnAdd.Text == "ADD")
               {

                   DataRow[] dr = gdt.Select("Item_Code = '" + txtItemCode.Text.Trim() + "'");

                   if (dr.Length > 0)
                   {
                       MessageBox.Show("Item Code Already Exist.");
                       txtItemCode.Text = "";
                       txtItemCode.Focus();
                       return;
                   }

                   tblItemMasterBLL oItem = new tblItemMasterBLL();
                   int ItemID = 0;
                   DataTable dt = new DataTable();
                   dt = oItem.getItemDetailByItemCode(txtItemCode.Text.Trim());
                   if (dt.Rows.Count > 0)
                   {
                       ItemID = Convert.ToInt32(dt.Rows[0]["Item_ID"]);


                       gdt.Rows.Add();

                       gdt.Rows[gdt.Rows.Count - 1]["Item_Code"] = txtItemCode.Text.Trim();
                       gdt.Rows[gdt.Rows.Count - 1]["Item_ID"] = ItemID;
                       gdt.Rows[gdt.Rows.Count - 1]["Qty"] = txtQty.Text.Trim();

                       gdt.Rows[gdt.Rows.Count - 1]["Article_Desc"] = Convert.ToString(dt.Rows[0]["Article_Desc"]);
                       gdt.Rows[gdt.Rows.Count - 1]["Color_Desc"] = Convert.ToString(dt.Rows[0]["Color_Desc"]);
                       gdt.Rows[gdt.Rows.Count - 1]["Size_Desc"] = Convert.ToString(dt.Rows[0]["Size_Desc"]);
                       gdt.Rows[gdt.Rows.Count - 1]["MRP"] = Convert.ToString(dt.Rows[0]["MRP"]);


                       dgv_Items.DataSource = null;
                       dgv_Items.DataSource = gdt;




                   }
                   else
                   {
                       MessageBox.Show("Invalid Item Code");
                   }
                   txtItemCode.Text = "";
                   txtQty.Text = "";
               }
               else if (btnAdd.Text == "UPDATE")
               {
                   if (gdt.Rows.Count > 0)
                   {
                       gdt.Rows[Convert.ToInt32(lblhdnRowIndex.Text)]["Qty"] = txtQty.Text.Trim();
                       dgv_Items.Rows[Convert.ToInt32(lblhdnRowIndex.Text)].Cells["Qty"].Value = txtQty.Text.Trim();
                   }
                   txtItemCode.ReadOnly = false;
                   txtItemCode.Text = "";
                   txtQty.Text = "";
                   lblhdnItemID.Text = "";
                   lblhdnItemCode.Text = "";
                   lblhdnQty.Text = "";
                   btnAdd.Text = "ADD";
                   lblhdnRowIndex.Text = "";
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
       {
           try
           {
               if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion",
                   MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
               {
                   ScrollPosition = 0;
                   ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
                   int iIndex = dgv_Items.CurrentRow.Index;

                       gdt.Rows.RemoveAt(iIndex);

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

       private void dgv_Items_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
       {
           try
           {
               dgv_Items.DataSource = null;
               dgv_Items.DataSource = gdt;
               dgv_Items.Rows[dgv_Items.Rows.Count - 1].Visible = false;

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



Please help me give any solution...
Posted
Updated 24-Jun-13 20:48pm
v2
Comments
Prasad_Kulkarni 25-Jun-13 2:25am    
Can you post your code snippets?
coolnavjot31 25-Jun-13 2:52am    
I have post my code u can check
Sanjay K. Gupta 25-Jun-13 2:41am    
You cannot have Datagridview in ASP.NET

@coolnavjot, Dear you can not windows application in ASP.Net... ;)

refer below link..
Performing Insert, Update and Delete Operations using DetailsView Control in ASP.NET[^]
 
Share this answer
 
You are trying to access a member of an collection where this member does not exists.

For e.g. consider an array has a size of 5. You are trying to access the seventh item arr[6].
This will throw an error.

Try stepping through your code and you will be able to figure out which collection is throwing this error.
 
Share this answer
 
Hi Guys, thanks all of u participate to solve my problem. Actually this is index problem.
I have find out the solution In which I have done changes in UserDeletingRow event of Datagridview. I have added a new line in UserDeletingRow eventhich is in bold font. Now my code is working fine.

Before:

C#
private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            try
            {
                if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    ScrollPosition = 0;
                    ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
                    int iIndex = dgv_Items.CurrentRow.Index;
                   
                        gdt.Rows.RemoveAt(iIndex);
                       
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

After
C#
private void dgv_Items_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
       {
           try
           {
               if (MessageBox.Show("Do you want to delete the current row?", "Confirm deletion",
                   MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
               {
                   ScrollPosition = 0;
                   ScrollPosition = dgv_Items.FirstDisplayedScrollingRowIndex;
                   int iIndex = dgv_Items.CurrentRow.Index;

                       DataRow dr = gdt.Rows[iIndex];
                       gdt.Rows.RemoveAt(iIndex);
                       gdt.Rows.InsertAt(dr, iIndex);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
 
Share this answer
 
v4

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