Hi Guys,
I am getting index out of range error.My code looks like this

private void submit_Click(object sender, EventArgs e)
        {
            if (form.dataGridView2.SelectedRows.Count == -1) return; // column header clicked - do nothing    // get the row just clicked    
            DataRowView drv = form.dataGridView2.SelectedRows[0].DataBoundItem as DataRowView;

            if (drv != null)
            {
                //convert row to strongly typed row from dataset 
                DataRow row = drv.Row as DataRow;
                string myString;
                string mystring2;
                SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");

                SqlDataAdapter da = new SqlDataAdapter();
                SqlCommandBuilder cmdBuilder;

                DataSet ds = new DataSet();

                cn.Open();
                mystring2 = @"SELECT count(word) FROM wordTbl where word = ('" + (string)row["word"] + "')";
                SqlCommand myCmd = new SqlCommand(mystring2, cn);
                //myCmd.ExecuteNonQuery();
                object result = myCmd.ExecuteScalar();
                int result2 = (int)result;
                cn.Close();
                if (result2 == 1)
                {
                    MessageBox.Show(row["word"] + " already exist in your personal dictionary");
                }
                else
                {
                    //cata.Show();
                    cn.Open();

                    myString = @"INSERT INTO wordTbl(word,text,date)  Values('" + (string)row["word"] + "','" + form.richTextBox1.Text + "','" + DateTime.Now.ToString("yyyyMMdd hh:mm:ss:fff") + "')";
                    myString += @"insert into categoryTbl(word,category) Values('" + (string)row["word"] + "','" + comboBox1.SelectedIndex.ToString() + "')";
                    SqlCommand myCmd2 = new SqlCommand(myString, cn);
                    myCmd2.ExecuteNonQuery();

                    cn.Close();
                    MessageBox.Show(row["word"] + "is added to your personal dictionary");
                    form.richTextBox1.Text = "Word:" + row["word"];
                    //cata.Show();
                    
                }
            }

I am getting error at the line :- DataRowView drv = form.dataGridView2.SelectedRows[0].DataBoundItem as DataRowView;
Thanks

I don't know :) but do you see there's row selected to be edited? or when you leave the grid it unselect all selected rows to be changed?! I really don't know your scenario, but the debugger very helpful guy in such situations.

i am pretty sure i select the row but dont know if it deselect all the rows.is there a way i can have the row to be selected

This article has been dead for over six months. Start a new discussion instead.