C# - ASP.NET - Calendar Control - Specified cast is not valid - asp.net

I have a gridview where I keep track of deadlines. I have 2 dates herefor. A expected date (date where the deadline is supposed to be completed) and an actual date (date where the deadline was completed). Now in my DB all the expected dates have values because they are meant to finish at around the same date each month. However the actual date is blank.
This gives me following error due to the null-values: Exception Details: System.InvalidCastException: Specified cast is not valid.
Now I found something while looking on the Internet and that is to call a helper method (see the selectedDate property - I copied to same value of selectedDate into VisibleDate, because else I had the same error) However this presents me with another error which is that I can't get the newly selected value!
<asp:TemplateField HeaderText="Actual Date" SortExpression="Actual_Date">
<EditItemTemplate>
<asp:Calendar ID="Calendar2" runat="server" VisibleDate='<%# Bind("Actual_Date") %>' SelectedDate='<%# FixNullDate(Eval("Actual_Date")) %>'></asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("Actual_Date", "{0:d}") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Here is my code of from my templatefield for the actual date
Please help, I'm kinda lost :p

You need to check for null:
<%# string.IsNullOrEmpty(Bind("Actual_Date").ToString()) ? "0000-00-00 00:00:00.000": Bind("Actual_Date").ToString() %>

Related

error message "String was not recognized as a valid Boolean" in aspx page

I have a SQL stored proc where I am creating a column ("Certified") dynamically based on two other columns. The value from this column is a '0' or '1'. The SQL stored proc query is:
, CASE WHEN
(StartMiles < EndMiles)
AND (StartTime < EndTime)
AND (bcd.Status != 'C')
THEN '1' ELSE '0' END
AS Certified
On the front end in my aspx page, I have a telerik radgrid that will display a checkbox (enabled if value is 1, disabled if value is 0). The aspx code is:
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# !bool.Parse(Eval("Certified").ToString()) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I am getting an error on the aspx page String was not recognized as a valid Boolean
To resolve the error, how can I set a datatype in the stored proc?
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# !Convert.ToBoolean(Convert.ToInt32(Eval("Certified").ToString())) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I would recommend using a code-behind method to do this instead of putting logic into the markup via embedded code blocks, like this:
Markup:
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified"
Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# IsCertified(Eval("Certified").ToString()) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
Code-behind:
protected bool IsCertified(string certifiedValue)
{
return !bool.Parse(certifiedValue);
}
Note: To be safer, I would recommend using the Boolean.TryParse() method instead of just the Parse(), as that will eliminate the chance of a string that cannot be parsed into a bool from throwing an exception. Read Boolean.TryParse Method documentation for more information.
This provides two advantages, in my opinion, over the OP code:
Simplified markup, because you do not have conditional logic in the markup, but now just a call to the method.
You can leverage the power of Visual Studio's IntelliSense, compiler to catch syntax errors at compile-time instead of run-time and the debugger itself.

asp.net gridview bind dateformat not working with update

