Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DataSet ds = _objTC.GetEmployeeLevelID(_objSession.userid, _objSession.schoolAcaID);
            int LevelID = Convert.ToInt32(ds.Tables[0].Rows[0]["LevelID"].ToString());
            ViewState["LevelID"] = LevelID;


What I have tried:

how to solve it and tll immediately
Posted
Updated 28-Feb-22 19:57pm

You can't "solve it" - the value returned from the table row and column reference is not a number - and since it comes from a database (I assume) that means two things:

1) The data in your db is not correct.
2) You DB design is faulty.

If that columns should always contain a number, then make it a number field: INT is good. When you take "the easy way out" and define all columns as text, it makes INSERTs easy, but will at some point mean that the data itself will become corrupt - not a number. That may not be spotted for days, months, or even years - but when it is it will be an nasty job to fix!

Always hold numeric data in numeric fields: INT, FLOAT, DATE, and so on: VARCHAR and NVARCHAR columns are for text based data only!

Then all you have to do is cast the field to an integer to use it:
int LevelID = (int) (ds.Tables[0].Rows[0]["LevelID"])
Easier to read, easier to type, and your whole system becomes more reliable.

So look at your data, fix it, and change your DB design! Then your problem will go away and can't come back!
 
Share this answer
 
Don't use Convert.ToInt32 to try and convert a string to an integer because it will throw an exception if the string cannot be converted, you should use Int32.TryParse method instead.

Because Int32.TryParse returns a true result you can tell the number was successfully parsed, if it returns false then you know that the field contains a value that could not be parsed and that will allow you to take the appropriate action.

int LevelID;

bool success = int.TryParse(ds.Tables[0].Rows[0]["LevelID"], out LevelID);

if (success)
{
   // Successfully parsed
}
else
{
   // Parsing failed
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900