Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Everytime I Clicked the Header name that index was out of range happen. I'm still a newbie

What I have tried:

try
            {
                string colNamee = dataGridView2.Columns[e.RowIndex].Name;
                if (colNamee == "Edit")
                {
                    FrmAddProduct frmadd = new FrmAddProduct(this);
                    frmadd.btnSave.Enabled = false;
                    frmadd.btnUpdate.Enabled = true;
                    frmadd.txtID.Text = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
                    frmadd.txtDescriprion.Text = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
                    frmadd.txtPrice.Text = dataGridView2.Rows[e.RowIndex].Cells[4].Value.ToString();
                    frmadd.txtQty.Text = dataGridView2.Rows[e.RowIndex].Cells[5].Value.ToString();
                    frmadd.cboBrand.Text = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
                    frmadd.cboCategory.Text = dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString();
                    frmadd.ShowDialog();
                    RefreshProduct();
                }
                else if (colNamee == "Delete")
                {
                    if (MessageBox.Show("Are you sure you want to delete this product record?", "Delete Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        con.Open();
                        cmd = new MySqlCommand("Delete from tblProduct where Pcode ='" + dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString() + "'", con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                        RefreshProduct();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
C#

Posted
Updated 17-Apr-20 0:43am
Comments
Maciej Los 17-Apr-20 6:34am    
Debug the programme to find out what value is passed by e.RowIndex when you click on header of column ;)
F-ES Sitecore 17-Apr-20 6:41am    
There are about 20 lines in your code that could generate that error. When you have an error always say what line it occurs on.
Richard Deeming 21-Apr-20 13:37pm    
cmd = new MySqlCommand("Delete from tblProduct where Pcode ='" + dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString() + "'", con);

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

1 solution

Try this:

C#
DataGridViewRow currentRow = dataGridView2.Rows[e.RowIndex];
if(currentRow==null) return;

//your code here...


or
C#
if(e.RowIndex==-1) return;

//your code here...
 
Share this answer
 
Comments
rayver bautista 17-Apr-20 7:36am    
maraming salamat po gumana na po siya, di ko po makakalimutan pag tulong nyo sakin. salamat po

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