Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Friday, March 9, 2012

NULLReferenceException from SqlDataReader.GetValue()

I am getting a random exception coming out of ADO.NET's SqlDataReader that I can't seem to track down for the life of me. I have put SQL Server and IIS under heavy load, with neither giving me the exceptions except occasionally (they come when there is no load as well). Web Site is built in VS2005 using .NET 2.0.

Basically, I have a web page that caches a few tables from a SQL database (about 10 tables are loaded into cache from this page) and the error can come from any of the tables while they are being read from the database randomly. I had assumed it was possible that a timeout was occuring while reading the data but from what the brief results google shows, there would be a different exception thrown it a timeout had occured.

Here is the exception that is showing up:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout)
at System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i)
at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)
at Library.Data.DataProxy.ExecuteStoredProcedure(ArrayList& returnValues, TypeFactory factory)

I got the following exception once from the same section of code as well:

System.InvalidOperationException: Invalid attempt to FieldCount when reader is closed.
at System.Data.SqlClient.SqlDataReader.get_FieldCount()
at Library.Data.DataProxy.ExecuteStoredProcedure(ArrayList& returnValues, TypeFactory factory)

The code generating these errors is on line 29 below:

1 /// <summary>
2 /// Executes a stored procedure that returns values
3 /// </summary>
4 /// <param name="returnValues">An arraylist of Types</param>
5 /// <param name="factory">The Type specific factory</param>
6 /// <returns>True if successful, false otherwise</returns>
7 public bool ExecuteStoredProcedure(out ArrayList returnValues, TypeFactory factory)
8 {
9 bool bResult =true;
10 SqlDataReader reader =null;
11
12 returnValues =new ArrayList();
13 MenseType nextType =null;
14
15 try
16 {
17 // Open database connection
18 OpenConnection();
19
20 // execute SP
21 reader = m_sqlCommand.ExecuteReader();
22
23 while(reader.Read())
24 {
25 nextType = factory.CreateInstance();
26
27 for(int i = 0; i < reader.FieldCount; i++)
28 {
29 object val = reader.GetValue(i);
30
31 if(DBNull.Value == val)
32 val =null;
33
34 nextType.SetValue(reader.GetName(i), val);
35 }
36
37 returnValues.Add(nextType);
38
39 }// while
40
41 }// @. try
42 catch(Exception ex)
43 {
44 returnValues =null;
45 m_LastError = ex.ToString();
46 bResult =false;
47
48 #if(DEBUG)
49 Library.IO.Logging.AddSQLMessage(ex);
50 #endif
51
52 }// @. catch
53 finally
54 {
55 // close the reader if nessessary
56 if (null != reader)
57 reader.Close();
58
59 // close db connection
60 CloseConnection();
61
62 }// @. finally
63
64 return bResult;
65
66 }// ExecuteStoredProcedure()

Any input would be greatly appreciated.

Do you need to read every table as an untyped arraylist, why not read each column of each table as a specific data type into a typed structure?

Wednesday, March 7, 2012

Null Values

I set up a new SQL database file, in that file I allowed nulls, When I went through code to save the record, the exception is saying it doesnt allow nulls.

Before I get to involved with SQL, is it a bad practice to use nulls?

If it is what do you enter in place of the null value,

which will lead to more code, right?

Davids Learning SQL

Having and using nulls is not bad practice.....

Your problem maybe that your trying to have a null on your key field........

|||

No, the primary key did have a number assigned that did not exist.

I did a msgbox to throw the value back to me just to see in case and it was the number I chose.

I intentionally did not enter a value into a int field to see what would happen.

I opened the database file, and allow nulls is checked for the field and the field is not the primary

Any thoughts?

Davids Learning

|||Is the field a date field?|||I remember running in to similar problem, but I am not sure if it is exactly the same. The work around (or the solution for that matter) was I opened the DataSet containing my dataTable. Clicked on the field in that dataTable and in properties, I Changed 'NullValue' property to (Nothing). By default, it's set to (Throw Exception), which forces the dataSet to throw exception even if its AllowDBNull is set to true. See if that solves your issue.|||

is there a way to default to "Nothing" instead of throwing an exception?

I wont be able to try your suggestion until Monday, when I get back to my server at work.

Thanks

Davids Learning

|||Move the thread in this forum in order to get better answers.|||That depends, what does nothing mean in your case ? Normally nothing is equal to NULL when "nothing" is entered.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

Hey, sorry for the delay, been having ticker problems, Im back now

What I was getting at was to have the designer default or not check for null instead of throwing exceptions.

There is not but 1 field that needs not to be null and thats the primary field and I am taking care of that.

Is there a way to accomplish that.

The tables that I am dealing with contain a lot of fields and it will take some time to go through and change.

Thanks Again

Davids Learning

|||Hi,

what about setting a default value in SQL Server then ?

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

I need to post what I did to mask the problem

I went into the table designer in VB and changed how each column handles nulls, basically setting them to not throw an exception on dbnulls.

I didnt want values in some fields intentionally because of other factor, like dates, if I set a default date in an expiration field(that would be bad).

