Last post Feb 28, 2018 06:58 PM by ravl13
Feb 26, 2018 07:08 PM|ravl13|LINK
I have a SqlDataReader object:
SqlDataReaderEx sdrEx = new SqlDataReaderEx(mConnectionString);
and I created a query string, and it is able to read in string values fine from a SQL database, and I can assign those values fine to string variables. For example, I can take a "firstname" column field (which is varchar datatype), and assign it to a string variable like below:
string mStudentFirst;mStudentFirst = sdrEx.String("firstname");
However, if I try and work with a smallint value from a SQL database, I cannot get it to work. In the below code, the "Typeclass" column of the SQL database is a "smallint" data type:
int mTypeclass;mTypeclass = Int32.Parse(sdrEx.String("Typeclass"));
I've looked at the available methods of the sdrEx (Sql datareader) object in visual studio, and I don't see anything else that could help me. I tried using "long" and "toString", but those give immediate errors even before attempting to run the code. The
"sdrEx.String" code I have above does not throw any immediate errors before I run it, but upon actual running, I get this error in my web browser (only partial error is shown below):
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +14198648
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +177
Why does the above code with the Typeclass variable not work? sdrEx.String should be taking in the column as a string, and then I try to parse that string into an Int32, and assign it to the mTypeclass integer variable. What's the issue here? Potentially,
it could be that sdrEx.String may be expecting only varchar/char type column data, but I don't see any other SqlDataReader methods that I could use instead.
Can someone please assist me in correcting my problematic code?
Feb 27, 2018 03:05 AM|X.Daisy|LINK
SqlDataReaderEx seems a custom object. Please share the object code with us. Then we can understand your problem better.
Then, since you're trying to get data of each column from database, why don't you use SqlDataReader directly? I made a demo with this method, you can take it as a reference:
string queryString =
"SELECT ID, FirstName, Age FROM Students";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Case1801ConnectionString"].ToString()))
SqlCommand command =
new SqlCommand(queryString, connection);
SqlDataReader sdrEx = command.ExecuteReader();
// Call Read before accessing data.
List<int> Ages = new List<int>();
List<string> FirstNames = new List<string>();
// Call Close when done reading.
Feb 28, 2018 06:58 PM|ravl13|LINK
Thanks for the catch! I didn't realize it was custom - I glossed over the "Ex" at the end of the class name.
I'll look at that class and implement an integer handling method, thanks.