Click here to Skip to main content
15,887,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnmodi_Click(object sender, EventArgs e)
        {
            if (radGridView1.RowCount <= 0)
            {

                MessageBox.Show(" Select Record to Updae","" );
                return;
            }

            if (blUpdate == true)
            {
                btnmodi.Text = "&Update";
                blUpdate = false;


                txtCode.Text = radGridView1.SelectedCells[0].Value.ToString();
                txtcatagory.Text = radGridView1.SelectedCells[1].Value.ToString();



                btndele.Enabled = false;
                btnsav.Enabled = false;
                txtCode.Enabled = false;
                txtCode.Focus();
            }
            else
            {
                btnmodi.Text = "&Edit";
                blUpdate = true;

                {
                    SqlConnection cn = new SqlConnection(Program.myConnection);
                    string sql = "update student set code=@code,catagory=@catagory,  where code=" + radGridView1.SelectedCells[0].Value.ToString();

                    SqlCommand cmd = new SqlCommand(sql, cn);



                    cmd.Parameters.AddWithValue("@code", txtCode.Text.Trim());
                    cmd.Parameters.AddWithValue("@catagory", txtcatagory.Text.Trim());
              


                    try
                    {
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Data has been update successfully in database.", "Update Record", MessageBoxButtons.OK, MessageBoxIcon.Information);


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        cn.Close();
                        cmd.Dispose();

                        fillGrid();
                        btnsav.Enabled = true;
                        btndele.Enabled = true;
                        txtCode.Enabled = true;
                        txtCode.Focus();
                        

                    }
                }
            }
        }
Posted
Updated 5-Feb-15 1:52am
v2
Comments
CHill60 5-Feb-15 7:55am    
On what line?
Haris Baig 5-Feb-15 8:18am    
txtCode.Text = radGridView1.SelectedCells[0].Value.ToString();
CHill60 5-Feb-15 8:40am    
That would imply that no cells have been selected when you hit this code
Haris Baig 5-Feb-15 8:55am    
same error on
string sql = "update catagory set code=@code,catagory=@catagory where code=" + radGridView2.SelectedCells[0].Value.ToString();
CHill60 5-Feb-15 8:57am    
If you check with the debugger you'll probably find that radGridView2.SelectedCells is null. No cells are selected.
You need to check for that before trying to use them.

1 solution

Might be an index you are using that is out of bounds, e.g.:
C#
radGridView1.SelectedCells[1]

will throw that exception if there is no element at position 1 (second element in the array).

You can provide more information by debugging to see the exact line in which the exception is being thrown.

Cheers,
C
 
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