[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2008-06-11
4
Medium Priority
?
812 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 2000 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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