Question

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.

Was it helpful?

Solution 3

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();

OTHER TIPS

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.

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

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

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);

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top