Sunday, February 12, 2012

ASP.NET C# Calendar DataAccess Issue

Hello thereSmile

I'm having a little issue with Updating a SQL Table. *Deep Breath* OK, lets begin from the start...Confused

I Have a Table Called Job. I've an ASP Page setup to insert a new record into the Job table via a DetailsView Control with an SQLDataSource bound to it. This table has many columns in it, 4 of which are DateTime columns. I use a calendar Control from the toolbox for the DateTime selection and bound all the Calender control's SelectedDate to: SelectedDate='<%# Bind('EnteredDate') %>' using the EnteredDate field as my example.

I only insert DateTime Values into 2 of the 4 DateTime columns in the original INSERT, so the remaining 2 columns are NULL. I have an UPDATE setup on a different page to edit the previously inserted rows in job using another SQLDataSource again using the bind method for calendars: '<%# Bind('RevisedDate') %>'

My Problem is (according to this post I found onlinehttp://www.msdner.com/forum/thread529052.html ) the page crashes with "Specified cast is not valid"
. The reason being I'm trying to bind to the remaining 2 NULL DateTime columns in an attempt to update them with a value my user will not be able to insert in the original INSERT. The solution Code I found on the link above is in VisualBasic and I have had little luck in trying to convert it into C# to even see if it solves my problem:

VB Code:

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

ASP Code:


<asp:calendar id="calEditRevisedDate" runat="server"
selecteddaystyle-backcolor="red"
selecteddate='<%#FixDBNull(eval("RevisedDate")) %>'
visibledate='<%# FixDBNull( eval("RevisedDate")) %>'
selectorstyle-backcolor="Green">
</asp:calendar>

Any suggestions or work arounds would be greatly appreciatedSmile

Thank You

Hi Hornwook509,

Do you want to convert that vb code to C#? If yes, try the code below

protected DateTime FixDBNull(DateTime inVal){if(inVal==DBNull.value)return DateTime.Now;else return inVal;}
|||

Thank you for the replySmile

I get the following compile error with the code you supplied... "Operator '==' cannot be applied to operands of type 'System.DateTime' and 'System.DBNull' "

The Null DB Field is quite an annoying problem. As a means of overstepping this frustrating issue I have resorted to using 3rd party software that handles the NULL DB Date Entry http://www.basicdatepicker.com/download/. I'm just running low on time trying to sort this issue outSad

The free version (BDP Lite) of this software (as far as i can tell) does not handle the the Null DateTime column problem, but the Registered full version of basic Date Time Picker does.

|||

forget about the dbnull issue. Try inVal==null instead. I think it should work

|||

I managed to sort my problem out. Found this link onlinehttp://www.syncfusion.com/FAQ/aspnet/WEB_c8c.aspx

This bit of the page in particular

using System.Data.SqlTypes;

if (txtDate.Text == "")

{

cmd.Parameters ["@.Date"].Value =sqldatenull ;

//cmd.Parameters["@.Date"].Value = DBNull.Value;

}

else

{

cmd.Parameters["@.Date"].Value = DateTime.Parse(txtDate.Text);

}

Hope this helps anybody who is having Null DateTime issuesSmile

No comments:

Post a Comment