"Cast" error with VB code in .aspx page, with a twist...

  • Thread starter Thread starter roy.anderson
  • Start date Start date
R

roy.anderson

Error is thus: "Cast from type 'DBNull' to type 'String' is not valid."
Simple, right? Well, no (or at least not for me).

Here's a function, followed by the calling code below:

Function GetDetails(ByVal x As String, ByVal y as String)
Dim conn As New SqlConnection
("server=localhost;uid=blah;pwd=blah;database=test")
Dim strSQL As String
Dim ds As New DataSet()

strSQL = "SELECT firstv.ocean_car, firstv.nr,firstv.equip "
strSQL &= "FROM firstv INNER JOIN "
strSQL &= "lla ON firstv.doc + firstv.po = lla.doc + lla.po "
strSQL &= "WHERE firstv.doc = " & x & "AND firstv.po = " & y

Dim da As New SqlDataAdapter(strSQL, conn)
da.Fill(ds, "firstv")
Return ds.Tables("firstv")
End Function

Calling code:

<asp:DataGrid runat=server id=dgDetails
DataSource=<%#
GetDetails(container.dataitem("doc"),container.dataitem("po"))%>
AutoGenerateColumns=false>
<Columns>
<asp:BoundColumn DataField="ocean_car"
HeaderText="Carrier"></asp:BoundColumn>
<asp:BoundColumn DataField="nr" HeaderText="Booking"></asp:BoundColumn>
<asp:BoundColumn DataField="Equip"
HeaderText="Equip"></asp:BoundColumn>
</Columns>
</asp:DataGrid>


The error occurs on the very first line of the calling code above.
Naturally, I thought "ah, it must be pulling in nulls from the doc and
po fields in firstv and that must be the cause." So I opened Query
Analyzer and changed all the nulls (in every column) into blanks. Still
got the error so I went back to QA and changed them all to "xyz."
Still, I got the error!! I thought maybe it had to do with the "ByVal"
statement in the function and switched to ByRef... still no go. Anyone
have any tips, please?
Thanks! And yes, I am still am relatively new to .NET. ;)
 
I am not sure that I have an answer for you if your sure there are no nulls
coming through your sql. This may be just a hint for next time for you
then. This is how I would write the statement next time; at least I do when
I want to eliminate snulls as an issue.

strSQL = "SELECT IsNull(firstv.ocean_car, ''), IsNull(firstv.nr,
''),IsNull(firstv.equip, '') "
strSQL &= "FROM firstv INNER JOIN "
strSQL &= "lla ON firstv.doc + firstv.po = lla.doc + lla.po "
strSQL &= "WHERE firstv.doc = " & x & "AND firstv.po = " & y

Chris
 
Hey Chris,

Thanks for the input. In sheer desperation I popped your code there in
place of mine. Same error on the same line. *sigh*
 
Actually... something else occurred to me. Could the error perhaps be a
red herring? As in, assuming either "x" or "y" contained blank data,
could the join or where part of the sql statement (within the function)
be causing the "cast" error?

Would the compiler recognize the empty x or y values as '' or ....?
Anyone know how the compiler would interpret blanks in this case?
 
Roy,

Since x and y are strings, when you are integrating them into your SQL Where
clause I think you need to surround them with single-quote delimiters:

strSQL &= "WHERE firstv.doc = '" & x & "' AND firstv.po = '" & y & "'"

Kerry Moorman
 
Good point.

Chris

Kerry Moorman said:
Roy,

Since x and y are strings, when you are integrating them into your SQL
Where
clause I think you need to surround them with single-quote delimiters:

strSQL &= "WHERE firstv.doc = '" & x & "' AND firstv.po = '" & y & "'"

Kerry Moorman
 
Thanks for the input Kerry, but dishearteningly enough, even after
modifying the code as you suggested, the exact same error occurs.
*sigh*
 
Roy,

When you call the function like this:

GetDetails(container.dataitem("doc"),container.dataitem("po"))

might either container.dataitem("doc") or container.dataitem("po") be null?

Kerry Moorman
 
Correction. I misread what you were saying. As of now, the table those
two fields are extracted from (firstv) contains absolutely no "NULL"
values at all. There are blank values here and there, but no "NULL."
 
Thanks for the help you two... I figured it out. I changed the string
parameters to object parameters then changed them back using .ToString
within the function. Sloppy perhaps, but it works. :)

I have encountered something beyond this though and I'd love your
input. Posting it separately, since it is of a different subject matter.
 
Back
Top