Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2009, 10:11 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Problems with typed dataset.

Hi
I have some columns in my table that have null values in them. I am trying to grabs the rows that may or may not have null values in the columns(right now testing with nulls in them).
So I am getting a error when I am trying to retreive a column that has null in it. So It tried to check for dbNull but I still get an error.
When I have this

Code:
ArrayList gridViewRow = new ArrayList();
           for (int i = 0; i < gradesDT.Count; i++)
           {

              
              
               Planner.GradesRow row = gradesDT[i];
               string[] gridViewCells = new string[5];
               
           
               
               gridViewCells[0] = row.AssignmentName;
               gridViewCells[1] = row.Type;
               gridViewCells[2] = row.AchievedMark.ToString();// can have null                gridViewCells[3] = row.TotalMark.ToString(); //can have null               gridViewCells[4] = row.AssignmentWeight.ToString(); // can have null                gridViewCells[5] = row.AchievedWeight.ToString(); // can have null               gridViewRow.Add(gridViewCells);
              
           }
Code:
So I get this error System.Data.StrongTypingException was unhandled by user code
  Message="The value for column 'AchievedMark' in table 'Grades' is DBNull."
  Source="App_Code.3vxi8no6"
  StackTrace:
       at Planner.GradesRow.get_AchievedMark() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\planner\fce908b7\a9a33e47\App_Code.3vxi8no6.4.cs:line 2182
       at GetCoursesByGrade.ProcessRequest(HttpContext context) in i:\StudentPlanner\Planner\AJAX\Planner\GetCoursesByGrade.ashx:line 41
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
  InnerException: System.InvalidCastException
       Message="Specified cast is not valid."
       Source="App_Code.3vxi8no6"
       StackTrace:
            at Planner.GradesRow.get_AchievedMark() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\planner\fce908b7\a9a33e47\App_Code.3vxi8no6.4.cs:line 2179
       InnerException:
Now I put an if statement checking for dbNullsuch as if (Convert.IsDBNull(row.AchievedMark))
{
string test = null;
}

Still get the same error. I am using a strong typed dataset. So I am not sure how to fix this. The first thing I tried to do is change the action to happen when a Database Null is found(to use a null value and not throw an expecption) But my dataset won't let me. When I change NullValue from expection to Null I get this"The value entered is not valid for the current data type." All the rows that have nulls in them are all decimals(5,2); Thanks
 
Old March 14th, 2009, 10:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

why not do something like this?

Code:
if (row.field != dbnull.value)
gridviewcell[n] = row.field;
else
gridviewcell[n] = "";
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old March 14th, 2009, 11:21 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

I get this error if I do that.

Error 18 Operator '==' cannot be applied to operands of type 'System.DBNull' and 'decimal' i:\StudentPlanner\Planner\AJAX\Planner\GetCoursesB yGrade.ashx 42 21 I:\StudentPlanner\Planner\


So I would have to convert it to something different and since that row contains and exception I probably will keep getting the same error.
 
Old March 15th, 2009, 07:51 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

DBNull does not implement the == and != operators, although can use DBNull.Value.Equals(object). The preferred method is Convert.IsDBNull(object) as chobo used.

For strongly typed DataSets however, this is not the correct approach, as you will get these errors. When you create the DataSet, the rows will create an IsFieldNull() method for each nullable column. So for AchievedMark the row will have a method bool IsAchievedMarkNull(). For each field then you could do

c# Code:
gridViewCells[2] = row.IsAchievedMarkNull() ? String.Empty : row.AchievedMark.ToString();

The other option is to use the untyped row.IsNull("AchievedMark"), but that kinda defeats the point of creating a typed DataSet.

HTH
Phil

PS. Could you possibly add line breaks to the last line of code, so it doesn't stretch off screen and make things hard to read. Thanks

Last edited by philip_cole; March 15th, 2009 at 08:25 AM.. Reason: Corrected highlight lang
The Following User Says Thank You to philip_cole For This Useful Post:
chobo2 (March 15th, 2009)
 
Old March 15th, 2009, 05:00 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Upon initial testing this seems to work . I been reading online that the only type in a typed dataset that can return null or empty is the string type. The rest all throw exceptions.

I hear that Microsoft has known about this issue for quite some time. I hope they one day fix it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create new row using a typed dataset humour ASP.NET 2.0 Professional 0 May 9th, 2008 09:49 PM
insert nulls using a typed dataset humour ASP.NET 2.0 Professional 3 April 16th, 2008 05:17 AM
Converting a untyped dataset to a typed dataset daphnean Visual Studio 2005 0 July 13th, 2006 01:16 AM
What Are Typed Dataset vinod_pawar1 ADO.NET 2 May 13th, 2005 04:52 AM
Dynamic DataAdapter from Typed Dataset dhay1999 ADO.NET 3 May 3rd, 2005 07:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.