Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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