• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

ASP.NET question (easy)

Martin

Lifer
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)


 
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
 
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?
 
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?
 
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.
 
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.
 
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.
 
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.



 
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".
 
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.
 
Back
Top