473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.dat aitem("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. ;)

Nov 21 '05 #1
10 1615
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

<ro**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.dat aitem("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. ;)

Nov 21 '05 #2
Roy
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*

Nov 21 '05 #3
Roy
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?

Nov 21 '05 #4
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
"ro**********@gmail.com" wrote:
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.dat aitem("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. ;)

Nov 21 '05 #5
Good point.

Chris

"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:85**********************************@microsof t.com...
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
"ro**********@gmail.com" wrote:
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.dat aitem("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. ;)

Nov 21 '05 #6
Roy
Thanks for the input Kerry, but dishearteningly enough, even after
modifying the code as you suggested, the exact same error occurs.
*sigh*

Nov 21 '05 #7
Roy,

When you call the function like this:

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

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

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

Nov 21 '05 #8
Roy
Yes on both counts.

Nov 21 '05 #9
Roy
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."

Nov 21 '05 #10
Roy
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.

Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by XSDObjectGen. The hierarchy goes like this:...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
1
by: Sandeep | last post by:
Hi, I am trying for word 2000 automation with c#. There I want to get all custom document property of the document as follows: Microsoft.Office.Core.DocumentProperties props =...
0
by: Peter Afonin | last post by:
Hello: When I try to access a SQL server or a network share from an ASP.Net application that I run on my computer, I run into security problems (for instance, I cannot execute DTS package using...
1
by: Shawn Wildermuth | last post by:
I have some simple code that loads a control with LoadControl and casts it to the codebehind object: Control ctrl = LoadControl("~/Controls/BlogEntryViewer.ascx"); BlogEntryViewer entry =...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
3
by: hazz | last post by:
I get an InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid. for If (CInt(objBuyInfo.Budget) & " " & arrRuleList(i).operator & " " &...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.