What is the best way to write out three records when an exception occurs

T

Tony Johansson

Hello!

In this database sales.sdf there is some record and some columns might also
have null values.
As the program is written now will the foreach loop be terminated as soon as
an exception occurs.
In this case the exception that occurs is StrongTypingException.

So if I want to write out every record in the database even if some dbvalue
is null what is the best solution.?
What I can do is to do something like this
string s = row["ShipRegion"].ToString();
if (s != "")
Console.WriteLine("ShipRegion = {0}", row.ShipRegion);

for every columns but then I loose much of the point to have strongly types
dataset

static void Main(string[] args)
{
SalesDataSet ds = new SalesDataSet();
string conStr = @"Data Source=|DataDirectory|\Sales.sdf";
SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Orders", new
SqlCeConnection(conStr));
SqlCeDataAdapter adapt = new SqlCeDataAdapter(cmd);
adapt.Fill(ds, "Orders");


try
{
foreach (SalesDataSet.OrdersRow row in ds.Orders.Rows)
{
Console.WriteLine("OrderID = {0}", row.OrderID);
Console.WriteLine("CustomerID = {0}", row.CustomerID);
Console.WriteLine("EmployeeID = {0}", row.EmployeeID);
Console.WriteLine("OrderDate = {0}", row.OrderDate);
Console.WriteLine("RequiredDate = {0}", row.RequiredDate);
Console.WriteLine("ShipperDate = {0}", row.ShippedDate);
Console.WriteLine("ShipVia = {0}", row.ShipVia);
Console.WriteLine("Freight = {0}", row.Freight);
Console.WriteLine("ShipName = {0}", row.ShipName);
Console.WriteLine("ShipAddress = {0}", row.ShipAddress);
Console.WriteLine("ShipCity = {0}", row.ShipCity);
Console.WriteLine("ShipRegion = {0}", row.ShipRegion);
Console.WriteLine("ShipPostalCode = {0}",
row.ShipPostalCode);
Console.WriteLine("ShipCountry = {0}", row.ShipCountry);
Console.WriteLine("==================================");
}
}
catch (StrongTypingException ex)
{
Console.WriteLine("Note: {0}", ex.Message);
}
}

//Tony
 
A

Arne Vajhøj

In this database sales.sdf there is some record and some columns might also
have null values.
As the program is written now will the foreach loop be terminated as soon as
an exception occurs.
In this case the exception that occurs is StrongTypingException.

So if I want to write out every record in the database even if some dbvalue
is null what is the best solution.?
What I can do is to do something like this
string s = row["ShipRegion"].ToString();
if (s != "")
Console.WriteLine("ShipRegion = {0}", row.ShipRegion);

for every columns but then I loose much of the point to have strongly types
dataset

static void Main(string[] args)
{
SalesDataSet ds = new SalesDataSet();
string conStr = @"Data Source=|DataDirectory|\Sales.sdf";
SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Orders", new
SqlCeConnection(conStr));
SqlCeDataAdapter adapt = new SqlCeDataAdapter(cmd);
adapt.Fill(ds, "Orders");


try
{
foreach (SalesDataSet.OrdersRow row in ds.Orders.Rows)
{
Console.WriteLine("OrderID = {0}", row.OrderID);
Console.WriteLine("CustomerID = {0}", row.CustomerID);
Console.WriteLine("EmployeeID = {0}", row.EmployeeID);
Console.WriteLine("OrderDate = {0}", row.OrderDate);
Console.WriteLine("RequiredDate = {0}", row.RequiredDate);
Console.WriteLine("ShipperDate = {0}", row.ShippedDate);
Console.WriteLine("ShipVia = {0}", row.ShipVia);
Console.WriteLine("Freight = {0}", row.Freight);
Console.WriteLine("ShipName = {0}", row.ShipName);
Console.WriteLine("ShipAddress = {0}", row.ShipAddress);
Console.WriteLine("ShipCity = {0}", row.ShipCity);
Console.WriteLine("ShipRegion = {0}", row.ShipRegion);
Console.WriteLine("ShipPostalCode = {0}",
row.ShipPostalCode);
Console.WriteLine("ShipCountry = {0}", row.ShipCountry);
Console.WriteLine("==================================");
}
}
catch (StrongTypingException ex)
{
Console.WriteLine("Note: {0}", ex.Message);
}
}

If you were using a simple DataReader then:

rdr.IsDBNull(i) ? "This is NULL" : rdr

would do.

DataSet is another animal.

But doesn't SalesDataSet.OrdersRowhave some methods that
can be used to detect?

Arne
 
T

Tony Johansson

Hello!
But doesn't SalesDataSet.OrdersRowhave some methods that
can be used to detect?

Arne

Yes you were right there were a method for each column to check if this
column is null.
For the column ShippedDate the method was called IsShippedDateNull
//Tony
 
Top