By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

Data conversion error

P: n/a
SAL
I'm sorry if this has been answered before but I didn't see it in a quick
scan of the list.
The following code is causing an error when the field is null:

Error text:
Conversion from type 'DBNull' to type 'Date' is not valid.

<asp:Calendar ID="Calendar2" runat="server" SelectedDate='<%#
Bind("DateReceived") %>'
VisibleDate='<%# Eval("DateReceived") %>'></asp:Calendar>

The control is a DetailsView and it's set up to enable editing.

How can I fix this. I new to asp.net and IIF doesn't seem to work.

Thanks
S
May 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On May 8, 12:39 am, "SAL" <S...@NoNo.comwrote:
I'm sorry if this has been answered before but I didn't see it in a quick
scan of the list.
The following code is causing an error when the field is null:

Error text:
Conversion from type 'DBNull' to type 'Date' is not valid.

<asp:Calendar ID="Calendar2" runat="server" SelectedDate='<%#
Bind("DateReceived") %>'
VisibleDate='<%# Eval("DateReceived") %>'></asp:Calendar>

The control is a DetailsView and it's set up to enable editing.

How can I fix this. I new to asp.net and IIF doesn't seem to work.
Your DateReceived is NULL and SelectedDate/VisibleDate supposed to
have a value. So, you should decide what date has to be selected when
there is no DateReceived returned and tell it the Calendar Control.

For example, you can show current date

In this case:

<asp:Calendar ID="Calendar2" runat="server"
SelectedDate='<%# IIf(IsDBNull(Bind("DateReceived")), DateTime.Now,
Bind("DateReceived")) %>'
VisibleDate='<%# IIf(IsDBNull(Bind("DateReceived")), DateTime.Now,
Bind("DateReceived")) %>'
></asp:Calendar>
or you can create a function

Function MyDate(ByVal o As Object) As DateTime
If o Is DBNull.Value Then
Return DateTime.Now
Else
Return o
End If
End Function

and use it in Calendar Control

<asp:Calendar ID="Calendar2" runat="server"
SelectedDate='<%# MyDate(Bind("DateReceived")) %>'
VisibleDate='<%# MyDate(Bind("DateReceived")) %>'
></asp:Calendar>
Hope it helps

May 8 '07 #2

P: n/a
SAL
Alexey,
thanks for the response and sorry for my delay. I was off work yesterday.

I tried wrapping it in a function and when I do that I get a compile error
that says the Bind variable has not been declared. That confuses the heck
out of me because when I wrap the Eval statement, I don't get a compile
error that say the Eval variable hasn't been declared. Any knowledge on
this?

Thanks
S

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On May 8, 12:39 am, "SAL" <S...@NoNo.comwrote:
>I'm sorry if this has been answered before but I didn't see it in a quick
scan of the list.
The following code is causing an error when the field is null:

Error text:
Conversion from type 'DBNull' to type 'Date' is not valid.

<asp:Calendar ID="Calendar2" runat="server" SelectedDate='<%#
Bind("DateReceived") %>'
VisibleDate='<%# Eval("DateReceived") %>'></asp:Calendar>

The control is a DetailsView and it's set up to enable editing.

How can I fix this. I new to asp.net and IIF doesn't seem to work.

Your DateReceived is NULL and SelectedDate/VisibleDate supposed to
have a value. So, you should decide what date has to be selected when
there is no DateReceived returned and tell it the Calendar Control.

For example, you can show current date

In this case:

<asp:Calendar ID="Calendar2" runat="server"
SelectedDate='<%# IIf(IsDBNull(Bind("DateReceived")), DateTime.Now,
Bind("DateReceived")) %>'
VisibleDate='<%# IIf(IsDBNull(Bind("DateReceived")), DateTime.Now,
Bind("DateReceived")) %>'
>></asp:Calendar>

or you can create a function

Function MyDate(ByVal o As Object) As DateTime
If o Is DBNull.Value Then
Return DateTime.Now
Else
Return o
End If
End Function

and use it in Calendar Control

<asp:Calendar ID="Calendar2" runat="server"
SelectedDate='<%# MyDate(Bind("DateReceived")) %>'
VisibleDate='<%# MyDate(Bind("DateReceived")) %>'
>></asp:Calendar>

Hope it helps

May 9 '07 #3

P: n/a
On May 9, 5:59 pm, "SAL" <S...@NoNo.comwrote:
Alexey,
thanks for the response and sorry for my delay. I was off work yesterday.

I tried wrapping it in a function and when I do that I get a compile error
that says the Bind variable has not been declared. That confuses the heck
out of me because when I wrap the Eval statement, I don't get a compile
error that say the Eval variable hasn't been declared. Any knowledge on
this?
Please post the code of the datasource control/object, I will have a
look. There are some differences between Eval() and Bind(). The Eval
function is used to define one-way (read-only) binding. The Bind
function is used for two-way (updatable) binding.

Here's complete overview:
http://msdn2.microsoft.com/en-us/library/ms178366.aspx

May 9 '07 #4

P: n/a
SAL
It seems the problem is when there's a null value in this field however,
which is completely expected. There are many date fields that could be null
before editing a record.

