delcmd.CommandText = "DELETE FROM add_investor WHERE investor_cnic=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
my.Open();
delcmd.Connection = my;
delcmd.ExecuteNonQuery();
my.Close();
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
my.Close();
Conversion failed when converting the nvarchar value '34101-1212121-1' to data type int.
whats wrong

Jignesh TrivediRank #16 · 50.3K reputationPosted Dec 15, 2013, 10:01 PM EST
it might be possible investor_cnic is int field and dataGridView1.SelectedRows[0].Cells[0].Value.ToString() return string value like '34101-1212121-1' , so this error occurred....
so check whether correct data you are pass in where condition..
hope this will help you.
ta mu198 reputationPosted Dec 15, 2013, 10:06 PM EST
SqlCommand delcmd = new SqlCommand();
delcmd.CommandText = "DELETE FROM add_investor WHERE investor_cnic='@Investor'";
delcmd.Parameters.Add("@Investor", System.Data.SqlDbType.NChar);
delcmd.Parameters["@Investor"].Value = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
my.Open();
delcmd.Connection = my;
delcmd.ExecuteNonQuery();
my.Close();
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
my.Close();
Jignesh TrivediRank #16 · 50.3K reputationPosted Dec 15, 2013, 10:12 PM EST
I think there is no need to put single quote before and after the parameter name...
try...
delcmd.CommandText = "DELETE FROM add_investor WHERE investor_cnic=@Investor";
hope this will help you...
Abhay ShankerRank #375 · 6.7K reputationPosted Dec 15, 2013, 10:30 PM EST
Try with also below modification:
1.Change Datatype of investor_cnic by nvarchar(SIZE).
2.Despite of
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
line
Re-Bind your gridview.
VulpesRank #55 · 29.2K reputationPosted Dec 16, 2013, 1:44 AM EST
However, even if you remove the hyphens from 34101-1212121-1, you still won't be able to convert it to the int type as the latter can't exceed 10 digits. It will be automatically converted to the decimal type instead.
So, if you do in fact want to insert this value into the database (and not just a part of it), you'll need to change the column type to bigint, decimal or varchar first.