Results 1 to 5 of 5

Thread: Help - DataGrid - Specified cast is not valid. *Resolved*

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Help - DataGrid - Specified cast is not valid. *Resolved*

    I have been pulling my hair out over this and have yet to find a resolution. The code was working until I changed the bound until I started tweaking the DataGrid.

    The ASP page code is:

    VB Code:
    1. <asp:datagrid id="DataGrid1" runat="server" GridLines="Vertical" DataKeyField="City" AllowPaging="True"
    2.                 PageSize="6" BackColor="White" CellPadding="3" width="80%" BorderStyle="None" BorderWidth="1px"
    3.                 BorderColor="#999999" AutoGenerateColumns="False">
    4.                 <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
    5.                 <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
    6.                 <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
    7.                 <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
    8.                 <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
    9.                 <Columns>
    10.                     <asp:TemplateColumn>
    11.                         <ItemTemplate>
    12.                             <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="false"></asp:LinkButton>
    13.                         </ItemTemplate>
    14.                         <EditItemTemplate>
    15.                             <asp:LinkButton runat="server" Text="Update" CommandName="Update"></asp:LinkButton>&nbsp;
    16.                             <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
    17.                         </EditItemTemplate>
    18.                     </asp:TemplateColumn>
    19.                     <asp:TemplateColumn>
    20.                         <ItemTemplate>
    21.                             <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
    22.                         </ItemTemplate>
    23.                     </asp:TemplateColumn>
    24.                     <asp:TemplateColumn HeaderText="City">
    25.                         <ItemTemplate>
    26.                             <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'>
    27.                             </asp:Label>
    28.                         </ItemTemplate>
    29.                         <EditItemTemplate>
    30.                             <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'>
    31.                             </asp:TextBox>
    32.                         </EditItemTemplate>
    33.                     </asp:TemplateColumn>
    34.                     <asp:TemplateColumn HeaderText="State/Country">
    35.                         <ItemTemplate>
    36.                             <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.State/Country") %>'>
    37.                             </asp:Label>
    38.                         </ItemTemplate>
    39.                         <EditItemTemplate>
    40.                             <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.State/Country") %>'>
    41.                             </asp:TextBox>
    42.                         </EditItemTemplate>
    43.                     </asp:TemplateColumn>
    44.                     <asp:TemplateColumn HeaderText="Contact Email">
    45.                         <ItemTemplate>
    46.                             <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Contact Email") %>'>
    47.                             </asp:Label>
    48.                         </ItemTemplate>
    49.                         <EditItemTemplate>
    50.                             <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Contact Email") %>'>
    51.                             </asp:TextBox>
    52.                         </EditItemTemplate>
    53.                     </asp:TemplateColumn>
    54.                     <asp:BoundColumn DataField="Active?" ReadOnly="True" HeaderText="Active?"></asp:BoundColumn>
    55.                 </Columns>
    56.                 <PagerStyle Font-Size="Smaller" HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"
    57.                     Mode="NumericPages"></PagerStyle>
    58.             </asp:datagrid>

    The DataGrid_Update Sub line of code that blows up:


    VB Code:
    1. Was working before my changes to the Datagrids look and feel:
    2.         Dim City As String = Server.UrlEncode(CType(E.Item.Cells(2).Controls(0), TextBox).Text)
    3.  
    4. Tried this option as well:
    5.         Dim city As TextBox = CType(E.Item.Cells(2).Controls(0), TextBox)
    6.         Dim strcity As String = Server.UrlEncode(city.Text)

    What am i doing wrong??
    Last edited by Tenebrosity; Apr 1st, 2005 at 12:19 AM. Reason: Detailed title
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Help - DataGrid - Specified cast is not valid.

    I have also tried this method.

    VB Code:
    1. Dim tbCity As TextBox
    2.         tbCity = E.Item.Cells(3).Controls(0)
    3.         Dim strCity As String = tbCity.Text.Trim

    Any Ideas? I am so frustrated right now.
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Help - DataGrid - Specified cast is not valid.

    *bump*
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  4. #4
    Member EricDalnas's Avatar
    Join Date
    Sep 2004
    Location
    Rhode Island
    Posts
    51

    Re: Help - DataGrid - Specified cast is not valid.

    try giving IDs to the controls like
    <EditItemTemplate>
    <asp:TextBox ID="City" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>'></asp:TextBox>
    </EditItemTemplate>


    and then in the code

    tbCity = directcast(e.Item.FindControl("City"),TextBox)

    Lemme know if that works.
    Maybe if you'd stop breathing and die for a change I wouldn't be so pissed off all the time.

    www.mredkj.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Help - DataGrid - Specified cast is not valid.

    Thats got it! Thanks!

    I was ready to toss my laptop across the room. hehehe
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width