Conversion from type 'DBNull' to type 'String' is not valid.

 Hello Experts,

I am reading data into a datagrid and am checking for nulls

Here is the code in the datagrid which checks a function in the code-behind:

<ItemTemplate>
    <asp:Label id="Label2" runat="server" text='<%# GetPickUpDate(container.dataitem("ActualPickUpDate"))%>'></asp:Label>
</ItemTemplate>

 Here is the funtion:

Function GetPickUpDate(ByVal DateVal As Date) As String
            If Not IsDBNull(DateVal) Then
                DateVal = String.Format("{0:d}", DateVal)
            Else
                DateVal = String.Empty
            End If
            Return DateVal
End Function

 

But I am getting an error -  " Conversion from type 'DBNull' to type 'String' is not valid. "

 

Also for a separate case I need to pull boolean value from database and return either "F" or "P" and at the same time check for NULLs.

Here is the function for that - 

        Function GetFullOrPartial(ByVal FldVal As Boolean) As String
            If Not IsDBNull(FldVal) Then
                Select Case FldVal
                    Case "0"
                        Return "P"
                    Case "1"
                        Return "F"
                End Select
            Else
                FldVal = String.Empty
                Return FldVal
            End If
        End Function

and the datagrid line is:

<ItemTemplate>
     <asp:Label id="Label1" runat="server" text='<%# GetFullOrPartial(container.dataitem("FullOrPartial"))%>'></asp:Label>
</ItemTemplate>

 

In this case I get a - "Conversion from type 'DBNull' to type 'Boolean' is not valid."

 

Not sure what I foing wrong. Thanks for the help!

Steve

 

 

0
stevew8
4/12/2009 9:28:46 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

2 Replies
638 Views

Similar Articles

[PageSpeed] 36

Hi Steve,

This is one of the quirks when dealing with DBNulls.  Since DBNull is a type, not just a null string, the type doesn't match the function signature and you get this error.  If you change the function signature to accept an object, then check for DBNull and process accordingly, it should work.

 

Function GetPickUpDate(ByVal DateValObj As Object) As String
    Dim DateString as String
    If DateValObj = DBNull.Value Then
        DateString = String.Empty
    Else
        DateString = Convert.ToDateTime(DateValObj).ToString("{0:d}")
    End If
    Return DateString
End Function
 

Hope that helps.

Aaron


Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
0
agolden
4/13/2009 12:20:27 AM

 That is exactly what I was looking for. Thanks!

0
stevew8
4/13/2009 1:00:09 AM
Reply:

Similar Artilces:

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

I get Conversion from type 'DBNull' to type 'String' is not valid.
I get the following errors when I try and access certain pages.  It is caused by not populating all the text boxes with data and subsequent viewing causes these conversion errors.  Does any one else experience this?  How have you guys worked around this problem? Conversion from type 'DBNull' to type 'String' is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Conversion f...

Conversion from type 'DBNull' to type 'String' is not valid
I get this error when I try to add an event:Conversion from type 'DBNull' to type 'String' is not validI've been able to add events before, but all of the sudden I'm getting the error...Here is the output of the error:Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.Source Error: Line 115: </h3> Line 116: <p> Line 117: <asp:Label ID="descriptionLabel" runat="server" Text='<%# truncate(CStr(Eval("description"))) %>' />...

Conversion from type 'DBNull' to type 'String' is not valid...
Thanks for your time.  Getting following error when grid source populates a rows Date field with Null:     - Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.It works works when I filter the nulls out, but I need to show all rows in the grid. Boils down to:  a) How could I maintain a blank value in the grid and cast this properly to include all rows?    < or >  b) Is there a way to default a value that isnt a date (to avoid the null & keep the IsDate logic as is)  c) ...

Conversion from type 'DBNull' to type 'String' is not valid
Hello Friends            How are you?? Friends i am getting problem in SQL Server 2005. I am deployng web application on production server as well as Databse also. In production server i inserted new field in all tables which is rowguid and its type is uniqueidentifier. The default binding for this field is newsequentialid(). In some pages it works ok but in some places it generates error like 'Conversion from type 'DBNull' to type 'String' is not valid'. Can anybody help me to solve this problem. Its urgent so plz re...

Conversion from type 'DBNull' to type 'String' is not valid
I get an error (Conversion from type 'DBNull' to type 'String' is not valid.) when I try to assign a field value to a text box using the datareader. Is there code that will fix this error?  wrap an IF check around the retrieval and check for DBNull you'll need to check for null first, before you assign it - - Something like: if Not dr("Yourfield") is System.DbNull.Value then...........David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper - easily control pr...

Conversion from type 'DBNull' to type 'String' is not valid
Hello! All,I try to use ObjectDataSource with business layers and used the code given by SDK 2.0. But I got the following message: Conversion from type 'DBNull' to type 'String' is not valid.  I couldn't figure out what 's the problem and felt sorry about this.I attached my code below.  Can anyone help me out of it. Thanks a lot! Vivia  Error Massage: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code Exception ...

Conversion from type 'DBNull' to type 'String' is not valid
I am using Jeremy's Personal Web Site, which I must say is great.  I have uploaded the site to my GoDaddy web hosting account and created the database tables.  I created an entry for a user and for the site_settings.  I am getting the following error: Conversion from type 'DBNull' to type 'String' is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: C...

Conversion from type 'DBNull' to type 'String' is not valid #2
I'm getting a error message saying: Server Error in '/' Application. Conversion from type 'DBNull' to type 'String' is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.Source Error: Line 113: </h3> Line 114: ...

Conversion from type 'DBNull' to type 'String' is not valid #2
i have with my DB fetch content is Ogject class so i am not use any dataset or datatable but i have problem when field in DB is empty i can insert DBNull in string like this curr.visible = r("visible") curr.title = r("title")curr.description = r("description") curr.price = r("price")   how can i  solve this problem without put if with every insert code fedailr:curr.visible = r("visible") curr.title = r("title")curr.description = r("description") curr.price = r("price") So I assume r ...

Bound TextBox error: Conversion from type 'DBNull' to type 'String' is not valid
 Hi,I use DataSet designer to generate my strongly typed datasets and to bind my FormView to a Business Layer (BLL). The BLL uses the generated Dataset classes. I have a column  in the DB of type string. The column is bound to a textbox like this:Text='<%# Bind("Local") %>'  If the column has a value, thingts work well, but  When the column has a null value, I get the error: Conversion from type 'DBNull' to type 'String' is not validWhat can I do Please? Thanks a lot, I appreciate your taking the time to help me. ...

Conversion from type 'DBNull' to type 'Date' is not valid
I'm using VWD 2005 and have a grid on the page. In that grid, there'sa column that is of date/time type. Nulls are allowed in this column.I'm having problems when trying to edit this date column.I've converted the column to a template and changed theEditItemTemplate from a textbox to a calendar. If a date previouslyexisted for the record being edited, the update action works. If thedate was previously NULL and then a date is chosen via the calendar I get the error. How do I work around this? Charlie Here's my code line:<EditItemTemplate> <asp:Calendar ID="Calendar1" run...

Web resources about - Conversion from type 'DBNull' to type 'String' is not valid. - asp.net.getting-started

Resources last updated: 1/22/2016 6:42:45 PM