none
Unable to cast object of type 'System.DBNull' to type 'System.String'.

    Question

  • Hello,

    I am very new new to .net and C# so I'm sure this is a very basic question but it has me stumped.  I have loaded a DataGridView from an Access database and when I click on a record I want it to populate form fields from the selected record.  The text fields from the DataGrid seem to work fine but number or date fields are giving me a problem when they are null.  The code I'm using is:

    this.txtManagerZip.Text = (string)dgv1["Zip", e.RowIndex].Value;

    and when empty produces the above error.

    I've tried various flavors of Try and Catch but I can't get the proper syntax.

    Can anyone help?


    Vic


    Visual Basic; Access; PHP; SQL Server; MySQL Developer
    Friday, December 19, 2008 2:52 AM

Answers

  • There's a number of different solutions for dealing with DBNull; each programmer has their own preferences.

    I like as-casting database values:
    this.txtManagerZip.Text = dgv1["Zip", e.RowIndex].Value as string; 

    which will set "this.txtManagerZip.Text" to null if the Value is DBNull.

           -Steve
    • Marked as answer by VicSpain Friday, December 19, 2008 2:46 PM
    Friday, December 19, 2008 3:11 AM
  • Hi

    try

    this.txtManagerZip.Text = Convert.ToString(dgv1["Zip", e.RowIndex].Value); 


    Thanks
    Dhinesh paramasivam
    • Marked as answer by VicSpain Friday, December 19, 2008 2:46 PM
    Friday, December 19, 2008 4:05 AM

All replies

  • There's a number of different solutions for dealing with DBNull; each programmer has their own preferences.

    I like as-casting database values:
    this.txtManagerZip.Text = dgv1["Zip", e.RowIndex].Value as string; 

    which will set "this.txtManagerZip.Text" to null if the Value is DBNull.

           -Steve
    • Marked as answer by VicSpain Friday, December 19, 2008 2:46 PM
    Friday, December 19, 2008 3:11 AM
  • Hi

    try

    this.txtManagerZip.Text = Convert.ToString(dgv1["Zip", e.RowIndex].Value); 


    Thanks
    Dhinesh paramasivam
    • Marked as answer by VicSpain Friday, December 19, 2008 2:46 PM
    Friday, December 19, 2008 4:05 AM
  • Thank you Steve and Dhinesh you've gotten me out of the mud and and rolling again!

     

    Vic

     


    Visual Basic; Access; PHP; SQL Server; MySQL Developer
    Friday, December 19, 2008 2:48 PM
  •  
                                if ( row[ 0 ] != System.DBNull.Value )  
                                {  
                                    acc = ( int )row[ 0 ];  
                                } 
    In C# this issue will be taken care off differently.


    AlexB
    Friday, December 19, 2008 4:08 PM
  • VicSpain said:

    Hello,

    I am very new new to .net and C# so I'm sure this is a very basic question but it has me stumped.  I have loaded a DataGridView from an Access database and when I click on a record I want it to populate form fields from the selected record. 


    Vic!

    Why don't you use Complex and Simple DataBinding mechanism to data bound your text boxes. This way when you scroll up/down your datagridview, the text boxes would be updated automatically. And when you will change the text in any of the text box it would be updated in the data object, and hence in the datagridview also.
    MSN: kalilani@hotmail.com - Yahoo: kalilanipk
    Friday, December 26, 2008 1:44 PM