ASP.NET Form and DB Connectivity

Posted on 2006-05-23
Last Modified: 2010-04-17
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.
Question by:igorblackbelt
    LVL 32

    Expert Comment

    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.
    LVL 2

    Accepted Solution

    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.
    LVL 1

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now