0

I am trying to execute a SQL select statement to retrieve my id, then increment the id. I can select from the database just fine. But when I try to convert the string to a int, I get the error of format exception was caught

Input string was not in a correct format.

Here is my code:

var sql = new SQL_Statements();
var string_sql_id ="";
var string_sql_select = "select * from is_inventory where id =(select max(id) from is_inventory)";

var sql_ds = sql.SelectFromDB(string_sql_select);

int DataIndex;

if (sql_ds.Tables["CurData"].Rows.Count > 0)
   for (DataIndex = 0; DataIndex <= sql_ds.Tables["CurData"].Rows.Count - 1; DataIndex++)
   {
       (sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString();
       string_sql_id ="CurData";
       var int_sql_id = Convert.ToInt32(string_sql_id);
       ++int_sql_id;
   }

On the second to last line of code is where it throws the exception. I have tried to change the convert to something else like double, but that didn't work either. Thanks in advance for the help.

3
  • You're using so many vars that I can't even tell what you're trying to do. Can you add some comments maybe? Commented May 22, 2013 at 15:00
  • You need to store the result of (sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString(); in a local variable Commented May 22, 2013 at 15:00
  • Prefacing all your variables with their type is really going to anger the next dev that reads your code. Commented May 22, 2013 at 15:09

5 Answers 5

3

For some reason you're fetching the value then discarding it, and then trying to convert the string CurData to an int.

(sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString();
string_sql_id ="CurData";
var int_sql_id = Convert.ToInt32(string_sql_id);

The value of string_sql_id is always just CurData then - how did you expect it to be anything else?

The value in the datatable should already be an integer, so you shouldn't need any parsing:

var id = (int) sql_ds.Tables["CurData"].Rows[DataIndex]["id"];

(You may find that it's a long instead of an int - it depends on the type of your field.)

You should avoid unnecessary string conversions where possible. In this case it's not clear why you're using a DataTable at all. You could just use a DataReader - and if you change you code to just select the max ID, you can just use ExecuteScalar to get a single value. Your current approach is very convoluted.

Sign up to request clarification or add additional context in comments.

1 Comment

to be perfectly honest, I like your answer better than mine :(. lol.
3
string_sql_id ="CurData";
var int_sql_id = Convert.ToInt32(string_sql_id);

You're trying co convert to int string "CurData"

1 Comment

Quite surprised this has more up votes than Jon's answer - especially when there is no further explanation on how to fix the issue.
1

in the line immediately before you're convert line, you set string_sql_id ="CurData";. "CurData" is not an integer, and attempting to convert it to one will not work.

try this instead;

string_sql_id = (sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString();

3 Comments

in that case, its highly likely that you are either retrieving the wrong piece of data, or retrieving it incorrectly. What, EXACTLY, is returned by this statement: (sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString()?
I replaced string_sql_id ="CurData"; with string_sql_id = (sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString();
It still looks like you're doing some redundant conversion here - is the "id" field of "CurData" actually stored as an int in the returned DataSet? If so, it should be able to be cast to an int. If "id" can ever return null, you'll need to check for DBNull first though.
1

You are trying to convert the string "CurData" to an integer which is what is throwing the exception.

 string_sql_id ="CurData";
 var int_sql_id = Convert.ToInt32(string_sql_id);

I assume you mean to assign string_sql_id to the Row ID:

 string_sql_id = sql_ds.Tables["CurData"].Rows[DataIndex]["id"]).ToString();

Then you can attempt to convert:

 var int_sql_id = Convert.ToInt32(string_sql_id);

Comments

0

You are trying convert primitive string type to Int. So it is something to do with your logic.

string_sql_id ="CurData";
var int_sql_id = Convert.ToInt32(string_sql_id);

Note: If you intend to work with Identity ( Incrementing field ) then I advise not to explicitly create incremental fields say Identity columns. Try to use database built in feature which generate it for you. Google it on how to defined AUTO INCREMENT Field in SQL or the db which you prefer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.