Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null Date in VB.NET 2005 Gridview

Status
Not open for further replies.

jonbatts

Programmer
Apr 12, 2005
114
0
0
US
OK, I've got a Gridview that contains Users and their info; FirstName, LastName, .... It also contains "Last Login", a date. When "Last Login" is null, "1/1/0001 12:00:00 AM" is showing up. I understand that this is happening because a VB date can never really be "Nothing", but is instead set to "1/1/0001 12:00:00 AM". Typically I would just pass a string instead of a date, but I'm using a data layer with a "UserCollection" class like so.
Code:
Dim users As New UserCollection()
users.GetUsers()
grdUsers.DataSource = users
grdUsers.DataBind()
Any thoughts? Thanks, and have a great day.
 
Hi,
If you are using SQL Server 2005, you'll have another "gotcha", because 01/01/0001 is invalid in SQL Server 2005. In SQL Server 2005, the minimum valid date value is 01/01/1753.

Don't ask me why, I'm just the messenger!!!

In any event, I'm using a initialized date of 01/01/1900. When I'm checking to see if someone entered a valid date, such as 4/27/2007, I'd first check to see if the value is 01/01/1900.

HTH, Randy
 
Just go to your query if it is for example

Select theDate from SomeTable

change to:

Select IsNull(theDate,'') from SomeTable

then you wont have to change your code and it will give you a null value from the query.
 
As I mentioned, it not as simple as changing a query because I'm using a datalayer of which the property for "LastLogin" is a date. Thus, in VB, A Date must be a date. Not an empty string, and not null ("Nothing"). I do appreciate your time and thoughts though.
One thing I thought about was adding a property to my User Object "LastLoginAsString", but to me that seems REALLY sloppy. Any other ideas?
 
You could check the value on databind like this:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'Here you can change the value that gets displayed ie if it is '1/1/1900'
e.Row.Cells(0).Text
End Sub
 
While I wish there was a better way than having to check each row on RowDataBound, it will suffice. However, is there a more efficient way to check the cell that I want than referencing it by index. For instance, is there a "FindCellByName"? I hate to use index numbers to cells, because then i have to change the index number if I change the columns in the table. Thanks again, and have a great night.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top