Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
ASP .Net
SqlDataReader showing Empty columns until after error.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="lithoman, post: 1984450"] I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the reader.Read(), it has a row with 13 data columns, but they're all empty. So, my GetInt() function throws an error. When it jumps to the catch(), reader's columns then show the proper data. What gives? I have reader._data[0] (which is the ID column) in my variable Watch and it should be an Int32 and contain the value 18. After reader.Read() it has _type=Empty; Value=; The next line, int tid, throws an error {"Specified cast is not valid."} and jumps to catch() After catch(), however, it has _type=Int32; Value=18; ---------------------------------------------------------------------------------------------------------- SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("Batch_Select", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)); cmd.Parameters["@ID"].Value = 18; SqlDataReader reader; try { con.Open(); reader = cmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); int tid = GetInt(reader, "ID"); int tpartid = GetInt(reader, "PartID"); string tpartnum = GetString(reader, "Part#"); string tpartname = GetString(reader, "PartName"); string tdesc = GetString(reader, "Description"); string trev = GetString(reader, "Revision"); string tserial = GetString(reader, "SerialNumber"); int tqty = GetInt(reader, "Quantity"); string ttdr = GetString(reader, "TDR"); string trm = GetString(reader, "RM"); string trmlot = GetString(reader, "RMlot"); int tstatus = GetInt(reader, "StatusID"); DateTime tdate = GetDate(reader, "DateBorn"); } else { throw new ApplicationException("Retrieval Error:<br /> Unable to load Part: " + id); } } catch (Exception err) { throw new ApplicationException("Data Error:<br />" + err.Message); } finally { con.Close(); } ---------------------------------------------------------------------------------------------------------- I'm sure someone is going to ask, so here's the GetInt() ---------------------------------------------------------------------------------------------------------- public static int GetInt(IDataReader reader, string columnName) { int columnNum = reader.GetOrdinal(columnName); if (reader.IsDBNull(columnNum)) { return 0; } else { return (int)reader[columnName]; } } ---------------------------------------------------------------------------------------------------------- And this exact same code snippet format is working fine in another function for another Stored Procedure, thus my confusion. This code is in a different class, however. I'm trying to figure out if there is anything in the class that could interfere. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
ASP .Net
SqlDataReader showing Empty columns until after error.
Top