Link to home
Start Free TrialLog in
Avatar of jaytechnology
jaytechnologyFlag for Afghanistan

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaytechnology

ASKER

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.
Glad to help :-)