Why the following compile error occurs when I wrap the Bind("DateReceived")
statement in the FixNullDate function I just don't get.
Here's the HTML for the EditItemTemplate for the DateReceived field in the
DetailsView:

<EditItemTemplate>
<asp:Calendar ID="Calendar2" runat="server" SelectedDate='<%#
Bind("DateReceived") %>'
VisibleDate='<%# FixNullDate(Eval("DateReceived")) %>'
></asp:Calendar>
</EditItemTemplate>

Here's the code for the FixNullDate function in the code behind page:
Function FixNullDate(ByVal dt As Object) As Date
If IsDBNull(dt) Then
Return Nothing
Else
Return dt
End If
End Function
This is the code in the business logic layer for updating a row:

Public Function UpdateAnnexation(ByVal annexationId As Integer, _
ByVal taxDistrictEntityId As Nullable(Of
Integer), _
ByVal dateReceived As Nullable(Of Date),
_
ByVal name As String, _
ByVal estimatedTaxImpacts As Nullable(Of
Decimal), _
ByVal estimatedAssessedValue As
Nullable(Of Decimal), _
ByVal acreage As Nullable(Of Single), _
ByVal gsReview As Nullable(Of DateTime),
_
ByVal legalOkay1 As Boolean, _
ByVal cityAnx As Nullable(Of DateTime),
_
ByVal statuteId As Nullable(Of Integer),
_
ByVal statuteRequirementsMet As Boolean,
_
ByVal ordinanceId As Nullable(Of
DateTime), _
ByVal legalOkay2 As Boolean, _
ByVal updateCities As Nullable(Of
DateTime), _
ByVal toLevySpecialist As Nullable(Of
DateTime), _
ByVal levySpecialistOkay As Boolean, _
ByVal updateCityAnx As Nullable(Of
DateTime), _
ByVal packetsSent As Nullable(Of
DateTime), _
ByVal ordinanceIn As Nullable(Of
DateTime), _
ByVal fromLevySpecialist As Nullable(Of
DateTime)) As Boolean
Adapter.UpdateAnnexation(taxDistrictEntityId, dateReceived, name,
estimatedTaxImpacts, _
acreage, estimatedAssessedValue, gsReview, legalOkay1, cityAnx,
statuteId, _
statuteRequirementsMet, ordinanceIn, legalOkay2, updateCities,
toLevySpecialist, _
levySpecialistOkay, fromLevySpecialist, updateCityAnx, packetsSent,
annexationId)
End Function
The code in the BLL for GetAnnexationByAnnexationID:

Public Function GetAnnexationByAnnexationId(ByVal annexationId As
Integer) As Annexationds.AnnexationsDataTable
Return Adapter.GetAnnexationByAnnexationId(annexationId)
End Function
And, here's the dataset queries:

To GetAnnexationByAnnexationID:

SELECT Acreage, AnnexationId, CityAnx, DateReceived,
EstimatedAssessedValue, EstimatedTaxImpacts, FromLevySpecialist, GSReview,
LegalOkay1, LegalOkay2, LevySpecialistOkay, Name, OrdinanceIn, PacketsSent,
StatuteId, StatuteRequirementsMet, TaxDistrictEntityId, ToLevySpecialist,
UpdateCities, UpdateCityAnx FROM Annexations WHERE (AnnexationId =
@AnnexationId)

And to UpdateAnnexation:

UPDATE Annexations
SET TaxDistrictEntityId = @TaxDistrictEntityId, DateReceived =
@DateReceived, Name = @Name, EstimatedTaxImpacts = @EstimatedTaxImpacts,
Acreage = @Acreage, EstimatedAssessedValue =
@EstimatedAssessedValue, GSReview = @GSReview, LegalOkay1 = @LegalOkay1,
CityAnx = @CityAnx, StatuteId = @StatuteId,
StatuteRequirementsMet = @StatuteRequirementsMet, OrdinanceIn =
@OrdinanceIn,
LegalOkay2 = @LegalOkay2, UpdateCities =
@UpdateCities, ToLevySpecialist = @ToLevySpecialist, LevySpecialistOkay =
@LevySpecialistOkay,
FromLevySpecialist = @FromLevySpecialist,
UpdateCityAnx = @UpdateCityAnx, PacketsSent = @PacketsSent
WHERE AnnexationId = @AnnexationId

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
On May 9, 5:59 pm, "SAL" <S...@NoNo.comwrote:
>Alexey,
thanks for the response and sorry for my delay. I was off work yesterday.

I tried wrapping it in a function and when I do that I get a compile
error
that says the Bind variable has not been declared. That confuses the heck
out of me because when I wrap the Eval statement, I don't get a compile
error that say the Eval variable hasn't been declared. Any knowledge on
this?

Please post the code of the datasource control/object, I will have a
look. There are some differences between Eval() and Bind(). The Eval
function is used to define one-way (read-only) binding. The Bind
function is used for two-way (updatable) binding.

Here's complete overview:
http://msdn2.microsoft.com/en-us/library/ms178366.aspx

May 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.