Data conversion error

S

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
 
A

Alexey Smirnov

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=' said:
</asp:Calendar>

Hope it helps
 
S

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
 
A

Alexey Smirnov

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
 
S

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=' said:
</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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top