Uploaded by Liyana Ross

Submitting Form Data to a Database with a Submit Button The ASP

advertisement
ASP.NET
Home
Get Started
Learn
Hosting
Downloads
Community
Forums
Help
Home / ASP.NET Forums / General ASP.NET / Web Forms / Submitting Form Data to a Database with a Submit Button
Submitting Form Data to a Database with a
Submit Button [Answered]
11 replies
Last post Sep 09, 2009 02:50 PM by mgarcia0720
Submitting Form Data to a Database with a Submit Button
Aug 19, 2009 11:51 AM | GaryDiehl31
Ok, I know this has got to be the easiest thing to do but I just can't figure it out. I have already searched the forums, and everywhere
else but can't find an answer. Please Help!
All I want to do is have 2 text boxes and a submit button that inputs data into my database. The connection string works fine I just
don't know how to "bind" the submit button to the text boxes so it knows what tables and fields to post to in the database. I'm using
visual web developer express.
Thanks For Your Help
Gary
DATABASE
FORMS
SUBMIT BUTTON
Re: Submitting Form Data to a Database with a Submit Button
Aug 19, 2009 12:37 PM | MetalAsp.Net
You could, of course, do it manually using all code behind code, or you can make use of maybe a FormView.
Refer here: http://www.asp.net/learn/data‐access/tutorial‐14‐vb.aspx ﴾http://www.asp.net/learn/data‐access/tutorial‐14‐vb.aspx﴿
Re: Submitting Form Data to a Database with a Submit Button
Aug 19, 2009 01:51 PM | GaryDiehl31
Thanks, I tried form view with the text boxes and the button inside, then linked the form view to my datasource...didn't work. It
submitted but never posted to the database.
Is there somewhere I could look to see how to do in code behind?
Re: Submitting Form Data to a Database with a Submit Button
Aug 19, 2009 02:02 PM | MetalAsp.Net
Well to do it in the code behind, you would setup the connection/sqlcommand execute the command and then set the .Text property
of the textboxes to whatever results you got from the DB.
Look at this simple example here: http://msdn.microsoft.com/en‐us/library/system.data.sqlclient.sqlcommand.aspx
﴾http://msdn.microsoft.com/en‐us/library/system.data.sqlclient.sqlcommand.aspx﴿
I'll only say that instead of using indexes to access the results in the reader ﴾like the example shows﴿, I'd use the name of the field. I
think it makes for cleaner, more readable and less error prone code.
Re: Submitting Form Data to a Database with a Submit Button
Aug 19, 2009 03:20 PM | GaryDiehl31
There's not a simple "onClick" submit kind of thing?
I went back and tried putting the text boxes and submit button within a formview. The button is still not in any way that I can see
connected to the text boxes. It sure seems like this should be simple enough. The "new" and "update" commands work fine in the
formview when adding to the database, just not my textboxes and buttons
Re: Submitting Form Data to a Database with a Submit Button
Aug 19, 2009 09:45 PM | GaryDiehl31
I think what I'm looking for is the actual VB code that would go in the code behind that triggers an onclick event handler. Please Help!!
Re: Submitting Form Data to a Database with a Submit Button
Aug 20, 2009 01:08 AM | venkatu2005
GaryDiehl31
All I want to do is have 2 text boxes and a submit button that inputs data into my database. The connection string works fine I just
don't know how to "bind" the submit button to the text boxes so it knows what tables and fields to post to in the database. I'm
using visual web developer expre
Have you writtent the insert query on your Submit button to Insert the data to database
if possible let you post the code ?
Re: Submitting Form Data to a Database with a Submit Button
Aug 20, 2009 01:53 AM | akmsaeea
Submit click..You wnat to write the insert code:
string s="insert into clientcred﴾id,name﴿values﴾'" + txt1.Text+ "','" + txt2.Text + "'﴿";
con.Open﴾﴿;
SqlCommand cmd4 = new SqlCommand﴾s, con﴿;
cmd4.ExecuteNonQuery﴾﴿;
con.Close﴾﴿;
Re: Submitting Form Data to a Database with a Submit Button
Aug 20, 2009 11:21 AM | GaryDiehl31
Guys I'm a big time newbe here so please dumb it down for me if possible. Here is my code: just 2 text boxes and a button. What I
need to know is what goes in the code behind for the button click event. If I'm oversimplifying it, please let me know. Thanks
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:GaryDiehlConnectionString %>"
DeleteCommand="DELETE FROM [Orders] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [Orders] ﴾[Name], [Number]﴿ VALUES ﴾@Name, @Number﴿"
SelectCommand="SELECT * FROM [Orders]"
UpdateCommand="UPDATE [Orders] SET [Name] = @Name, [Number] = @Number WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Number" Type="Int32" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Number" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" />
</p>
</form>
Here is the code behind:
Protected Sub NewButton_Click﴾ByVal sender As Object, ByVal e As System.EventArgs﴿
﴾what goes here?﴿
End Sub
Re: Submitting Form Data to a Database with a Submit Button
Aug 20, 2009 06:09 PM | GaryDiehl31
Oh Guys, I knew the solution was going to be amazingly simple.
Use Details View, under properties, click Fields and change the button type properties for New, Insert, Cancel to Button and change the
text to "Submit"
Thanks for all the input though.
Re: Submitting Form Data to a Database with a Submit Button
Sep 02, 2009 11:58 AM | geogerf
Hi,
I have the same desire to create this: however your solution does not make sense to me.
1. "Details View" ‐ do you mean "Design View"?
2. Under "Properties" of the button, there is no "Fields" property?
I am confused. Thanks.
Re: Submitting Form Data to a Database with a Submit Button
Sep 09, 2009 02:50 PM | mgarcia0720
What he means is from the detailsview on your toolbar. Drag that into your page. then go into the "Details View Tasks" ﴾the little arrow
that shows up next to your dropped detail view﴿ and click enable inserting. Then go into the properties section on the right and Inserts
set to true. If you want to use SQL validation set the EnableModalValidation to true. Also you can choose what you want the default to
be. If this is solely for inserting or updating a form then just set it to default = Insert.
That worked like a charm for me. Spent 3 hours searching how to get this done properly. I will do some formating of it, but the code is
there, just replicate and move on..
Hope this helps!
OH the last part is that you can change the Insert text link to read Submit..
This site is managed for Microsoft by Neudesic, LLC. | © 2016 Microsoft. All rights reserved.
Download