ASP.NET question (easy)

Martin

Lifer
Jan 15, 2000
29,178
1
81
so I'm just learning asp.net, but I can't seem to access the data stored in a database.

I have a database with a tables named "Users" with three columns: id, alias, password.

Now, I made and filled a DataSet and mapped the users Table to it, but I can't access the data in individual cells.

here is the code I am having trouble with


foreach (DataRow myRow in dsUsers.Tables["users"].Rows) {
msg = myRow["alias"];
}

alias.Text = msg.ToString();


The problem is, this returns an object, and I want the actual value inside the cell (string in this case)


 

DAGTA

Diamond Member
Oct 9, 1999
8,172
1
0
I wrote almost entirely in C# so my VB syntax isn't as strong. Look for a .Text or a .Value member inside of that object. I think that will get you what you want.
-DAGTA
 

GtPrOjEcTX

Lifer
Jul 3, 2001
10,784
6
81
haven't used .net but the old asp way was to use the .value to get it...maybe that still works.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
I don't quite get what you're asking.

myRow["alias"] should get the value of the "alias" column in your DataTable, which is of type Object. When you call myRow["alias"].ToString(), it should get you the value as a string. (In other words, I don't see what's wrong with what you've written)

What are you seeing in alias.Text instead?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
I tried your code in a quick web page I threw together. I preceded your code with some additional code to create the DataSet. Here is what I have below:

DataSet dsUsers = new DataSet();
DataTable userTable = dsUsers.Tables.Add("users");
userTable.Columns.Add("id", typeof(int));
userTable.Columns.Add("alias", typeof(string));
userTable.Columns.Add("password", typeof(string));

userTable.Rows.Add(new object[] { 1, "user1", "pwd1" });
userTable.Rows.Add(new object[] { 1, "user2", "pwd2" });

object msg = null;
foreach (DataRow myRow in dsUsers.Tables["users"].Rows)
{
msg = myRow["alias"];
}

alias.Text = msg.ToString();

The code works fine. The foreach loop runs until we reach the second row, and the text assigned to alias.Text is set to "user2". Is this what you're getting?
 

Martin

Lifer
Jan 15, 2000
29,178
1
81
oog, your code does work, so it seems there must be something wrong with my database connection (perhaps I have no mapped the tables correctly or something)

Thanks for your help.
 

Martin

Lifer
Jan 15, 2000
29,178
1
81
I guess I haven't initialized the dataset tables properly, because when I use my dataset instead of constructing one like you did, I get an error saying object points to null reference.

I will work on the page later and post an update.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Can you show how you are getting data into the DataSet? You can hide your connection string if that contains some kind of user id and password.
 

Martin

Lifer
Jan 15, 2000
29,178
1
81
Originally posted by: oog
Can you show how you are getting data into the DataSet? You can hide your connection string if that contains some kind of user id and password.

Oh no, I just thought it was irrelevant, that's why I didn't post it. But I found out my problem yesterday, and now it works alright.

The problem was that I was calling

daUsers.Fill(dsUsers); (dataadapter filling in a dataset)

when I changed it to

daUsers.Fill(dsUsers, "users");

everything worked out fine. I guess the first method didn't properly transfer the tables from the database into the dataset.



 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Yeah, without that second parameter, I think it doesn't assign a name to the table in the DataSet. You can still access it as dsUsers.Tables[0], but not as the table "users".
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: oog
Yeah, without that second parameter, I think it doesn't assign a name to the table in the DataSet. You can still access it as dsUsers.Tables[0], but not as the table "users".

I'm late to this thread, but you are correct. It by default assigns the names incrementally: Table1, Table2, ...

You can also generate a typed dataset to access the tables/fields in a typesafe manner.