Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 14th, 2009, 10:11 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
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
  #2 (permalink)  
Old March 14th, 2009, 10:20 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
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.
================================================== =========
  #3 (permalink)  
Old March 14th, 2009, 11:21 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
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.
  #4 (permalink)  
Old March 15th, 2009, 07:51 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
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)
  #5 (permalink)  
Old March 15th, 2009, 05:00 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:21 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.