|
-
Aug 13th, 2010, 11:28 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] DBNull to type String is not valid. From a datagrid
I am trying to populate a textbox.text with a value with a record from a datagrid.
I can get my value from the datagrid by the following
Dim eRow As Integer
eRow = datagrid.CurrentRow.Index
txtSelecteditem.Text = datagrid.Item(0, eRow).Value
This all works fin when there is a value in the cell it is picking up. However, I hit a problem when this filed is blank.
The error I get is:
Conversion from type 'DBNull' to type 'String' is not valid.
How can I get the textbox to populate with null if the field is null.
-
Aug 13th, 2010, 11:40 AM
#2
Frenzied Member
Re: DBNull to type String is not valid. From a datagrid
Before assigning it to the textbox you can check whether it is null or not by using this
vb Code:
If TypeOf datagrid.Item(0, eRow).Value Is DBNull Then
-
Aug 13th, 2010, 11:45 AM
#3
Thread Starter
Hyperactive Member
Re: DBNull to type String is not valid. From a datagrid
Excellant - works perfectly
-
Aug 13th, 2010, 11:47 AM
#4
Junior Member
Re: DBNull to type String is not valid. From a datagrid
Hi.
The other way you could do it, is to use the .ToString method.
Code:
txtSelecteditem.Text = datagrid.Item(0, eRow).Value.ToString
That way, any value that is DBNull (blank) will be filled as it is on the textbox.
-
Aug 13th, 2010, 11:52 AM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] DBNull to type String is not valid. From a datagrid
Works great too.
I thought I had tried that earlier but obviously not.
Many thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|