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

Posted on 2011-04-29
Last Modified: 2012-05-11
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]">
                <asp:Parameter Name="state" Type="String" />
                <asp:Parameter Name="state" Type="String" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="state" DataSourceID="SqlDataSource1" ShowFooter="True">
                <asp:TemplateField HeaderText="state" SortExpression="state">
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label>
                        <asp:TextBox ID="tbState" runat="server" Width="43px"></asp:TextBox>
                        <asp:Button ID="btInsert" runat="server" onclick="btInsert_Click" 
                            Text="insert" />
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("state") %>'></asp:Label>

Open in new window

Protected Sub btInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        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
    End Sub

Open in new window

Question by:jaytechnology
    LVL 83

    Accepted Solution

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


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

    Author Closing Comment

    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.
    LVL 83

    Expert Comment

    Glad to help :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now