0

I am trying to insert a new product into my database, although I am unsure how to make my Add Product button work properly. The gridview helped me in setting up the edit/update/delete and works perfectly. But I am not sure how to make my button communicate and add from my 4 textboxes to my database database. I am not sure if the code below will be helpful.

To explain though the bottom table contains the text boxes I am trying to take values from to insert into my database. Maybe there is a property I am missing?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ProductCode" DataSourceID="AccessDataSource1">
    <Columns>
        <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" 
            ReadOnly="True" SortExpression="ProductCode" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Version" HeaderText="Version" 
            SortExpression="Version" />
        <asp:BoundField DataField="ReleaseDate" HeaderText="ReleaseDate" 
            SortExpression="ReleaseDate" />
        <asp:CommandField ButtonType="Button" ShowEditButton="True" />
        <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
    </Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/TechSupport.mdb" 
    DeleteCommand="DELETE FROM [Products] WHERE [ProductCode] = ?" 
    InsertCommand="INSERT INTO [Products] ([ProductCode], [Name], [Version], [ReleaseDate]) VALUES (?, ?, ?, ?)" 
    SelectCommand="SELECT * FROM [Products] ORDER BY [ProductCode]" 
    UpdateCommand="UPDATE [Products] SET [Name] = ?, [Version] = ?, [ReleaseDate] = ? WHERE [ProductCode] = ?">
    <DeleteParameters>
        <asp:Parameter Name="ProductCode" Type="String" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Version" Type="Decimal" />
        <asp:Parameter Name="ReleaseDate" Type="DateTime" />
        <asp:Parameter Name="ProductCode" Type="String" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="ProductCode" Type="String" />
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Version" Type="Decimal" />
        <asp:Parameter Name="ReleaseDate" Type="DateTime" />
    </InsertParameters>
</asp:AccessDataSource>
<br />
<br />
To add a new Product, enter the product information below and Click Add Product!<br />
<br />
<table border="0" cellpadding="0" cellspacing="6"><tr><td>Product Code:</td><td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td><td>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator></td></tr>
    <tr><td>Name:</td><td> <asp:TextBox ID="TextBox2" runat="server" Width="349px"></asp:TextBox></td><td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
            ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2"></asp:RequiredFieldValidator></td></tr>
    <tr><td>Version:</td><td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td><td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
            ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
        </td></tr>
    <tr><td>Release Date:</td><td><asp:TextBox ID="TextBox4" runat="server">mm/dd/yy</asp:TextBox></td><td>
        &nbsp;</td></tr>
    </table>
    <br />
    <br />
<asp:Button ID="Button1" runat="server" Text="Add Product" />
</asp:Content>

1 Answer 1

2

Convert insert parameters from Parameter to ControlParameter, use appropriate textbox ID for ControlID property value for each parameter and Text for PropertyName property value (i.e. change InserParameters section as below). After that, place following code in Button1 Click event handler: AccessDataSource1.Insert().

<InsertParameters>
    <asp:ControlParameter Name="ProductCode" Type="String" ControlID="TextBox1" PropertyName="Text" />
    <asp:ControlParameter Name="Name" Type="String" ControlID="TextBox2" PropertyName="Text"/>
    <asp:ControlParameter Name="Version" Type="Decimal" ControlID="TextBox3" PropertyName="Text"/>
    <asp:ControlParameter Name="ReleaseDate" Type="DateTime" ControlID="TextBox4" PropertyName="Text" ConvertEmptyStringToNull="true"/>
</InsertParameters>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.