473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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.

Mar 22 '07 #1
3 1597
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

On Mar 21, 9:52 pm, itfet...@gmail.com wrote:
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.

Mar 22 '07 #2
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.

Mar 22 '07 #3
If you want rows and columns, bind the data to a GridView in 2.0, a DataGrid
in 1.1

If you want more fine tuned control, look at the Repeater object.

As in

<asp:repeater ......

http://www.asp101.com/articles/john/...er/default.asp
(this article is for 1.1, but you can learn from it)
http://www.gridviewguy.com/CategoryD...x?categoryID=7
<it******@gmail.comwrote in message
news:11********************@o5g2000hsb.googlegroup s.com...
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.

Mar 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Gollery | last post by:
I installed Postgres 8 beta 3 on an XP box, with Postgres running as a service. TaskManager tells me that postgres and postmaster are both running. Using pgAdmin III, I can connect to the server...
3
by: Chris | last post by:
Don't know if there is a simple solution for this one or not. When running SQL server on a machine with 2000 loaded and the complete SQL package I don't have any issues. Now I'm trying to login...
0
by: Quantum | last post by:
Hi there, I've got a VS2K5 project in the folder: c:\BigProject\Windows\ However, I've got all the source in this folder: c:\BigProject\Independent\
5
by: Odd Bjørn Andersen | last post by:
I have installed DB2 9 Enterprise Edition on my laptop and created the sample database. Now I'm having truble connecting to the database from Command Editor. If I connect from Command Window it's...
4
by: SteveW | last post by:
Now that vista and the 3.0 framework are gold, I'm a bit confused, so any clarification would be very helpful. The 3.0 framework is gold and is downloadable etc. What is not 'gold' and...
3
by: =?Utf-8?B?QWRpbCBBa3JhbQ==?= | last post by:
I posted this question in vsnet.ide newsgroup about more than a week back but didn't get any single response yet therefore, reposting again same to several newsgroups. When I run my C#...
3
by: ist | last post by:
Hi, I am trying to get (and transfer over ASP.NET) some encrypted data from some MySQL fields. Since the data contains many unicode characters, I tried to get the data as a series of ASCII...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
30
by: iheartvba | last post by:
Hi, I already have 3 Databases running: A. they all have the same tables and the same structure B. There is no 1 Master table they are all separate tables What I want to do is to merge them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.