Unable to read NULL DateTime value
-
Here's my problem. I have a SQL db Table with a DateTime column. On my web app, I have a DataGrid that displays a list of info from that table (Order, Customer, Date of Delivery, etc..) When I read the DB, if the Date of Delivery doesn't exist I display nothing in the Cell. Alternately, When the order is delivered, the Date is displayed in that cell. The problem is this one: When there is no delivery date in the DB, the default on the DB is "01/01/1900 12:00AM". Now in my app, if I get the 01/01/1900 string for the Date, I don't display the value in the Cell. This was working fine until I installed the app and DB at the Customer. After running the app, the value for undelivered orders was displayed in the cell as 1/1/1900. So I changed my code again to check for the 01/01/1900 AND 1/1/1900 string and not display the value. This worked fine but I am not happy the way I check for the value of the date. After posting my problem in tan SQL forum, someone suggested to put the default value of that column to NULL, which is the logical thing to do. The only problem is that when I do that, I get an casting ERROR of my DateTime value (which is normal because NULL is not a Date value:zzz:) I'm reading from the DB. Here's my code to get the list of orders:sigh:: public static ArrayList GetAllOrders() { SqlDataReader reader = null; ArrayList orders = null; try { reader = SqlHelper.ExecuteReader(Resource.ConnectionString, CommandType.StoredProcedure, SP_GET_ORDERS, null); if (reader.HasRows) { orders = new ArrayList(); while (reader.Read()) { Order order = new Order(); order.WayBill = reader["WayBill"] as string; order.OrderDate = (DateTime)reader["OrderDate"]; order.Shipper.name = reader["Shipper"] as string; order.Items = (int)reader["Items"]; order.Weight = (int)reader["Weight"]; orders.Add(order); order = null; } } return orders; } This code reads a Stored Procedure where my order object has a OrderDate property casted to DateTime. Can anyone offer a suggestion/workaround for this problem.. Thanks a lot Pat
-
Here's my problem. I have a SQL db Table with a DateTime column. On my web app, I have a DataGrid that displays a list of info from that table (Order, Customer, Date of Delivery, etc..) When I read the DB, if the Date of Delivery doesn't exist I display nothing in the Cell. Alternately, When the order is delivered, the Date is displayed in that cell. The problem is this one: When there is no delivery date in the DB, the default on the DB is "01/01/1900 12:00AM". Now in my app, if I get the 01/01/1900 string for the Date, I don't display the value in the Cell. This was working fine until I installed the app and DB at the Customer. After running the app, the value for undelivered orders was displayed in the cell as 1/1/1900. So I changed my code again to check for the 01/01/1900 AND 1/1/1900 string and not display the value. This worked fine but I am not happy the way I check for the value of the date. After posting my problem in tan SQL forum, someone suggested to put the default value of that column to NULL, which is the logical thing to do. The only problem is that when I do that, I get an casting ERROR of my DateTime value (which is normal because NULL is not a Date value:zzz:) I'm reading from the DB. Here's my code to get the list of orders:sigh:: public static ArrayList GetAllOrders() { SqlDataReader reader = null; ArrayList orders = null; try { reader = SqlHelper.ExecuteReader(Resource.ConnectionString, CommandType.StoredProcedure, SP_GET_ORDERS, null); if (reader.HasRows) { orders = new ArrayList(); while (reader.Read()) { Order order = new Order(); order.WayBill = reader["WayBill"] as string; order.OrderDate = (DateTime)reader["OrderDate"]; order.Shipper.name = reader["Shipper"] as string; order.Items = (int)reader["Items"]; order.Weight = (int)reader["Weight"]; orders.Add(order); order = null; } } return orders; } This code reads a Stored Procedure where my order object has a OrderDate property casted to DateTime. Can anyone offer a suggestion/workaround for this problem.. Thanks a lot Pat
Call IsDBNull method to check for null values before getting the value. //Start of joke Never comment ur code. If it was hard to write, it should be hard to understand !!! //End of joke