1,105,585 Community Members

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

Member Avatar
silvercats
Posting Pro in Training
429 posts since Apr 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
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............".

Member Avatar
Ketsuekiame
Posting Virtuoso
1,676 posts since May 2010
Reputation Points: 793 [?]
Q&As Helped to Solve: 231 [?]
Skill Endorsements: 27 [?]
 
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 :)

Member Avatar
Ketsuekiame
Posting Virtuoso
1,676 posts since May 2010
Reputation Points: 793 [?]
Q&As Helped to Solve: 231 [?]
Skill Endorsements: 27 [?]
 
0
 

Additionally, please use SQL Parameters

Member Avatar
ChrisHunter
Practically a Master Poster
625 posts since Feb 2011
Reputation Points: 120 [?]
Q&As Helped to Solve: 69 [?]
Skill Endorsements: 15 [?]
Featured
 
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).

Member Avatar
silvercats
Posting Pro in Training
429 posts since Apr 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried

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

Member Avatar
tinstaafl
Postaholic
2,014 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 36 [?]
 
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.

Member Avatar
silvercats
Posting Pro in Training
429 posts since Apr 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
tinstaafl
Postaholic
2,014 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 36 [?]
 
0
 

probably cause sql is declared as a string.

Member Avatar
Ange1ofD4rkness
Posting Whiz
312 posts since May 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 9 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article