• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

ASP.NET 4.0 VB how to Insert footer row from gridview to SQL server (Total meltdown)

I am writing a simple gridview in VS 2010 with 1 column called state.  I added a footer row with a text box tbState and a submit button.  I have done this a 100 times before but not in the last month and have forgotten how to do it.
I have the code executing on the on click submit button.

I need my hand held here.
Code on the Default page:
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:StatesConnectionString %>" 
            DeleteCommand="DELETE FROM [states] WHERE [state] = @state" 
            InsertCommand="INSERT INTO [states] ([state]) VALUES (@state)" 
            SelectCommand="SELECT * FROM [states]">
            <DeleteParameters>
                <asp:Parameter Name="state" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="state" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="state" DataSourceID="SqlDataSource1" ShowFooter="True">
            <Columns>
                <asp:TemplateField HeaderText="state" SortExpression="state">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="tbState" runat="server" Width="43px"></asp:TextBox>
                        &nbsp;
                        <asp:Button ID="btInsert" runat="server" onclick="btInsert_Click" 
                            Text="insert" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("state") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </p>
</asp:Content>

Open in new window

Protected Sub btInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'CREATE CONNECTION STRING
        Dim conn As New SqlConnection("Server=JRIDLE-PC\SQL2008R2;Database=States;User ID=sharepoint;Password=sharepoint;")

        Dim CMD As New SqlCommand("insert into [state] values (@state", conn)
        Dim GV As New GridView
        Dim index As Integer = GV.EditIndex
        Dim row As GridViewRow = GV.Rows(index)



        Dim st As TextBox = CType(row.FindControl("tbState"), TextBox)


        CMD.Parameters.AddWithValue("@state", SqlDbType.NChar).Value = st.Text
        conn.Open()
        CMD.ExecuteNonQuery()
    End Sub

Open in new window

0
jaytechnology
Asked:
jaytechnology
  • 2
1 Solution
 
CodeCruiserCommented:
>Dim GV As New GridView

You are creating a new gridview hence all the problems.

Change this bit

        Dim GV As New GridView
        Dim index As Integer = GV.EditIndex
        Dim row As GridViewRow = GV.Rows(index)



        Dim st As TextBox = CType(row.FindControl("tbState"), TextBox)


to

        Dim st As TextBox = CType(GridView1.FooterRow.FindControl("tbState"), TextBox)
0
 
jaytechnologyAuthor Commented:
Quick and precise.  I feel like a well, I am gracious.

Thanks.. YOU ROCK!

I see my error based of of your explanation.  I want creating a new grid, already had one.
0
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now