views:

784

answers:

2

Hi All,

My SQL database has a a column in a table that has the datatype of datetime and allows nulls (because I need it to either be a datetime value or blank) and I need to be able to read this value and then display it in a textbox. The problem is that on inserting and updating, the autogenerated tableadapter (when dragged from the server explorer to the dataset designer) sets all columns that are datetimes to automatically throw exceptions if a null is attempted to be input and it is not possible to change this property. Though this column is able to take nulls in the database.

A: 

try using nullable datetime value:

VB.net

 Dim myDate As System.Nullable(Of DateTime) = Nothing

C#:

DateTime? date = null;

now use myDate or date to assign to column.

EDIT:- Suppose you are using table adapters for Northwind database. Then to add new order you can use it as follows:

orderAdapter.InsertOrder(CustomerID,EmployeeID,
        date, ////Use nullable date here
        RequiredDate,ShippedDate,ShipVia,Freight,ShipName,
        ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry);
TheVillageIdiot
But how does he do that in the TableAdapter?
John Saunders
Ok, so what you're saying is he can't set that column in a DataRow to null, since he can't make the column DateTime? and can't change the NullValue property?
John Saunders
A: 

Hi,

This article suggests you need to use a nullable version of the DateTime variable as a normal DateTime variable cannot contain nulls.

AdaTheDev