Results 1 to 4 of 4

Thread: Error when assigning a dataview to a gridview control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Question Error when assigning a dataview to a gridview control

    Hi Everyone,

    I've been working on this error all day! I have a dataview created from a sqldatasource control and I'm trying to assign it to a gridview but I get Object reference not set to an instance of an object.

    Code:

    Code:
            Try
                Session("dsCustomer") = "Y"
                dsCustomer.SelectParameters("SEARCH1").DefaultValue = Trim(txtAccountNo.Text)
                Dim dv As DataView = DirectCast(dsCustomer.[Select](DataSourceSelectArguments.Empty), DataView)
                If dv.Count > 0 Then
                    C1GridView4.DataSource = dv
                    C1GridView4.DataBind()
                    C1GridView4.Visible = True
    Can't I assign a dataview as a datasource? Or not?

    Thanks!

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Error when assigning a dataview to a gridview control

    Actually the error is on this line:

    Code:
    If dv.Count > 0 Then
    Why?

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Error when assigning a dataview to a gridview control

    dv as new dataview
    but then again i'm never certain of what will happen whenever i use an sqldatasource.
    In my opinion you shouldn't try to assign a dataview the sqldatasource and then the grid.
    Just assign to the grid directly with a datasource.
    If you want it to be sqldatasource then assign the default value on page_load and, well, pray.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Error when assigning a dataview to a gridview control

    Hello,

    The "problem" that you are seeing is on this line:

    Code:
    Dim dv As DataView = DirectCast(dsCustomer.[Select](DataSourceSelectArguments.Empty), DataView)
    It is perfectly possible for the DirectCast call to return a null/Nothing value. This means, as soon as you do this:

    Code:
    dv.Count
    You are trying to access a properties of a null object, and you get the exception that you are seeing. When using DirectCast, before accessing any properties of the object, you should always do a null/Nothing before accessing any properties/methods. If it is null/Nothing then raise an exception.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width