Solved

Cannot get value because it is DBNull

Posted on 2013-12-16
10
396 Views
Last Modified: 2014-05-05
Obtained [InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.] when I tried to get the data for the column of the data set.

Public Class dsRegions
    Inherits DataSet

Public Property LOBCode() As String
            Get
                Try
                    Return CType(Me(Me.tabletblRegions.LOBCodeColumn), String)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set(ByVal Value As String)
                Me(Me.tabletblRegions.LOBCodeColumn) = Value
            End Set
        End Property

The following error was obtained:

Source File: C:\Inetpub\QSIRegister\dsRegions.vb    Line: 325

Stack Trace:

[InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.]
   Microsoft.VisualBasic.CompilerServices.StringType.FromObject(Object Value)
   ReadymixQSI_ASPNET.tblRegionsRow.get_LOBCode() in C:\Inetpub\QSIRegister\dsRegions.vb:325

[StrongTypingException: Cannot get value because it is DBNull.]
   ReadymixQSI_ASPNET.tblRegionsRow.get_LOBCode() in C:\Inetpub\QSIRegister\dsRegions.vb:327
   ReadymixQSI_ASPNET.QSIStage1.drpRegions_SelectedIndexChanged(Object sender, EventArgs e) in C:\Inetpub\QSIRegister\QSIStage1.aspx.vb:707
   ReadymixQSI_ASPNET.QSIStage1.Page_Load(Object sender, EventArgs e) in C:\Inetpub\QSIRegister\QSIStage1.aspx.vb:283
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()

I am not sure if the data set is loaded again after I included the new column.

Thanks
Shanthi
0
Comment
Question by:shanthi_joseph
  • 6
  • 4
10 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39722951
The usually means that the data you are trying to obtain has a null in that column. Try manually running the query against the database.

You have two options, one is to modify your query to never return null e.g. you would do something like isnull(MyColumn,'') as MyColumn. The second option is to retrieve the value into code as an 'object' and then test whether the object equals DBNull before you attempt to cast it to a string.

HTH
0
 

Author Comment

by:shanthi_joseph
ID: 39722989
The data set is not loaded again after the new column is added in the ASP .NET application. I restarted IIS and it still did not load the data for the new column.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39723000
That could only be the case if you are caching the dataset outside the application - are you doing this?

Otherwise your code isn't loading the dataset correctly, or your SQL is incorrect.

Unfortunately its not possible to give you further help with any of this based on the code you have posted.
0
 

Author Comment

by:shanthi_joseph
ID: 39723032
The data table appears to be  loaded in this class.

    Public Class tblRegionsDataTable
        Inherits DataTable

        Friend Sub New()
            MyBase.New("tblRegions")
            Me.InitClass()
        End Sub

>      readymixqsi_aspnet.dll!ReadymixQSI_ASPNET.dsRegions.tblRegionsDataTable.New() Line 150      Basic
       readymixqsi_aspnet.dll!ReadymixQSI_ASPNET.dsRegions.InitClass() Line 123 + 0x14 bytes      Basic
       readymixqsi_aspnet.dll!ReadymixQSI_ASPNET.dsRegions.New() Line 31 + 0xa bytes      Basic
       readymixqsi_aspnet.dll!ReadymixQSI_ASPNET.WebForm1.InitializeComponent() Line 28 + 0x16 bytes      Basic
       readymixqsi_aspnet.dll!ReadymixQSI_ASPNET.WebForm1.Page_Init(Object sender = {ASP.QSIMain_aspx}, System.EventArgs e = {System.EventArgs}) Line 845 + 0xa bytes      Basic


However it does not appear to have loaded a new column.  How do we verify if the database is cached?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39723050
The database won't be cached - you might be caching the dataset in code. In fact the component which you just showed the code for may cache the results internally.

When you say you've added a new column, where have you added this new column? And are you sure it has data in it? You obviously have to add it in the database, in the query against the database, and in the code where you want to use that data. To work out whats going wrong you need to carefully check each step. So...

1) Manually run the query against the database to be sure the data exists.

2) Debug the section where it queries the database and see what its returning and whether thats right or not.

Unfortunately its hard to help when we can't see most of the code.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:shanthi_joseph
ID: 39723083
The data exists when I run the query in the database. However, DBNull values are returned  in the function, Property LOBCode() As String, in the ASP .NET code. How do we clear the cache in the code?
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39723086
You have to work out whether you code is caching anything and if so how. Then you will know how to clear it.

Are you sure the code that fills the dataset is using the correct SQL?

Is that some kind of off-the-shelf component you are using? Or is it a component you have written yourself?
0
 

Author Comment

by:shanthi_joseph
ID: 39723109
The SQLDataAdapter was defined in another file that contained the SQL statements that needed to be modified.
Thanks for your help.
0
 

Accepted Solution

by:
shanthi_joseph earned 0 total points
ID: 40033829
I do not require any solution to this problem.
0
 

Author Closing Comment

by:shanthi_joseph
ID: 40041607
I do not require a solution to this problem.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now