ASP.NET, Help, submit button onClick write to database question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mturner64
    New Member
    • Feb 2007
    • 49

    ASP.NET, Help, submit button onClick write to database question.

    I am using Microsoft VWD 2008 express edition. I have linked an Access 2007 database to my asp.net application using a gridview control. On the webpage are four text boxes allowing a user to input (first name, last name, donation amount and date).

    After the user inputs the values, I want them to click the "Submit" button and have that information from the text boxes write to the Access 2007 database.

    This is where my problem lies. I hit a mental blank when I begin to program the button. Not sure where to start. Any help is appreciated! Thanks. ~Mike

    My code is below:

    Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <p>
            First Name:&nbsp;
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            Last Name:&nbsp;
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            Donation:&nbsp;
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            Donation Date:&nbsp;
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        </p>
        <p>
            &nbsp;</p>
        <p>
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" style="height: 26px" />
        </p>
        <p>
            &nbsp;</p>
        <p>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConflictDetection="CompareAllValues" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                DeleteCommand="DELETE FROM [Table1] WHERE (([Last Name] = ?) OR ([Last Name] IS NULL AND ? IS NULL)) AND [ID] = ? AND (([First Name] = ?) OR ([First Name] IS NULL AND ? IS NULL)) AND (([Donation Amount] = ?) OR ([Donation Amount] IS NULL AND ? IS NULL)) AND (([Date of Donation] = ?) OR ([Date of Donation] IS NULL AND ? IS NULL))" 
                InsertCommand="INSERT INTO [Table1] ([ID], [First Name], [Last Name], [Donation Amount], [Date of Donation]) VALUES (?, ?, ?, ?, ?)" 
                OldValuesParameterFormatString="original_{0}" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT * FROM [Table1]" 
                UpdateCommand="UPDATE [Table1] SET [ID] = ?, [First Name] = ?, [Donation Amount] = ?, [Date of Donation] = ? WHERE (([Last Name] = ?) OR ([Last Name] IS NULL AND ? IS NULL)) AND [ID] = ? AND (([First Name] = ?) OR ([First Name] IS NULL AND ? IS NULL)) AND (([Donation Amount] = ?) OR ([Donation Amount] IS NULL AND ? IS NULL)) AND (([Date of Donation] = ?) OR ([Date of Donation] IS NULL AND ? IS NULL))">
                <DeleteParameters>
                    <asp:Parameter Name="original_Last_Name" Type="String" />
                    <asp:Parameter Name="original_ID" Type="Int32" />
                    <asp:Parameter Name="original_First_Name" Type="String" />
                    <asp:Parameter Name="original_Donation_Amount" Type="Decimal" />
                    <asp:Parameter Name="original_Date_of_Donation" Type="DateTime" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                    <asp:Parameter Name="First_Name" Type="String" />
                    <asp:Parameter Name="Donation_Amount" Type="Decimal" />
                    <asp:Parameter Name="Date_of_Donation" Type="DateTime" />
                    <asp:Parameter Name="original_Last_Name" Type="String" />
                    <asp:Parameter Name="original_ID" Type="Int32" />
                    <asp:Parameter Name="original_First_Name" Type="String" />
                    <asp:Parameter Name="original_Donation_Amount" Type="Decimal" />
                    <asp:Parameter Name="original_Date_of_Donation" Type="DateTime" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                    <asp:Parameter Name="First_Name" Type="String" />
                    <asp:Parameter Name="Last_Name" Type="String" />
                    <asp:Parameter Name="Donation_Amount" Type="Decimal" />
                    <asp:Parameter Name="Date_of_Donation" Type="DateTime" />
                </InsertParameters>
            </asp:SqlDataSource>
        </p>
        <p>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="Last Name" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                        SortExpression="ID" />
                    <asp:BoundField DataField="First Name" HeaderText="First Name" 
                        SortExpression="First Name" />
                    <asp:BoundField DataField="Last Name" HeaderText="Last Name" ReadOnly="True" 
                        SortExpression="Last Name" />
                    <asp:BoundField DataField="Donation Amount" HeaderText="Donation Amount" 
                        SortExpression="Donation Amount" />
                    <asp:BoundField DataField="Date of Donation" HeaderText="Date of Donation" 
                        SortExpression="Date of Donation" />
                </Columns>
            </asp:GridView>
        </p>
        <p>
            &nbsp;</p>
        <p>
            &nbsp;</p>
        <div>
        
        </div>
        </form>
    </body>
    </html>
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    OK, here are some things you will need to do. First, if ID is an AutoNumber, you need to remove it from the insert command. Make sure you remove one of the '?' from the VALUES list as well. The reason you do this is because you can't insert a value into a AutoNumber field. You will also need to remove it from the <InsertParamete rs> section.

    Next, you will need to change all the <asp:Paramete r> into <asp:ControlPar ameter> tags in the <InsertParamete rs> section. There are two extra properties you must set: ControlID and PropertyName. ControlID is the control that the parameter will get it's value from. PropertyName is the property of that control that holds the value. For TextBoxes, you use the Text property. ListBoxes will usually be SelectedValue, and so on.

    Here's an example of what the <InsertParamete rs> should look like.
    Note that this is just a sample, you will have to make this fit your own program.
    Code:
    <InsertParameters>
        <asp:ControlParameter ControlID="tbUsername" PropertyName="Text" Name="username" Type="String" />
        <asp:ControlParameter ControlID="tbLevel" PropertyName="Text" Name="level" Type="Int16" />
    </InsertParameters>
    Now, you need an event handler for your button. Just double-click it in the designer. You should be taken to the codepage and a handler should be automatically set up for you.

    The way to trigger an insert is to call the SqlDataSource's Insert() method.

    So, in the handler that was just set up:
    C# code, but VB.NET is quite similar
    Code:
    protected void b1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.Insert();
        //if you want your gridview to show the update use this:
        GridView1.DataBind();
    }
    Now you should have inserted a record into your DB.

    Edit:
    One other thing I noticed. If you want the date to auto-populate instead of having to type it in, you can add a Hidden control to the page. In the Page_Load event, you can set the Hidden's Value attribute to DateTime.Now.To String() and use that as one of your parameters. The PropertyName would be "Value".

    Hope that helps.

    Comment

    • mturner64
      New Member
      • Feb 2007
      • 49

      #3
      Sounds great! I will give it a go and hope for the best. I appreciate your assistance. Have a great day! ~Mike

      Comment

      • mturner64
        New Member
        • Feb 2007
        • 49

        #4
        Ths solution above works very well! A++ Thanks for the help!

        Comment

        Working...