Specified 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?

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>

LVL 1
lynntonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smoogaCommented:
Make sure you actually have a TextBox at e.Item.Cells(5).Controls(0).  If you have, say, a checkbox there, you cannot cast it to a TextBox :)  I'm not very familiar with VB.NET, but can you say "e.Item.Cells(5).Controls(0) as TextBox" instead?  Not sure what DirectCast does.  Also, it looks like you are checking for isdate on the Textbox object itself.  I believe isdate takes a string.  You want to check the text inside the TextBox.
0
ayha1999Commented:
Hi,

Make sure the following contains textbox. You have to give correct control name. If this item contains label or dropodownlist then replace textbox with correct control.
 
Line 122:    if isdate(DirectCast(e.Item.Cells(5).Controls(0),TextBox)) or _

hope this helps.

ayha
0
lynntonAuthor Commented:
ayha1999,

We are trying to catch :

</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>

Please kindly guide me the proper values on DirectCast(e.Item.Cells(5).Controls(0),TextBox)

Thanks.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ayha1999Commented:
Hi,

If you want to retrive textbox in edit mode then just change to;

                            <asp:TextBox "YourVarText"  runat="server" Width="80px" Text='<%# DataBinder.Eval(Container, "DataItem.break2") %>'></asp:TextBox>
                        </EditItemTemplate>

in the code-behind

dim YourVarText string CType(e.item.FindControl("YourVarText"),TextBox).Text

Or if you want to retrive label value other than edit mode then change to

 </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Second Break">
                        <ItemTemplate>
                            <asp:Label "YourVarLbl" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.break2") %>'></asp:Label>
                        </ItemTemplate>

in teh code behind use

dim YourVarLbl string CType(e.item.FindControl("YourVarLbl "),Label).Text

ayha
                 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lynntonAuthor Commented:
ayha1999,

Awsome, just for additional knowledge,

What should be the proper values if ever we are going to use  DirectCast(e.Item.Cells(?).Controls(?),TextBox)

The html is posted on the first page.

Thanks.
0
lynntonAuthor Commented:
ayha1999,

For sample "Second Break column"

Thanks.
0
ayha1999Commented:
Hi,

Always try to use FindControl method instead of referring to cell indexs which cause error or will be difficult to change order of columns.

You can give names to template column and retrive in the method I shown. If you want retive values from edit mode then give names to textbox,checkbox,radio,dropdown etc.

if in normal mode you can just give names to labels and retrive.

ayha
0
lynntonAuthor Commented:
ayha1999,

Noted, clear and consice, in the sake of wanting to know and learning asp.net  :-)

What should be the proper values if ever we are going to use  DirectCast(e.Item.Cells(?).Controls(?),TextBox) ?

for second break.

Thanks.
0
ayha1999Commented:
Hi Lynnton,

I have not much used this methoed. But I think in the cell you have to give cell index andin the controls the control sequence.

ayha
0
lynntonAuthor Commented:
ayha1999,

I'll repost and hope other experts will join.

Thanks.
0
ayha1999Commented:
Hi,

Glad to have helped.

ayha
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.