asp.net, vs2k5, getting started connecting to databases... I'm confused

I

itfetish

I've been programming a bit in PHP and then recently learnt classic
ASP for projects at work, now I'm working on another project that they
want done in .net. I've got my head around web parts, that wasnt as
complicated as I thought it would be.

Now I want to create a web part, that connects and uses our database
to show data depending on what user it is.

I know theres the dataview controls and all that, but I dont want to
display all the data, I just want to have the data in a recordset like
you could in classic so then I can manipulate it and display it how I
want.

Can you even do that anymore, Ive been reading guides but they all
talk about data acess layers which seem like more complication than I
need.

Can anyone point me in the right direction, or link to a good simple
guide on doing such a thing, I'm really confused.
 
L

Liming

Welcome and forget about PHP! :)

It's a bit confusing when you first get started and the short answer
is yes.

Depending on how you want to display the data in your webparts, say
retrieve a list of users into a dropdown box, you can do your magic in
sql, use our data ADO.NET classes such as "DataReader", "DataSet" or
"DataTable", you can bind your results to them. A quick example using
sqlserver

SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;

try
{
// Open connection to the database
string ConnectionString = "server=xeon;uid=sa;"+
"pwd=manager; database=northwind";
con = new SqlConnection(ConnectionString);
con.Open();

// Set up a command with the given query and associate
// this with the current connection.
string CommandText = "SELECT FirstName, LastName" +
" FROM Employees" +
" WHERE (LastName LIKE @Find)";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;

// Add LastName to the above defined paramter @Find
cmd.Parameters.Add(
new SqlParameter(
"@Find", // The name of the parameter to map
System.Data.SqlDbType.NVarChar, // SqlDbType
values
20, // The width of the parameter
"LastName")); // The name of the source column

// Fill the parameter with the value retrieved
// from the text field
cmd.Parameters["@Find"].Value = txtFind.Text;

// Execute the query
rdr = cmd.ExecuteReader();

// Fill the list box with the values retrieved
lbFound.Items.Clear();
while(rdr.Read())
{
lbFound.Items.Add(rdr["FirstName"].ToString() +
" " + rdr["LastName"].ToString());
}
}
catch(Exception ex)
{
// Print error message
MessageBox.Show(ex.Message);
}
finally
{
// Close data reader object and database connection
if (rdr != null)
rdr.Close();

if (con.State == ConnectionState.Open)
con.Close();
}
txtFind.Text is a textbox value on the page and lbFound is the
dropdownlist name.

Example taken from http://www.akadia.com/services/dotnet_data_reader.html

Hope that helps.

Liming Xu
Jumptree Project Management
www.jumptree.com
 
I

itfetish

Thanks!

I think I have that sorted, Im getting data from my database and can
play with it like I want to.

The only thing is, Im doing that in the controls .cs file (the code
file), which is all well and good, but I want to just use some of it
in tables and as parts of hyperlinks on my web parts main display. How
to I share the data from my code file, onto the main display so I can
do normal ASP coding instead of just straight c sharp.


What I mean is, at the moment in my mainjobs.ascx.cs file I have

protected void Page_Load(object sender, EventArgs e)
{
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;

try
{
string ConnectionString =
"server=NEWK;uid=infobase;password=sdfsdf; database=Infobase";
con = new SqlConnection(ConnectionString);
con.Open();

string CommandText = "Select StaffID, ProjectNumber" +
" FROM Project_Staff" +
" WHERE (StaffID = 187)" +
" ORDER BY ProjectNumber";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;

rdr = cmd.ExecuteReader();
ArrayList rowList = new ArrayList();
while (rdr.Read())
{
object[] values = new object[rdr.FieldCount];
rdr.GetValues(values);
rowList.Add(values);

lblJobs.Text += rdr["ProjectNumber"].ToString() +
"<br>";
}

}
catch (Exception ex)
{
Response.Write("fail");
}
finally
{
if (rdr != null)
rdr.Close();

if (con.State == ConnectionState.Open) ;
con.Close();


}

}

what I want to do is do a loop and write some values to a table, in
hyperlinks etc on my normal mainjobs.ascx(or I could even start an
aspx file and use iframes) - how do I get the values from one to the
other?

I want to be able to do a while loop that creates table cells and
hyperlinks and so forth for the rdr["ProjectNumber"] variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top