I have a GridView with a TemplateField column which shows a DateTime from a DataSource.
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Start Date">
<EditItemTemplate>
<asp:TextBox ID="txtDateStart" runat="server"
Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("dtDateStart", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Displaying the date in the correct format works as it should. Note that the format starts with DAY followed by MONTH.
When I switch to edit mode, change the date in the TextBox to '31-01-2013' and press the GridView's update-link i get an error:
Cannot convert value of parameter 'dtDateStart' from 'System.String' to 'System.DateTime'
The error is generated by the GridView not my own code.
It happens before the UpdateMethod of my DataSource is called.
When i type '01-31-2012' the data is processed correctly and the value is updated into the database.
Somehow when the date is displayed it uses format dd-MM-yyyy (just as I need it to)
But when it reads the new value form the TextBox it uses MM-dd-yyyy
Can somebody please help me?
thanks for your reply.
I do not know about culture settings within the website. I was assuming that proving the Bind-function with a dateformat would be sufficient.
By now I have implemented the following workaround, which is hopefully useful for others:
I have added an additional property to my class which is of type string. This property I only use for binding to the gridview.
// Original Date property //
public DateTime dtDateStart { get; set; }
// Additional Date property of type string //
public string sDateStart
{
get
{
return dtDateStart.ToString("dd/MM/yyyy");
}
set
{
dtDateStart = DateTime.ParseExact(value, "dd/MM/yyyy", null);
}
}
I am not sure if this should be marked as the answer. But maybe others can use this as a quick workaround as well.
I think you can use like this
<EditItemTemplate>
<asp:TextBox ID="txtDateStart" runat="server"
Text='<%# Convert.ToDateTime(Bind("dtDateStart")).ToString("dd/MM/yyyy") %>'</asp:TextBox>
</EditItemTemplate>
this weill help you.

getting error expression expected in asp.net, when trying to check radiobutton using db value

There are 4 readiobutton inside the repeater and i'm trying to show the checked radiobutton from database value.
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IIF(Eval("ANSWER")==1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
Second approach
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# Eval("ANSWER")==1 ? true : false %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
and so on for the rest radiobutton. But, it showing the error Expression Expected error. Need help. !!
It looks like you've got your C# and VB.Net mixed. Your first example looks like VB, the second like C#. However, you've got a few problems in your VB implementation:
The equality operator in VB is =, not ==
You should use the IF operator, not the IIF function, which is obsolete
The correct code should be as follows:
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IF(Eval("ANSWER")=1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />

Getting day of week when binding Date object to GridView Label (DateStringFormat)

I want to get the Day of week out of a Date object that im binding to a gridview label like this:
<asp:Label ID="ResourceAvailabilityDayLabel" runat="server" Text='<%# Bind("Date", "{0:d}") %>'></asp:Label>
{0:d} shows only the day in numbers if the date was 11-4-2012 it will post 11. Is there a way to have this done during binding? I cant find the correct DateStringFormat.
Edit
{0:dddd} fixes the problem.
To get day of the week use ddd or dddd format specifier:
<asp:Label ID="ResourceAvailabilityDayLabel" runat="server" Text='<%# Bind("Date", "{0:dddd}") %>'></asp:Label>

casting problem of star rating?

I have an application with ajax star rating but when i am assigning value to CurrentRating from datatable then it showing error of "Specified cast is not valid".
I am using this code.
<asp:TemplateField HeaderText="Rating" SortExpression="CustomerRating">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CustomerRating")%>'></asp:Label></a>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<cc1:Rating ID="Rating1" runat="server" CurrentRating='<%# Bind("CustomerRating") %>'
StarCssClass="ratingStar"
WaitingStarCssClass="savedRatingStar"
FilledStarCssClass="filledRatingStar"
EmptyStarCssClass="emptyRatingStar"
>
</cc1:Rating>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
Then its showing error CurrentRating='<%# Bind("CustomerRating") %>'.
I am taking refrence from these sites.
asp.net forum
Code Project
Same thing working on Code project.
The problem is most likely that the CustomerRating property of your data item is not of the correct data type. Rating expects an int. The Databinder does use reflection and attempts to automatically handle type conversions, but it has limits.
Unfortunatly there isn't enough information in your qustion to know what the actual runtime type of CustomerRating is, so I can't say why it can't be cast. My advise would be to explicitly cast or convert the property like so:
CurrentRating='<%# (string)Bind("CustomerRating") %>'
CurrentRating='<%# Bind("CustomerRating").ToString() %>'
CurrentRating='<%# (int)Bind("CustomerRating") %>'
If you can't convert it simply, or just need to get a debugger on it so you can figure out what the type is you can call out to a custom method in your code-behind instead (and you can attach a debugger there to so you can see the runtime type of the item:
CurrentRating='<%# MyCustomMethod(Eval("CustomerRating")) %>'
in code behind:
public string MyCustomMethod(object customerRating)
{
string rValue = ... //do whatever you need to do to customerRating to get a string out of it
// good place to set a breakpoint you you can examine what type customerRating actually is so you can figure out how best to convert it to something databinding can use
return rValue;
}

Resources