473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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:

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
3 3387
Curtis Rutland
3,256 Expert 2GB
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
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
Ths solution above works very well! A++ Thanks for the help!
Sep 28 '08 #4

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

Similar topics

4
by: Eric | last post by:
Hey Everyone.. I have a form that has approximately 7 text fields and 1 checkbox. Generally when this form is submitted(to itself BTW) it works fine, however, when the checkbox is only field...
15
by: M Smith | last post by:
I have a form I want to submit to itself. I want to be able to type in a list of numbers and submit the form and have that list show up on the same form under the text box I typed them into and...
7
by: Trvl Orm | last post by:
I am working with 2 frames, Left and Right and the main code is in the left frame, which has been attached. Can someone please help me with this code. I am new to JavaScript and can't figure it...
14
by: TrvlOrm | last post by:
OK. After much playing around, I managed to get my frame page this far.. see code below. BUT...there are still errors with it, and what I would like to have happened is this: 1) On the Left...
4
by: actionwoman63 | last post by:
Dear all I need to be able to check which one out of two submit buttons within the same form was pressed in a javascript function prior to form submission. And before I get flamed for not...
4
by: Dmitry Korolyov [MVP] | last post by:
When we use btnSubmit.Attributes = "javascript: this.disabled=true;" to make the button disabled and prevent users from clicking it again while form data still posting, there is no longer...
8
by: pamelafluente | last post by:
I am beginning aspNet, I know well win apps. Need a simple and schematic code example to start work. This is what I need to accomplish: ---------------------- Given button and a TextBox on a...
36
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
10
by: The Natural Philosopher | last post by:
I am coding up a bit of javascript stuff, and have managed to stumble my way through most of what I want.. But this one has got me stumped. I call submit() and get a javascript error 'Submit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.