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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,"Tes tName","00 0-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.StoredProcedur e;
//now you need to tell your stored procedures the value to insert
cmd.Parameters.Add(new SqlParameter("@id",SqlDbTy pe.int,4)) ;
cmd.Parameters["@id"].Valu e = id;
cmd.Parameters.Add(new SqlParameter("@firstName", SqlDbType. VarChar,50 ));
cmd.Parameters["@firstName "].Value = firstName;
cmd.Parameters.Add(new SqlParameter("@phoneSqlDbT ype.VarCha r,50));
cmd.Parameters["@phone"].V alue = phone;
//now execute it
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw new ApplicationException("Erro r "+err.Message.ToString());
}
finall
{
con.Close();
}
}//end function
}
}
}
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,"Tes
}
}
//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"
cmd.CommandType = CommandType.StoredProcedur
//now you need to tell your stored procedures the value to insert
cmd.Parameters.Add(new SqlParameter("@id",SqlDbTy
cmd.Parameters["@id"].Valu
cmd.Parameters.Add(new SqlParameter("@firstName",
cmd.Parameters["@firstName
cmd.Parameters.Add(new SqlParameter("@phoneSqlDbT
cmd.Parameters["@phone"].V
//now execute it
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw new ApplicationException("Erro
}
finall
{
con.Close();
}
}//end function
}
}
}
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