Strings Strings Strings from sql ds.

G

Guest

New to C#. Ive got my data coming out in the form of a datagrid. I want to
take my data which comes from a "text" field inside sql server 2000.

Ive tried forcing the data into a string as:

string xyz= ds.Tables.["news"].Columns.["article"].ToString();
and
richTextBox1.DataBindings.Add( new Binding ("Text",ds,"news.article"));

but come up with an object type.

Must be a simple to gather data and place it into a string...

How can i get the data which is in Column "article" and place it into string
xyz?

Id like to bypass using the datagrid if at all possible..I imagine there is
a myriad of ways to display data??


Thanks for any help/pointers.

ccma
 
A

Anders Borum

Hello!
string xyz= ds.Tables.["news"].Columns.["article"].ToString();

This simple gets the object representation of the "article" column in the
"news" table. Getting data from the DataSet (ds) is a matter of:

1. Referencing the correct DataTable ("news")
2. Referencing the correct DataRow from the DataTable
3. Referencing the correct DataColumn from the DataRow

Usually you do a foreach iteration on a DataTable, accessing the DataRows
with a variable and using that to display some data.

foreach (DataRow row in ds.Tables["news"])
{
string xyz = row["article"].ToString();
// You could leave the ToString() part out if the column is a string
datatype
}

When databinding, you associate a databindable object with a datasource like
this:

// Associate datasource
dataGrid.DataSource = ds.Tables["news"];

// Ask the datagrid to start databinding to the datasource
dataGrip.DataBind();

Hopefully, this should get you started. If not, let us know.
 
G

Guest

Anders Borum said:

Salut!

Im using:

foreach (DataRow row in ds.Tables["it_daily_news"])
{
string xyz = row["article"].ToString();
}

where it_daily_news is the table used in the sql query:
string query = "select TOP 1 article from it_daily_news order by dateval
desc";

I
Unfortunately this bails on build with a reference to:

Form1.cs(93): foreach statement cannot operate on variables of type
'System.Data.DataTable' because 'System.Data.DataTable' does not contain a
definition for 'GetEnumerator', or it is inaccessible

eep.

Thanks for the help!
 
A

Anders Borum

Hello!

It was my impression that an enumerator from the DataTable was able to
enumerate the rows, but this was not the case. You simply have to change
your foreach to this:

// Notice the .Rows addition ..

foreach (DataRow row in ds.Tables["it_daily_news"].Rows)
{
string xyz = row["article"].ToString();
}
 
G

Guest

Fantastic. I am at home now. Tomorrow I will apply what you have written.
Thanks for the replys.

Ive been using php and building web applications , so I am familiar with
concepts of c#, but the syntax is confusing =>


Thanks again Anders.
 
A

Anders Borum

Hello!
Ive been using php and building web applications , so I am familiar with
concepts of c#, but the syntax is confusing =>

It's just a matter of getting to know the different constructs of the
language and the interfaces of the different types you work with. It could
have been the case that your foreach would have worked, if the BCL (base
class library) team had implemented an enumerator on the DataTable that
iterated the rows collection.
Thanks again Anders.

That's what we're here for ..
 

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

Top