473,405 Members | 2,338 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,405 software developers and data experts.

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

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

Nov 20 '05 #1
2 4175
Hi,

Try this.

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

Ken
------------------
"amber" <an*******@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
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

Nov 20 '05 #2
* "=?Utf-8?B?YW1iZXI=?=" <an*******@discussions.microsoft.com> 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/>
Nov 20 '05 #3

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

Similar topics

4
by: Dante | last post by:
Just a thought: what if you posted the W3C's "Valid XHTML" images on a webpage that doesn't even use XHTML? Or with a Valid CSS thing. What could the W3C do to you anyway? Maybe I'll do that just...
5
by: Yongming Wang | last post by:
FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me? class A { public: void FuncA() { B b(this);
2
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more...
3
by: dbuchanan | last post by:
How do I explicitly cast a string to Byte? Here are my circumstances; The string is from a textbox and could be empty. The Byte is a tinyint in SQL Server 2000 I am using the AddRow method as...
2
by: Matthias Langbein | last post by:
Hi all, i want to save the position of my Addin-Toolbar to the registry. It's easy to store the position as a string with MsoBarPosition.toString(). But when I read the key, I get it as a...
13
by: archuleta37 | last post by:
I'm trying to cast a String from a web forms textbox into an Int16 (see code below), but it's not quite working. When I post the textbox with numbers (no alpha chars) I get the following error in...
2
by: elena | last post by:
Hi, All Please, i need help with cast string to DateTime: DateTime dt = new DateTime(2006,09,17); it works i can update Field in Access DB Next field is Short Time data type, how i can cast...
1
by: tedqn | last post by:
Problem: - Stored procedure input parameters data type specific (SqlDbType.Int, SqlDbType.VarChar,etc). - Form submitted value in TextBox, DropDownList, etc are all string type (I guess). Some...
1
by: =?Utf-8?B?U2lzbmF6?= | last post by:
I'm having a very strange problem I can't seem to figure out and am hoping maybe somebody has seen it before. I get an exception "Cast string to date is invalid" with this chunk of code: '...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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...

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.