Trouble Setting Insert Parameters in DataGrid View in ASP.Net

Posted on 2010-08-12
Last Modified: 2012-05-10
I am trying to code an Insert from a DataGridView and I'm not setting the Insert Parameters correctly and can't figure out why.  The way it is currently coded, the new record is being added but not with the data that I entered (it is blank).  When debugging, the data is in the fields that I am using to set the parameters.  Here is the code and GridView Definition.  Any help is appreciated.
                <asp:Panel ID="Panel1" runat="server" CssClass="left_padding15">
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AutoGenerateColumns="False" CellPadding="5" CellSpacing="5" 
                        DataKeyNames="StaffID" DataSourceID="StaffDataSource" ShowFooter="True">
                            <asp:TemplateField HeaderText="Sort Order" SortExpression="SortOrder">
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SortOrder") %>'></asp:TextBox>
                                    <asp:TextBox ID="newSortOrderTB" runat="server"></asp:TextBox>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("SortOrder") %>'></asp:Label>
                            <asp:TemplateField HeaderText="Position" SortExpression="StaffPosition">
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("StaffPosition") %>'></asp:TextBox>
                                    <asp:TextBox ID="NewStaffPositionTB" runat="server" Width="212px"></asp:TextBox>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("StaffPosition") %>'></asp:Label>
                            <asp:TemplateField HeaderText="Staff Member" SortExpression="StaffMember">
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("StaffMember") %>'></asp:TextBox>
                                    <asp:TextBox ID="NewStaffMemberTB" runat="server" Width="227px"></asp:TextBox>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("StaffMember") %>'></asp:Label>
                            <asp:BoundField DataField="StaffID" HeaderText="StaffID" InsertVisible="False" 
                                ReadOnly="True" SortExpression="StaffID" Visible="False" />
                            <asp:TemplateField ShowHeader="False">
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                                        CommandName="Update" Text="Update"></asp:LinkButton>
                                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                    <asp:Button ID="AddNew" runat="server" CausesValidation="false"
                                        CommandName="Insert" Text="Add New"  OnClick="AddNew_Click"></asp:Button>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                        CommandName="Edit" Text="Edit"></asp:LinkButton>
                                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                        CommandName="Delete" Text="Delete"></asp:LinkButton>
                    <asp:AccessDataSource ID="StaffDataSource" runat="server" 
                        DeleteCommand="DELETE FROM [Staff] WHERE [StaffID] = ?" 
                        InsertCommand="INSERT INTO [Staff] ([SortOrder], [StaffPosition], [StaffMember]) VALUES (?, ?, ?)" 
                        SelectCommand="SELECT [SortOrder], [StaffPosition], [StaffMember], [StaffID] FROM [Staff] ORDER BY [SortOrder]" 
                        UpdateCommand="UPDATE [Staff] SET [SortOrder] = ?, [StaffPosition] = ?, [StaffMember] = ? WHERE [StaffID] = ?">
                            <asp:Parameter Name="StaffID" Type="Int32" />
                            <asp:Parameter Name="SortOrder" Type="Int16" />
                            <asp:Parameter Name="StaffPosition" Type="String" />
                            <asp:Parameter Name="StaffMember" Type="String" />
                            <asp:Parameter Name="StaffID" Type="Int32" />
                            <asp:Parameter Name="NewSortOrder" Type="Int16" />
                            <asp:Parameter Name="NewStaffPosition" Type="String" />
                            <asp:Parameter Name="NewStaffMember" Type="String" />

    Protected Sub StaffDataSource_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles StaffDataSource.Inserting
        Dim SO As TextBox = GridView1.FooterRow.FindControl("NewSortOrderTB")
        Dim SP As TextBox = GridView1.FooterRow.FindControl("NewStaffPositionTB")
        Dim SM As TextBox = GridView1.FooterRow.FindControl("NewStaffMemberTB")

        StaffDataSource.InsertParameters("NewSortOrder").DefaultValue = CInt(SO.Text)
        StaffDataSource.InsertParameters("NewStaffPosition").DefaultValue = SP.Text
        StaffDataSource.InsertParameters("NewStaffMember").DefaultValue = SM.Text

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub

Open in new window

Question by:dyarosh
LVL 41

Accepted Solution

guru_sami earned 500 total points
ID: 33424567
         e.Command.Paramters("NewSortOrder").Value  =CInt(SO.Text)

similarly others.

Author Closing Comment

ID: 33425195
Thank you.  That did it.

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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