Accessing a value from a dataset

G

Guest

Using c# I'm trying to access a value from a dataset using the following code.

keyword.Text = myDataSet.Tables[0].Columns["keyword"].ToString();

but rather than getting the value at that location I just get the value
'keyword'.
I've also tried this without success.

keyword.Text = myDataSet.Tables[0].Rows[0].ToString();
 
G

Guest

that wont work in c#

but this will, I found the answer....

keyword.Text = myDataSet.Tables[0].Rows[0].ItemArray[0].ToString();
 
C

Craig Deelsnyder

that wont work in c#

but this will, I found the answer....

keyword.Text = myDataSet.Tables[0].Rows[0].ItemArray[0].ToString();

Note you could also just use brackets [] instead of parens () in the
previous answer's code. That's the only difference in syntax for this
statement....well, that and the ; at the end of each statement :)
 
G

Guest

Note you could also just use brackets [] instead of parens () in the
previous answer's code. That's the only difference in syntax for this
statement....well, that and the ; at the end of each statement :)

Well actually the other difference is that Item(0) or Item[0] will not work
as well. It does not exist in c#. You need to use ItemArray ( which is crap
actaully ). Oh well.
=)
 
G

Guest

Just got it..

Label1.text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
--
:: KHAMAL ::


Protoculture said:
Note you could also just use brackets [] instead of parens () in the
previous answer's code. That's the only difference in syntax for this
statement....well, that and the ; at the end of each statement :)

Well actually the other difference is that Item(0) or Item[0] will not work
as well. It does not exist in c#. You need to use ItemArray ( which is crap
actaully ). Oh well.
=)
 
Joined
Jun 29, 2007
Messages
1
Reaction score
0
I think I know why this is

Basically the reason u need the itemarray is because otherwise it sees it as a collection and does not know which item you need, thats why the text does not come back.

Basically u need to refer to an instance of an object and not a collection.

PS.

I am very new to .NET so I am still learning but I think thats the reason.
 
Joined
Oct 31, 2007
Messages
1
Reaction score
0
Another way

This is just another way to pull data out. I like this way because you are using the column name and not just using an index of the array item

ds.Tables[0].Rows[0]["ColumnName"].ToString()
 
Joined
Feb 5, 2011
Messages
2
Reaction score
0
Dataset

that wont work in c#

but this will, I found the answer....

keyword.Text = myDataSet.Tables[0].Rows[0].ItemArray[0].ToString();

Yes, it is really helpful for me. I am stucking since 2 hours for retrieving particular value from dataset. now i got it.

Here i am using the stored procedure and its name is "SearchUser"

SqlCommand cmd = new SqlCommand("SearchUser", con);
cmd.Parameters.AddWithValue("@name", txtSearch.Text);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"ddd");
DataTable dt = new DataTable();

GridView1.DataSource = ds;
string id = ds.Tables[0].Rows[0].ItemArray[0].ToString();

GridView1.DataBind();
Thanks.
Happy Codindg
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top