Typed Dataset and its Usages
page 7 of 10
by Satheesh Babu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 44842/ 77

Handling Nulls

The database table can contain columns that allow null to be saved. In the same way we can make the typed dataset column to allow nulls by setting the Columns property: AllowDBNull = true;

Unfortunately, typed dataset does not allow Nullable types to be specified for a column. So we cannot specify nulls to the columns directly, instead we should use the methods that is generated to set nulls.

How to set Nulls?

The below table gives us a picture about the method it will generate for setting and checking nulls.

Column Name

Setting Null

Checking Null

UnitPrice

SetUnitPriceNull()

IsUnitPriceNull()

Total

SetTotalNull()

IsTotalNull()

 

.NetFramework adds methods that will set Null to a column without the user setting it. So to set null for a column named “UnitPrice” the method will be SetUnitPriceNull(). So the below line will set null for unitprice column if its has null value.

Listing 9: Set a column value

if (txtUnitPrice.Text != "")
{
  ProRow.UnitPrice = decimal.Parse(txtUnitPrice.Text);
}
else
{
  ProRow.SetUnitPriceNull();
}

In the same way, to check whether the column has a null value the method is ProRow.IsUnitPriceNull(); .

The above line will return either true or false depending on the value. On calling the Add() method the constructed row can be added to the dataset:  

prod.Rows.Add(ProRow); .

While accessing a value in typed dataset, we should check for null before accessing it. Because there can be an integer column in database that can allow nulls, accessing such column with null values will throw an exception. We can understand this when we select null for NullValue property for a primitive datatype in property box of a column which will throw an error “Property value is not valid.”

So the default value will be an exception for Null value in a column that is of a primitive type. Null values can be set to the columns of type string. To access the “UnitPrice” column that allows null in database, the code should be the following.

Listing 10: Get a column value

BO.Products.ProductsDataTable prod = new BO.Products.ProductsDataTable();
if(!prod[0].IsUnitPriceNull())
 {        
decimal price = prod[0].UnitPrice
 }

Now we have constructed a typed dataset programmatically.


View Entire Article

User Comments

Title: Why good for only very small projects?   
Name: Mark
Date: 2010-11-14 5:14:00 PM
Comment:
Why are Typed DataSets viable only if our project sizes are really small? How about a hybrid: BOs in the middle and UI tiers; Typed DataSets at the DataLayer side--Resolving data-changes from BOs to DataSets?
Title: I need urgent help plz   
Name: Fadi
Date: 2010-06-13 2:10:47 PM
Comment:
hi there, i am using typed dataset for my rdlc reports in my winforms app with C#. i m inserting data in database form different forms. the problem is, on run time when i insert data in the database its not updated data in rdlc reports, to get new data in my reports i need to close my app and debug it again to see new data in the reports. Is there any way to forcedly refresh data in my typed dataset on runtime???? Looking forward from your positive response.

Fadi
Title: developer   
Name: John Jones
Date: 2009-08-27 5:19:58 PM
Comment:
One point I forgot to mention:
Our main goal is to use the same SQL queries (basic inserts, updates, deletes) in both MS Access and SQL Server. Our app was originally written for Access using the VS designer to generate the queries. After testing these queries, we found that if the ` char was removed and replaced by [ ] brackets, then all the queries would work in both db's.
Title: developer   
Name: john
Date: 2009-08-26 12:46:38 PM
Comment:
We have a DataSet with Table Adapters built for MSAccess using OLEDB. We are using Visual Studio 2005 and .Net 2.0.

When the designer creates insert, update and delete queries it encloses the table name and field names with a ` (ASCII char 96).
For example:

INSERT INTO `table` (`col1`, `col2`, `col3`) values (....)

Is there a reason for this character? Assuming it's for escape purposes, is there a setting to make Visual Studio use another character, eg "[" or "]"?
Title: sir   
Name: jim
Date: 2008-12-15 8:18:50 PM
Comment:
where is the method SetUnitPriceNull found - not in this sample

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 3:08:43 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search