Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trouble Setting Insert Parameters in DataGrid View in ASP.Net

Posted on 2010-08-12
2
Medium Priority
?
264 Views
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.
DataGridView
================
                <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">
                        <Columns>
                            <asp:TemplateField HeaderText="Sort Order" SortExpression="SortOrder">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SortOrder") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="newSortOrderTB" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("SortOrder") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Position" SortExpression="StaffPosition">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("StaffPosition") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="NewStaffPositionTB" runat="server" Width="212px"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("StaffPosition") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Staff Member" SortExpression="StaffMember">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("StaffMember") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="NewStaffMemberTB" runat="server" Width="227px"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("StaffMember") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="StaffID" HeaderText="StaffID" InsertVisible="False" 
                                ReadOnly="True" SortExpression="StaffID" Visible="False" />
                            <asp:TemplateField ShowHeader="False">
                                <EditItemTemplate>
                                    <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>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:Button ID="AddNew" runat="server" CausesValidation="false"
                                        CommandName="Insert" Text="Add New"  OnClick="AddNew_Click"></asp:Button>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <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>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <asp:AccessDataSource ID="StaffDataSource" runat="server" 
                        DataFile="~/App_Data/ABVM.mdb" 
                        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] = ?">
                        <DeleteParameters>
                            <asp:Parameter Name="StaffID" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="SortOrder" Type="Int16" />
                            <asp:Parameter Name="StaffPosition" Type="String" />
                            <asp:Parameter Name="StaffMember" Type="String" />
                            <asp:Parameter Name="StaffID" Type="Int32" />
                        </UpdateParameters>
                        <InsertParameters>
                            <asp:Parameter Name="NewSortOrder" Type="Int16" />
                            <asp:Parameter Name="NewStaffPosition" Type="String" />
                            <asp:Parameter Name="NewStaffMember" Type="String" />
                        </InsertParameters>
                    </asp:AccessDataSource>
                </asp:Panel>

CODE
=======
    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
        StaffDataSource.DataBind()

    End Sub

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

Open in new window

0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

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

similarly others.
0
 

Author Closing Comment

by:dyarosh
ID: 33425195
Thank you.  That did it.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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