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!
Re: Error when assigning a dataview to a gridview control
Actually the error is on this line:
Code:
If dv.Count > 0 Then
Why?
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.
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:
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