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