Solved

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

Posted on 2008-06-11
4
807 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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