473,756 Members | 7,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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(ByVa l x As String, ByVal y as String)
Dim conn As New SqlConnection
("server=localh ost;uid=blah;pw d=blah;database =test")
Dim strSQL As String
Dim ds As New DataSet()

strSQL = "SELECT firstv.ocean_ca r, firstv.nr,first v.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("firs tv")
End Function

Calling code:

<asp:DataGrid runat=server id=dgDetails
DataSource=<%#
GetDetails(cont ainer.dataitem( "doc"),containe r.dataitem("po" ))%>
AutoGenerateCol umns=false>
<Columns>
<asp:BoundColum n DataField="ocea n_car"
HeaderText="Car rier"></asp:BoundColumn >
<asp:BoundColum n DataField="nr" HeaderText="Boo king"></asp:BoundColumn >
<asp:BoundColum n DataField="Equi p"
HeaderText="Equ ip"></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 1658
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.o cean_car, ''), IsNull(firstv.n r,
''),IsNull(firs tv.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**********@g mail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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(ByVa l x As String, ByVal y as String)
Dim conn As New SqlConnection
("server=localh ost;uid=blah;pw d=blah;database =test")
Dim strSQL As String
Dim ds As New DataSet()

strSQL = "SELECT firstv.ocean_ca r, firstv.nr,first v.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("firs tv")
End Function

Calling code:

<asp:DataGrid runat=server id=dgDetails
DataSource=<%#
GetDetails(cont ainer.dataitem( "doc"),containe r.dataitem("po" ))%>
AutoGenerateCol umns=false>
<Columns>
<asp:BoundColum n DataField="ocea n_car"
HeaderText="Car rier"></asp:BoundColumn >
<asp:BoundColum n DataField="nr" HeaderText="Boo king"></asp:BoundColumn >
<asp:BoundColum n DataField="Equi p"
HeaderText="Equ ip"></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**********@g mail.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(ByVa l x As String, ByVal y as String)
Dim conn As New SqlConnection
("server=localh ost;uid=blah;pw d=blah;database =test")
Dim strSQL As String
Dim ds As New DataSet()

strSQL = "SELECT firstv.ocean_ca r, firstv.nr,first v.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("firs tv")
End Function

Calling code:

<asp:DataGrid runat=server id=dgDetails
DataSource=<%#
GetDetails(cont ainer.dataitem( "doc"),containe r.dataitem("po" ))%>
AutoGenerateCol umns=false>
<Columns>
<asp:BoundColum n DataField="ocea n_car"
HeaderText="Car rier"></asp:BoundColumn >
<asp:BoundColum n DataField="nr" HeaderText="Boo king"></asp:BoundColumn >
<asp:BoundColum n DataField="Equi p"
HeaderText="Equ ip"></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**********@d iscussions.micr osoft.com> wrote in message
news:85******** *************** ***********@mic rosoft.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**********@g mail.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(ByVa l x As String, ByVal y as String)
Dim conn As New SqlConnection
("server=localh ost;uid=blah;pw d=blah;database =test")
Dim strSQL As String
Dim ds As New DataSet()

strSQL = "SELECT firstv.ocean_ca r, firstv.nr,first v.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("firs tv")
End Function

Calling code:

<asp:DataGrid runat=server id=dgDetails
DataSource=<%#
GetDetails(cont ainer.dataitem( "doc"),containe r.dataitem("po" ))%>
AutoGenerateCol umns=false>
<Columns>
<asp:BoundColum n DataField="ocea n_car"
HeaderText="Car rier"></asp:BoundColumn >
<asp:BoundColum n DataField="nr" HeaderText="Boo king"></asp:BoundColumn >
<asp:BoundColum n DataField="Equi p"
HeaderText="Equ ip"></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(cont ainer.dataitem( "doc"),containe r.dataitem("po" ))

might either container.datai tem("doc") or container.datai tem("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

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

Similar topics

0
1453
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: Requests...Request (1..n)... Payload (1)... PayloadCollection (1) ... Payload (0...n) I have the knowledge about the root object Responses which I can create directly. However I don't know what is the type for Request and Payload (These will be versioned...
10
2618
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 of that software. That subject seems not to be addressed, at least not directly, in the C FAQ where FAQ 5.2 seems most relevant. References: * C FAQ 5.2 Null pointers (Including conditions where "casting" of null pointer...
1
2324
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 = (Microsoft.Office.Core.DocumentProperties)aDoc.CustomDocumentProperties; (where aDoc is active Word document). But this gives me an Invalid Cast Exception. I feel like using the correct syntax. Pls help.
0
1588
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 Trusted connection or get a file information using FileInfo class). This is probably because my application is running under PETER\ASPNET account, where PETER is my computer's name. I can solve this problem by using Impersonation. However, when...
1
2191
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 = (BlogEntryViewer)ctrl; About 80% of the time this fails in the cast. The BlogEntryViewer.ascx file specifies the right file (and it compiles btw): <%@ Control Language="C#" AutoEventWireup="true" CodeFile="BlogEntryViewer.ascx.cs"
4
3839
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 absolutely no added functionality to it.
3
2014
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 & " " & CInt(arrRuleList(i).LeadMemberValue)) Then where arrRuleList(i).operator = ">"
17
2512
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.