Link to home
Start Free TrialLog in
Avatar of lynnton
lynnton

asked on

cast is not valid

Hi,

What does Specified cast is not valid. means?

how can we get the value of First Break, Lunch break, and second break? using DirectCast(e.Item.Cells( ?).Controls( ?),TextBox)

Thanks.


Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 120:                                'Dim txtlunch As TextBox = DirectCast(e.Item.Cells(6).Controls(0),TextBox)
Line 121:                                'Dim txtlast As TextBox = DirectCast(e.Item.Cells(7).Controls(0),TextBox)
Line 122:                                if isdate(DirectCast(e.Item.Cells(5).Controls(0),TextBox)) or _
Line 123:                                 isdate(DirectCast(e.Item.Cells(6).Controls(0),TextBox)) or _
Line 124:                                  isdate(DirectCast(e.Item.Cells(7).Controls(7),TextBox)) then
Source File: c:\inetpub\wwwroot\shiftview.aspx    Line: 122


<asp:DataGrid id="DataGrid1" runat="server" Font-Names="Verdana" Font-Size="XX-Small" DataKeyField="operatorshiftid" AutoGenerateColumns="False" CellPadding="4" onPageIndexChanged="pageGrid" AllowPaging="True" PageSize="50" OnDeleteCommand="manage_delete" OnUpdateCommand="manage_update" OnCancelCommand="manage_cancel_edit" OnEditCommand="manage_edit" OnItemDataBound="manage_ItemDataBound" GridLines="Horizontal">
                <HeaderStyle font-bold="True"></HeaderStyle>
                <Columns>
                    <asp:BoundColumn DataField="operatorid" ReadOnly="True" HeaderText="Operator ID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="operatorname" ReadOnly="True" HeaderText="Operator Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="shiftdate" ReadOnly="True" HeaderText="Shift Date"></asp:BoundColumn>
                    <asp:TemplateColumn HeaderText="Start Time">
                        <ItemTemplate>
                            <%# Container.DataItem("starttime")%>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label id="lblSelected1" visible="false" text=<%# DataBinder.Eval(Container.DataItem, "starttime") %> runat="server"/>
                            <asp:DropDownList ID="DropDownList1" runat="server" />
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="First Break">
                        <ItemTemplate>
                            <asp:Label runat="server" text='<%# DataBinder.Eval(Container, "DataItem.break1") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" Width="80px" Text='<%# DataBinder.Eval(Container, "DataItem.break1") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Lunch Break">
                        <ItemTemplate>
                            <asp:Label runat="server" text='<%# DataBinder.Eval(Container, "DataItem.lunch") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" Width="80px" Text='<%# DataBinder.Eval(Container, "DataItem.lunch") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Second Break">
                        <ItemTemplate>
                            <asp:Label runat="server" text='<%# DataBinder.Eval(Container, "DataItem.break2") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" Width="80px" Text='<%# DataBinder.Eval(Container, "DataItem.break2") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="endtime" ReadOnly="True" HeaderText="End Time"></asp:BoundColumn>
                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                    <asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
                </Columns>
                <PagerStyle horizontalalign="Right" position="TopAndBottom" pagebuttoncount="20" mode="NumericPages"></PagerStyle>
            </asp:DataGrid>
Avatar of djhex
djhex

why dont u convert in this way

dim res as string=  e.Item.Cells(5).Controls(0).text
dim dat as datetime = convert.todatetime(res)
Are u trying to make a validaton of datetimes? Why dont u use regular expression validators?
SOLUTION
Avatar of Type25
Type25

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynnton

ASKER

djhex,

We need to get the data first :-)

how can we get the value of First Break, Lunch break, and second break? using DirectCast(e.Item.Cells( ?).Controls( ?),TextBox)


Thanks.
I don't know what DirectCast is either, but can't you just do this?

if isdate(cType(e.Item.Cells(5).Controls(0),TextBox))

Also...  a lot of times the index isn't what you think when you are looking in a datagrid. If you can just reference the control by name.

if isdate(cType(e.Item.FindControl("textboxname"), TextBox))
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops, I forgot something

if isdate(cType(e.Item.FindControl("textboxname"), TextBox).text) then

Try that.
Avatar of lynnton

ASKER

thrill_house,

sadly :End of statement expected

Thanks.






Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30205: End of statement expected.
Source Error:
Line 117:                        '--for OnUpdateCommand (SP_team_editonupdate)
Line 118:                            Sub manage_update( s As Object, e As DataGridCommandEventArgs)
Line 119:                                dim YourVarLbl string CType(e.item.FindControl("txtfb"),textbox).text
Line 120:                                'Dim txtlunch As TextBox = DirectCast(e.Item.Cells(6).Controls(0),TextBox)
Line 121:                                'Dim txtlast As TextBox = DirectCast(e.Item.Cells(7).Controls(0),TextBox)
 

Source File: c:\inetpub\wwwroot\shiftview.aspx    Line: 119


                            <asp:Label runat="server" text='<%# DataBinder.Eval(Container, "DataItem.break1") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox id="txtfb" runat="server"  Width="80px" Text='<%# DataBinder.Eval(Container, "DataItem.break1") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Lunch Break">
                        <ItemTemplate>
I want to know why dont u make it the other Way.

You can even validate on the client
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynnton

ASKER

   Class DateTimeUtils
   
    Public Shared Function IsDate(ByVal strDate As String) As Boolean
        Dim dtDate As DateTime
        Dim bValid As Boolean = True
            Try
                dtDate = DateTime.Parse(strDate)
                Catch eFormatException As FormatException
                ' the Parse method failed => the string strDate cannot be converted to a date.
                bValid = False
            End Try
            Return bValid
    End Function
    End Class