Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now