Link to home
Start Free TrialLog in
Avatar of igorblackbelt
igorblackbelt

asked on

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.
Avatar of jhance
jhance

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.

http://www.amazon.com/gp/product/0735619034/sr=8-5/qid=1148422652/ref=sr_1_5/104-8432408-1715948?%5Fencoding=UTF8
ASKER CERTIFIED SOLUTION
Avatar of akumanova
akumanova

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

//IN IN YOUR DATABASE CREATE A STORED PROCEDURE LIKE THIS
//
CREATE PROCEDURE insertName
@id int,
@firstName varchar(50),
@phone varchar(50)
AS
Insert AddressBook
(id,firstName,phone) values (@id,@firstName,@phone)
GO
//
//
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",SqlDbType.int,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
    try
{
    con.Open();
    cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
    throw new ApplicationException("Error "+err.Message.ToString());
}
finall
{
    con.Close();
}
}//end function

      }
   }
}