locked
How do I work with an integer value read from a SqlDataReader object? RRS feed

  • Question

  • User-1958367851 posted

    Hello,

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

    Source Error:

    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.

    Stack Trace: 

    [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?

    Thanks!

    Monday, February 26, 2018 7:08 PM

Answers

  • User1400794712 posted

    Hi ravl13,

    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:

    //Test integer
    string queryString =
    "SELECT ID, FirstName, Age FROM Students";
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Case1801ConnectionString"].ToString()))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();
        SqlDataReader sdrEx = command.ExecuteReader();
    
        // Call Read before accessing data.
        List<int> Ages = new List<int>();
        List<string> FirstNames = new List<string>();
        while (sdrEx.Read())
        {
            Ages.Add(Convert.ToInt32(sdrEx["Age"]));
            FirstNames.Add(sdrEx["FirstName"].ToString());
        }
        // Call Close when done reading.
        sdrEx.Close();
    }

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 27, 2018 3:05 AM

All replies

  • User1400794712 posted

    Hi ravl13,

    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:

    //Test integer
    string queryString =
    "SELECT ID, FirstName, Age FROM Students";
    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Case1801ConnectionString"].ToString()))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();
        SqlDataReader sdrEx = command.ExecuteReader();
    
        // Call Read before accessing data.
        List<int> Ages = new List<int>();
        List<string> FirstNames = new List<string>();
        while (sdrEx.Read())
        {
            Ages.Add(Convert.ToInt32(sdrEx["Age"]));
            FirstNames.Add(sdrEx["FirstName"].ToString());
        }
        // Call Close when done reading.
        sdrEx.Close();
    }

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 27, 2018 3:05 AM
  • User-1958367851 posted

    Daisy,

    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. 

    Wednesday, February 28, 2018 6:58 PM