1.11M Members

Input string was not in a correct format. C#?

 
0
 
int sel = int.Parse(0+dataGridView2.CurrentRow.Cells["invoice_ID"].Value.ToString());

string sql = "DELETE FROM invoices WHERE invoice_id="+ sel +"";
DBConnection myCon = new DBConnection(); 

myCon.EditValues(sql); 

//mycon.EditeValues(sql) has all the sqldata reader,executenonQuery cmd

the error's arrow points out to the first line which is "int sel............".

 
0
 

Your 0 is an integer not a string, which you are trying to concatenate to and send to a method which accepts String. You just need to put quotes around the 0 :)

 
0
 

Additionally, please use SQL Parameters

 
0
 

As Ketsuekiame said if you're invoice is alph-numeric (letters and numbers) you can't parse it into an int.

Try taking the int.parse and .ToString() off the first line and adding (int)before the name dataGridView2 (that will also only work if the cell doesn't contain letters but it means you're not converting it to a string before trying to store it in an int).

 
0
 

I tried

with "0", 0, and without 0 or "0"

 
0
 

Perhaps try:

if(!int.TryParse("0"+dataGridView2.CurrentRow.Cells["invoice_ID"].Value.ToString(),out int sel))
    MessageBox("0"+dataGridView2.CurrentRow.Cells["invoice_ID"].Value.ToString());

This way you can see exactly why the string is not parsing right. If there is anything in the string that isn't a digit, a comma, or a period then it will fail.

 
0
 

I just convereted int to string. that is it. >string< sel fixed it. I wonder why :O. Thanks!!

 
0
 

probably cause sql is declared as a string.

 
0
 

LOL When in doubt, convert to a string.

To all my more experience member, think about how many times this has fixed an issue (at least this seems true so many times for me)

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article