Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Typed DataSet provides strongly typed access to its containing tables and columns.
Dataset and Typed Dataset
ds.Tables["0"].Rows[0][0].ToString();
or
ds.Tables["Customers"].Rows[0]["Name"].ToString();
Advantages
• The difference between DataSet and Typed DataSet is, schema already present for the
Typed DataSet. Hence any mismatch in the column data type will generate Compile time
errors rather than Runtime errors as in the case of DataSet(normal one). In other words, it
provides strongly typed access to its containing tables and columns
Normal DataSet:
//Assign a value to the first column
dsEmp.Tables["EMPLOYEE"].Rows[0][0] = "12345";//This will generate runtime
error as empno column is integer
Typed DataSet:
dsEmp.EMPLOYEE[0].empno = "12345";//This will generate compile time error.
• Also accessing the column value is much easier than the normal dataset as the column
definition will be available in the schema.
Normal DataSet:
//Let us print first row and first column of the table
Console.Write(dsEmp.Tables["EMPLOYEE"].Rows[0][0].ToString());
Typed Dataset:
Console.Write(dsEmp.EMPLOYEE[0].empno.ToString());
Select Server name, select the database name and click OK.
6) Drag and drop the Employees table into the dataset designer to create the typed dataset from the
server explorer. The designer will look like figure 3.
Figure 3 – Dataset Designer
By default, it will have a TableAdapter (EmployeesTableAdapter here) with Fill() and GetData()
methods that can be used to fill and fetch data from the database without implementing a line of
code.
How to use it?
We can use the typed dataset and fill it in the normal way as we do for a non-typed dataset.
Filling Typed Dataset in a usual way
The table name in the fill method should match the table name in the typed dataset.
Using EmployeesTableAdapter:
Filling Typed Dataset using TableAdapter
Here, “gvEmployees” is the gridview. In the next sections we will see how to use TableAdapter
and extend the typed dataset to provide custom methods that serve specific purposes. We use the
“customer” table in NorthWind database for this article.
What is a TableAdapter?
TableAdapters provide communication between your application and a database. More specifically,
a TableAdapter connects to a database, executes queries or stored procedures, and either returns a
new data table populated with the returned data or fills an existing DataTable with the returned
data. TableAdapters are also used to send updated data from your application back to the database.
Adding New Functionality
We will try to add a new Selection functionality and a new update functionality using
TableAdapter in Typed dataset using visual studio.
I would like to return details of a customer on giving the customerid in this example so I have
given the query:
Select * from Customers where CustomerID = @CustomerID.
Click Finish. We have just added a select functionality to the typed dataset using TableAdapter
writing a single line of code.
Now we can see the new select and update functionality with the insert, update and delete method
in TableAdapter intellisense.
Visual studio intellisense for TableAdapter
In the next section we will construct a custom typed dataset using visual studio instead of dragging
and dropping from server explorer.
3) To specify datatype, click the properties of the corresponding column and specify it.
So the final typed dataset will look like
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.
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.
Get a column value
BO.Products.ProductsDataTable prod = new BO.Products.ProductsDataTable();
if(!prod[0].IsUnitPriceNull())
{
decimal price = prod[0].UnitPrice
}