Results 1 to 5 of 5

Thread: [RESOLVED] DBNull to type String is not valid. From a datagrid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Resolved [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.

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    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:
    1. If TypeOf datagrid.Item(0, eRow).Value Is DBNull Then

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    Re: DBNull to type String is not valid. From a datagrid

    Excellant - works perfectly

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    28

    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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2010
    Location
    UK
    Posts
    288

    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
  •  



Click Here to Expand Forum to Full Width