Well you could always do as follows. And since your a new to.Net I'll try and outline things out for you:
1. Create two project in a solution. The first project is aclass library the second is a asp.net web application.
2. In the asp.net web application go to references, click on theprojects tab under the add reference option, and select theclass library project.
3. time to write some code.
4. in the class library setup a subroutine that return a dataset.For example:
//Begin Class Example Here
using System;
using System.Data;
using System.Data.SqlClient;
namespace Sample1
{
public class Sample
{
public Sample() {}
public string ConnectionString;
public string Param1;
public DataSet ViewItems(string val)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection Cn = new SqlConnection(ConnectionString);
SqlCommand Cmd = new SqlCommand([STORED PROCEDURE NAME HERE],Cn);
Cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pValue1 = Cmd.Parameters.Add("@Param1",SqlDbType.VarChar);
pValue1.Value = Param1;
Cn.Open();
da.SelectCommand = Cmd;
da.Fill(ds, "VALUES");
}
}
}
//End Class Example
5. Now in the ASP.Net web application double click on the form togo to the code behind page. Now you can create a new Sampleobject and run the dataset. For example:
//Begin Webform1 Example Here
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using Sample1;
namespace SampleProject
{
public class Webform1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}
public void BindData()
{
DataSet ds = new Dataset();
Sample s = new Sample();
s.ConnectionString =ConfigurationSettings.AppSettings["ConnectionString"];
s.Param1 = "Item1";
ds = s.View();
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
}
}
// End Webform1 Example
Okay, so what did I do here. First I just create a class filethat would get information from a database and set it to adataset. Then on the webform I reference that class andsubroutine and execute it. The I bind the information to arepeater control on the web form.
Hopefully this example will give you so ideas as to how toproceed. Not sure if the example will work word for word -- I'mnot exactly testing it -- but it should be close
One thing to note incase it's not entirely evident, the"ConfigurationSettings.AppSettings["ConnectionString"]" is avalue from the web.config file. Just an exmaple of where tostore the connection string.
Alan Washington
http://www.aewnet.com
I would like to first start off saying that this is pretty muchmy first week
of heavily getting into ASP.net. I previously have programmedin ASP Classic,
and now understanding the benefits of ASP.net I would like tocross over.
Anyways onto my question. I am curious how i can go aboutsetting up a class
with the connection to my database in it, which any page canuse to connect
to that database. I was never heavily in classes or functionsduring my ASP
years, so please bare with me.
Thank you so much for all your help...
Also if you know of any good example sites with source i canlook at and
read that might further my education on this subject, sincethat is the best
way i can learn..
~Devin
User submitted from AEWNET (
http://www.aewnet.com/)