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
VulpesPosted Dec 16, 2013, 6:44 AM
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.
Abhay ShankerPosted Dec 16, 2013, 3:30 AM
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.
Jignesh TrivediPosted Dec 16, 2013, 3:12 AM
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...
ta muPosted Dec 16, 2013, 3:06 AM
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 TrivediPosted Dec 16, 2013, 3:01 AM
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.