Solved

ERROR: OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT

Posted on 2008-06-11
4
810 Views
Last Modified: 2010-04-21
I'm trying to extract data from a sqldatasource on a page load event.  When I do, I get the above error when executing the dt = ctype(sqldatasourcecustomer.select(datasourceselectarguments.empty),data.dataview).totable()

I'm new to asp.net and copied this code from another site.  It seems to work in some situations but I don't know enough about the ctype function to understand what it's asking for here.  HELP!


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBoxLastNameCompany.Focus()
 
        'MAKE SURE THE SESSION VARIABLE IS NOT EQUAL TO ZERO MEANING THAT A VALID CUSTOMER ID IS BEING REQUESTED
        If Session.Item("CUSTOMER_ID") <> "0" Then
            'CREATE THE DATA TABLE OBJECT
 
            'CREATE THE DATA TABLE VARIABLE
            Dim dt As Data.DataTable
            'HERE'S WHERE I GET THE ERROR "OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT"
            dt = CType(SqlDataSourceCustomer.Select(DataSourceSelectArguments.Empty), Data.DataView).ToTable()
 
            'HERE I AM EXTRACTING THE DATA FROM THE DT OBJECT INTO FORM COMPONENTS
            If Page.IsPostBack = False Then
                If String.IsNullOrEmpty(dt.Rows(0).Item(2).ToString) = False Then
                    TextBoxLastNameCompany.Text = dt.Rows(0).Item(2)
                End If
                If String.IsNullOrEmpty(dt.Rows(0).Item(3).ToString) = False Then
                    TextBoxFirstName.Text = dt.Rows(0).Item(3)
                End If
                DropDownListPaymentTerms.Text = dt.Rows(0).Item(4)
                If dt.Rows(0).Item(5) = 1 Then
                    CheckBoxRequirePO.Checked = True
                Else
                    CheckBoxRequirePO.Checked = False
                End If
 
            End If
        End If
 
 
    End Sub

Open in new window

0
Comment
Question by:Randy Rich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
NazoUK earned 500 total points
ID: 21761328
What's the sqldatasource doing?
I suspect in some cases the select statement isn't returning any records.

Try this and see if it stops the crash:

Dim dt As Data.DataTable
Dim dv as Data.DataView
            'HERE'S WHERE I GET THE ERROR "OBJECT REFERENCE NOT SET TO AN INSTANCE OF AN OBJECT"
            dv = CType(SqlDataSourceCustomer.Select(DataSourceSelectArguments.Empty), Data.DataView)
            if dv isnot nothing then dt=dv.toTable()
            'HERE I AM EXTRACTING THE DATA FROM THE DT OBJECT INTO FORM COMPONENTS
            If Page.IsPostBack = False and dt isnot nothing Then
...
0
 

Author Closing Comment

by:Randy Rich
ID: 31466236
Yes that worked fine.  You are right, because of the way that I was querying the database, I wasn't getting a result set.  Doing it that way at least stopped it from giving an error.  I was then able to modify the query.  Can you give me a detail explanation of what is going on at every line?  For instance, I'm fuzzy about what the ctype function does and what exactly is it looking for in parameters.
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 21768237
Ctype is a function that converts from one type to another, in fact in this case I think it's probably unnecessary as I think SqlDataSource.Select returns a Dataview anyway.
Probably at some point you've encountered a function like cint which converts a value to an integer, e.g.

Dim s as string = "23"
Dim i as integer = cint(s)

Ctype is a more generalised version of functions like cint, so we could write the above line:

Dim i as integer = ctype(s, integer)

In VB with option strict off we could just write Dim i as integer = s to perform what an implicit conversion but this is bad practise and should be avoided.

So why is this important? Consider the definition of a  button click event:

Protected Sub btn_click(sender as object, e as eventargs)

In this case sender is the button that was clicked, but because sender is defined as object you can't query it directly in Visual Studio intellisense to get the standard properties of the button but if we use the ctype function eg:

Dim btn as button = ctype(sender, button)

then btn will refer to the button that was clicked and we can access and alter properties of this as required.

In your code: dv = CType(SqlDataSourceCustomer.Select(DataSourceSelectArguments.Empty), Data.DataView)
the line is assuming that SqlDataSourceCustomer.Select returns a type that isn't a DataView but can be converted to a DataView. As I say though, I'm not sure this is necessary, you could probably rewrite it as
dv = SqlDataSourceCustomer.Select(DataSourceSelectArguments.Empty)

0
 

Author Comment

by:Randy Rich
ID: 21768730
Thanks, that was explained very well.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question