• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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.
0
igorblackbelt
Asked:
igorblackbelt
1 Solution
 
jhanceCommented:
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
0
 
akumanovaCommented:
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:

http://www.w3schools.com/aspnet/default.asp

Hope this helps.
0
 
realjordannaCommented:
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

      }
   }
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now