By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

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

P: 49
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:

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <p>
  12.         First Name:&nbsp;
  13.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  14.     </p>
  15.     <p>
  16.         &nbsp;</p>
  17.     <p>
  18.         Last Name:&nbsp;
  19.         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  20.     </p>
  21.     <p>
  22.         &nbsp;</p>
  23.     <p>
  24.         Donation:&nbsp;
  25.         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  26.     </p>
  27.     <p>
  28.         &nbsp;</p>
  29.     <p>
  30.         Donation Date:&nbsp;
  31.         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  32.     </p>
  33.     <p>
  34.         &nbsp;</p>
  35.     <p>
  36.         <asp:Button ID="btnSubmit" runat="server" Text="Submit" style="height: 26px" />
  37.     </p>
  38.     <p>
  39.         &nbsp;</p>
  40.     <p>
  41.         <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  42.             ConflictDetection="CompareAllValues" 
  43.             ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
  44.             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))" 
  45.             InsertCommand="INSERT INTO [Table1] ([ID], [First Name], [Last Name], [Donation Amount], [Date of Donation]) VALUES (?, ?, ?, ?, ?)" 
  46.             OldValuesParameterFormatString="original_{0}" 
  47.             ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
  48.             SelectCommand="SELECT * FROM [Table1]" 
  49.             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))">
  50.             <DeleteParameters>
  51.                 <asp:Parameter Name="original_Last_Name" Type="String" />
  52.                 <asp:Parameter Name="original_ID" Type="Int32" />
  53.                 <asp:Parameter Name="original_First_Name" Type="String" />
  54.                 <asp:Parameter Name="original_Donation_Amount" Type="Decimal" />
  55.                 <asp:Parameter Name="original_Date_of_Donation" Type="DateTime" />
  56.             </DeleteParameters>
  57.             <UpdateParameters>
  58.                 <asp:Parameter Name="ID" Type="Int32" />
  59.                 <asp:Parameter Name="First_Name" Type="String" />
  60.                 <asp:Parameter Name="Donation_Amount" Type="Decimal" />
  61.                 <asp:Parameter Name="Date_of_Donation" Type="DateTime" />
  62.                 <asp:Parameter Name="original_Last_Name" Type="String" />
  63.                 <asp:Parameter Name="original_ID" Type="Int32" />
  64.                 <asp:Parameter Name="original_First_Name" Type="String" />
  65.                 <asp:Parameter Name="original_Donation_Amount" Type="Decimal" />
  66.                 <asp:Parameter Name="original_Date_of_Donation" Type="DateTime" />
  67.             </UpdateParameters>
  68.             <InsertParameters>
  69.                 <asp:Parameter Name="ID" Type="Int32" />
  70.                 <asp:Parameter Name="First_Name" Type="String" />
  71.                 <asp:Parameter Name="Last_Name" Type="String" />
  72.                 <asp:Parameter Name="Donation_Amount" Type="Decimal" />
  73.                 <asp:Parameter Name="Date_of_Donation" Type="DateTime" />
  74.             </InsertParameters>
  75.         </asp:SqlDataSource>
  76.     </p>
  77.     <p>
  78.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  79.             DataKeyNames="Last Name" DataSourceID="SqlDataSource1">
  80.             <Columns>
  81.                 <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
  82.                     SortExpression="ID" />
  83.                 <asp:BoundField DataField="First Name" HeaderText="First Name" 
  84.                     SortExpression="First Name" />
  85.                 <asp:BoundField DataField="Last Name" HeaderText="Last Name" ReadOnly="True" 
  86.                     SortExpression="Last Name" />
  87.                 <asp:BoundField DataField="Donation Amount" HeaderText="Donation Amount" 
  88.                     SortExpression="Donation Amount" />
  89.                 <asp:BoundField DataField="Date of Donation" HeaderText="Date of Donation" 
  90.                     SortExpression="Date of Donation" />
  91.             </Columns>
  92.         </asp:GridView>
  93.     </p>
  94.     <p>
  95.         &nbsp;</p>
  96.     <p>
  97.         &nbsp;</p>
  98.     <div>
  99.  
  100.     </div>
  101.     </form>
  102. </body>
  103. </html>
Sep 24 '08 #1
Share this Question
Share on Google+
3 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
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 <InsertParameters> section.

Next, you will need to change all the <asp:Parameter> into <asp:ControlParameter> tags in the <InsertParameters> 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 <InsertParameters> should look like.
Note that this is just a sample, you will have to make this fit your own program.
Expand|Select|Wrap|Line Numbers
  1. <InsertParameters>
  2.     <asp:ControlParameter ControlID="tbUsername" PropertyName="Text" Name="username" Type="String" />
  3.     <asp:ControlParameter ControlID="tbLevel" PropertyName="Text" Name="level" Type="Int16" />
  4. </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
Expand|Select|Wrap|Line Numbers
  1. protected void b1_Click(object sender, EventArgs e)
  2. {
  3.     SqlDataSource1.Insert();
  4.     //if you want your gridview to show the update use this:
  5.     GridView1.DataBind();
  6. }
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.ToString() and use that as one of your parameters. The PropertyName would be "Value".

Hope that helps.
Sep 24 '08 #2

P: 49
Sounds great! I will give it a go and hope for the best. I appreciate your assistance. Have a great day! ~Mike
Sep 24 '08 #3

P: 49
Ths solution above works very well! A++ Thanks for the help!
Sep 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.