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.