Hi in php i would do a form with an action to lets say a process.php page and in that page i would take the post values and using a mysql_query would do an insertion. now i am lost ,i am trying to create a from and do an inserstion in ASP.net with sql server 2008 using visual studio 2010.
i have defined a sql db in the App_Data folder. basicly what i need (unless there is a better way) is:
- how do i get the post values.
- how do i insert them into the db.
thanks.
There are tons of sample code online as to how to do this.
Here is just one example of how to do this: http://geekswithblogs.net/dotNETvinz/archive/2009/04/30/creating-a-simple-registration-form-in-asp.net.aspx
you define the text boxes between the following tag:
you create your textboxes and define them to runat="server" like so:
define a button to process your logic like so (notice the onclick):
in the code behind, you define what you want the server to do if the user clicks on the button by defining a method named
or you could just double click the button in the design view.
Here is a very quick sample of code to insert into a table in the button click event (codebehind)
Simple, make a simple asp page with the designer (just for the beginning) Lets say the body is something like this:
Great, now every asp object IS an object. So you can access it in the asp's CS code. The asp's CS code is triggered by events (mostly). The class will probably inherit from System.Web.UI.Page
If you go to the cs file of the asp page, you'll see a protected void Page_Load(object sender, EventArgs e) ... That's the load event, you can use that to populate data into your objects when the page loads.
Now, go to the button in your designer (Button1) and look at its properties, you can design it, or add events from there. Just change to the events view, and create a method for the event.
The button is a web control Button Add a Click event to the button call it Button1Click:
Now when you click the button, this method will be called. Because ASP is object oriented, you can think of the page as the actual class, and the objects will hold the actual current data.
So if for example you want to access the text in
TextBox1
you just need to call that object in the C# code:In the same way you can populate the objects when event occur.
Now that you have the data the user posted in the textboxes , you can use regular C# SQL connections to add the data to your database.