ASP.net using a form to insert data into an sql server table ASP.net using a form to insert data into an sql server table asp.net asp.net

ASP.net using a form to insert data into an sql server table


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:

<form id="form1" runat="server"> 

you create your textboxes and define them to runat="server" like so:

<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>

define a button to process your logic like so (notice the onclick):

<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />

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

protected void Button1_Click(object sender, EventArgs e)

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)

protected void Button1_Click(object sender, EventArgs e){   string name = TxtName.Text; // Scrub user data   string connString = ConfigurationManager.ConnectionStrings["yourconnstringInWebConfig"].ConnectionString;   SqlConnection conn = null;   try   {          conn = new SqlConnection(connString);          conn.Open();          using(SqlCommand cmd = new SqlCommand())          {                 cmd.Conn = conn;                 cmd.CommandType = CommandType.Text;                 cmd.CommandText = "INSERT INTO dummyTable(name) Values (@var)";                 cmd.Parameters.AddWithValue("@var", name);                 int rowsAffected = cmd.ExecuteNonQuery();                 if(rowsAffected ==1)                 {                        //Success notification                 }                 else                 {                        //Error notification                 }          }   }   catch(Exception ex)   {          //log error           //display friendly error to user   }   finally   {          if(conn!=null)          {                 //cleanup connection i.e close           }   }}


Simple, make a simple asp page with the designer (just for the beginning) Lets say the body is something like this:

<body>    <form id="form1" runat="server">    <div>        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>        <br />        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    </div>    <p>        <asp:Button ID="Button1" runat="server" Text="Button" />    </p>    </form></body>

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:

void Button1Click(Object sender,EventArgs e) { }

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:

String firstBox = TextBox1.Text;

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.