ASP.NET Form and DB Connectivity

Hey Guys -

I created a web form in VS 2005 that needs to upload data to a SQL Database, I'm completely new to programming and need your help understanding how to create code that will load this data to my MS SQL 2000 table.
The form is created and I can run it and see the form. I also did some reasearch on my end and learned that I'll need to use ADO.NET to connect to my database, also learned that .NET has "Code behind pages" available to the programmer and now I don't know if I should use code behind pages or code on the pages like regular ASP.

Let me know if you can help, with your help, this is going to be my first .net app.
Who is Participating?
One of the big differences between ASP 3.0 and ASP.Net is that ASP.Net can have a "Code Behind" technology, which is that it can completely separate the programming logic to another file.  For the old asp, both server side code (VBScript or JScript) and the HTML code has to be written within one single file, and this is hard to manage as the project size growth since the file may become larger and messy.  I strongly suggest that programmer should use the code behind instead of writing everything into a single file.

If you want to have a basic introduction of ASP.Net without paying, try to visit the following website:

Hope this helps.
Let me suggest an excellent book that will help you understand not only the .NET basics but also how to connect to a SQL or other database.

Programming ASP.NET by Dino Esposito is my favorite.
I learned a large amount from the book APress Book "Pro ASP.NET 2.0 in C# 2005".

And as for your problem - there are many approaches to connect to a database.  Ideally, you would use a Stored Procedure for your SQL and as akumanova mentioned, in your Code Behind include your code to connect and execute the query.

A VERY simple example - in C# but you can do the same in VB

//event triggered by a button placed on your ASP page - this code would reside in your code-behind
public partial class HomePage : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
   {//anything need to be loaded?
   //button event
   protected void Button1_Click(object sender, EventArgs e)
       InserterDB inDB = new InserterDB();
       inDB.insertIntoDb(101,"TestName","000-000-0000"); //insert data from your textboxes places on the ASP page

//I usually create an external object that contains my code to access the database.  Lets call this InserterDB.

@id int,
@firstName varchar(50),
@phone varchar(50)
Insert AddressBook
(id,firstName,phone) values (@id,@firstName,@phone)
namespace InserterDB
    class InserterDB
       public void insertIntoDb(int id, int firstName, string phone)
      //Use the Stored Procedure, you must declare a sqlconnection
     SqlConnection con = new SqlConnection("put your connection string here");
     SqlCommand cmd = new SqlCommand("InsertAddress",con);
     cmd.CommandType = CommandType.StoredProcedure;
     //now you need to tell your stored procedures the value to insert
     cmd.Parameters.Add(new SqlParameter("@id",,4));
     cmd.Parameters["@id"].Value = id;
     cmd.Parameters.Add(new SqlParameter("@firstName",SqlDbType.VarChar,50));
     cmd.Parameters["@firstName"].Value = firstName;
     cmd.Parameters.Add(new SqlParameter("@phoneSqlDbType.VarChar,50));
     cmd.Parameters["@phone"].Value = phone;

    //now execute it
catch (SqlException err)
    throw new ApplicationException("Error "+err.Message.ToString());
}//end function

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.