?
Solved

Specified cast is not valid.

Posted on 2005-04-08
11
Medium Priority
?
627 Views
Last Modified: 2008-02-01
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>

0
Comment
Question by:lynnton
  • 5
  • 5
11 Comments
 

Expert Comment

by:smooga
ID: 13735799
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
 
LVL 7

Expert Comment

by:ayha1999
ID: 13735800
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
 
LVL 1

Author Comment

by:lynnton
ID: 13735835
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
ayha1999 earned 2000 total points
ID: 13735894
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
 
LVL 1

Author Comment

by:lynnton
ID: 13735952
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
 
LVL 1

Author Comment

by:lynnton
ID: 13735976
ayha1999,

For sample "Second Break column"

Thanks.
0
 
LVL 7

Expert Comment

by:ayha1999
ID: 13736098
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
 
LVL 1

Author Comment

by:lynnton
ID: 13736160
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
 
LVL 7

Expert Comment

by:ayha1999
ID: 13736207
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
 
LVL 1

Author Comment

by:lynnton
ID: 13736279
ayha1999,

I'll repost and hope other experts will join.

Thanks.
0
 
LVL 7

Expert Comment

by:ayha1999
ID: 13736310
Hi,

Glad to have helped.

ayha
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question