I will on the other hand have to watch and plan on handling nulls now, and thats alright.

Thanks

Davids Learning

|||To me, it looked like a VB bug, but I will leave the judgement for you guys. If I allow nulls in my database table and create a typed DataSet from that table, the DataSet throws an error when it encounters a null value. If the DataSet's AllowDBNull is set to true, then it should default to 'Nothing' for NullValue property. It does not seem logical to me (Allow DBNull, but throw an error!!!). For those who want to reproduce the problem, use bound DataGridview with some fields that allow DBNull and try to save without entering data in those fields.|||

Glad to see someone in the same boat

Davids Learning

Saturday, February 25, 2012

Null Reference Exception

I cannot seem to get the crystal reports to work in asp.net

I set the databindings source property and the report shows in the designer a little weird. In the Initialize component, i call the BindData() and when i run the app i get a NullReferenceException.

I did exactly what a walkthrough from msdn said to do and its not working.

I also tried setting a breakpoint on InitializeComponent to see what was happening, but it errors before it gets called.

Any suggestions about this NullReferenceExceptionStill need help on this one

Monday, February 20, 2012

NULL Exception Configuring SRS 2005

We just installed SQL Server 2005 w/SRS on a Windows 2003 box. I fired up
the Report Server Configuration Manager to, well, configure things. The
server status was green. When to the "Report Server Virtual Directory" and
clicked "New". It seemed to do it's thing, then all of a sudden the tool
just exited without any messages.
Now when we go back into the tool, the Report Server Virtual Directory icon
is green, but when we try to go to that tab, we get the following error:
There was an error while switching panels. The most likely cause is an
error retrieving WMI properties. The exception details are:
Object reference not set to an instance of an object.
We have several web sites defined in IIS (including having SBS installed
w/Sharepoint). I was unable to find the ReportServer virtual directory
created in any of them. Nor where the SRS app pools created. The Report
Server Virtual Directory icon is grayed out.
Any suggestions on how to proceed to resolve this error?
Thanks,
ChrisOn Feb 16, 7:11 am, "Chris Walls" <chwal...@.community.nospam> wrote:
> We just installed SQL Server 2005 w/SRS on a Windows 2003 box. I fired up
> the Report Server Configuration Manager to, well, configure things. The
> server status was green. When to the "Report Server Virtual Directory" and
> clicked "New". It seemed to do it's thing, then all of a sudden the tool
> just exited without any messages.
> Now when we go back into the tool, the Report Server Virtual Directory icon
> is green, but when we try to go to that tab, we get the following error:
> There was an error while switching panels. The most likely cause is an
> error retrieving WMI properties. The exception details are:
> Object reference not set to an instance of an object.
> We have several web sites defined in IIS (including having SBS installed
> w/Sharepoint). I was unable to find the ReportServer virtual directory
> created in any of them. Nor where the SRS app pools created. The Report
> Server Virtual Directory icon is grayed out.
> Any suggestions on how to proceed to resolve this error?
> Thanks,
> Chris
One thought would be to check the EventViewer for SSRS/IIS-related
errors. They might lead you to a more informative error. Also, you
might check the SSRS log files, if SSRS was installed to that point.
They are usually located in this path: C:\Program Files\Microsoft SQL
Server\<MSSQL.2 -or- MSSQL.3>\Reporting Services\LogFiles. As a last
resort, you could try uninstalling and reinstalling SSRS/SQL Server.
Hope this helps.
Regards,
Enrique Martinez
Sr. Software Consultant|||Chris,
I don't know if this is related to your issue, but I just ran into the same
problem in Reporting Services Configuration Manager.
After Setting up SharePoint Integration the Report Manager section is grayed
out because Report Manager isn't needed in SharePoint integrated mode. When
I clicked on that section I saw a message saying that it's not supported and
that I should uninstall it to reduce the security footprint of the server.
It sounded like a good idea so I uninstalled the Report Manager portion of
SRS. Well that lead to the error you're seeing.
I reinstalled Report Manager and now I can get back into the Report Server
Virtual Directory section.
Seems like a bug to me.
Is Report Manager installed on this machine, and can you get into that
section of the Configuration Manager?
-Alex
"Chris Walls" wrote:
> We just installed SQL Server 2005 w/SRS on a Windows 2003 box. I fired up
> the Report Server Configuration Manager to, well, configure things. The
> server status was green. When to the "Report Server Virtual Directory" and
> clicked "New". It seemed to do it's thing, then all of a sudden the tool
> just exited without any messages.
> Now when we go back into the tool, the Report Server Virtual Directory icon
> is green, but when we try to go to that tab, we get the following error:
> There was an error while switching panels. The most likely cause is an
> error retrieving WMI properties. The exception details are:
> Object reference not set to an instance of an object.
> We have several web sites defined in IIS (including having SBS installed
> w/Sharepoint). I was unable to find the ReportServer virtual directory
> created in any of them. Nor where the SRS app pools created. The Report
> Server Virtual Directory icon is grayed out.
> Any suggestions on how to proceed to resolve this error?
> Thanks,
> Chris
>
>