PC Review


Reply
Thread Tools Rate Thread

Cast from 'DBNull' to "string' is not valid

 
 
=?Utf-8?B?YW1iZXI=?=
Guest
Posts: n/a
 
      26th Feb 2004
Hello
I'm new to this, and I'm not sure how to avoid the following error
I use the following code to pull data from a SQL database, depending on the choice selected in a listbox

Private Sub lBoxCP_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lBoxCP.SelectedIndexChange

dim xyz as DataRowView = lBoxCP.SelectedIte
txtCPMain.Text = xyz("STR_CUTTING_PERMIT"
txtOArea.Text = xyz("STR_OPERATING_AREA"

and so on..
this works great as long as there is data in the field, but if it's empty, I get an error "Cast from 'DBNull' to 'string' is not valid

How do I have VB.NET just leave it blank

Thanks
Ambe

 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      26th Feb 2004
* "=?Utf-8?B?YW1iZXI=?=" <(E-Mail Removed)> scripsit:
> dim xyz as DataRowView = lBoxCP.SelectedItem
> txtCPMain.Text = xyz("STR_CUTTING_PERMIT")


\\\
If Not xyz(...) Is DBNull.Value Then
...
End If
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet/>
 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      26th Feb 2004
Hi,

Try this.

txtCPMain.Text = xyz("STR_CUTTING_PERMIT").ToString
txtOArea.Text = xyz("STR_OPERATING_AREA").ToString

Ken
------------------
"amber" <(E-Mail Removed)> wrote in message
news:199DE4BA-A3DA-4220-8262-(E-Mail Removed)...
> Hello,
> I'm new to this, and I'm not sure how to avoid the following error.
> I use the following code to pull data from a SQL database, depending on

the choice selected in a listbox.
>
> Private Sub lBoxCP_SelectedIndexChanged(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles lBoxCP.SelectedIndexChanged
>
> dim xyz as DataRowView = lBoxCP.SelectedItem
> txtCPMain.Text = xyz("STR_CUTTING_PERMIT")
> txtOArea.Text = xyz("STR_OPERATING_AREA")
>
> and so on...
> this works great as long as there is data in the field, but if it's empty,

I get an error "Cast from 'DBNull' to 'string' is not valid"
>
> How do I have VB.NET just leave it blank?
>
> Thanks,
> Amber
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid. hazz Microsoft VB .NET 3 7th Jul 2005 10:22 PM
Cast from type 'DBNull' to type 'String' is not valid. tshad Microsoft ASP .NET 6 15th Dec 2004 05:10 PM
Cast from type 'DBNull' to type 'String' is not valid. =?Utf-8?B?RGF2ZSBCeXJvbg==?= Microsoft Dot NET 3 3rd Sep 2004 07:15 AM
Cast from type 'DBNull' to type 'String' is not valid. when creating dataset troutbum Microsoft ADO .NET 5 13th Mar 2004 01:30 PM
Cast from type 'DBNull' to type 'String' is not valid. Elmo Watson Microsoft ASP .NET 3 25th Dec 2003 03:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 AM.