C# .NET - Getting data from DataTable? - Asked By Billy Leo on 28-Jan-07 12:47 PM

Hey,
I would like to retrieve data from my data table to put into a textbox.
The datatable is populated with this code:
DataSet1.CurrentNewsDataTable NewsDT = NewsAdapter.GetDataByIDPASSED(ID);

The data set has 3 columnsL ArticleID ; HEading ; Paragraph.

I would like to set Textbox.text = NewsDT.Rows[0].Column["Heading"];

Is this the right coding for this?

If you only have one row with data in your - Erik Little replied to Billy Leo on 28-Jan-07 01:20 PM

dataset then you should be ok. However, if your dataset has more than one row  and you need to display something more than the first row (index) then you will need to do some looping..


Erik

DataTable Enhancements in ADO.NET 2.0 use DataTableReader - K Pravin Kumar Reddy replied to Billy Leo on 28-Jan-07 01:47 PM

use DataTableReader

DataReaders are much faster than DataSets and consume less http://www.sql-server-performance.com/jk_ado_2_datable.asp#. However, the major drawback of using DataReaders in the earlier versions of ADO.NET was that it always required an open connection to operate, i.e., it was connection oriented. Hence we needed to explicitly close the http://www.sql-server-performance.com/jk_ado_2_datable.asp# connections when we were done using it. With ADO.NET 2.0, a DataTableReader class has been introduced that is similar to other data readers but with one exception – it works in a disconnected mode.

Creating a DataTableReader

The CreateDataReader method of the DataTableReader class can be used to create a DataTableReader. The following listing shows how this is done.

public DataTableReader GetDataTableReader (string connectionString)
{
SqlConnection sqlConnection  = new SqlConnection(connectionString);
sqlConnection .Open()
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(“Select * from States”, sqlConnection);
DataTable dataTable = new DataTable ("States");
sqlDataAdapter.Fill(dataTable);
DataTableReader datatableReader = dataTable.CreateDataReader();
sqlConnection.Close();
return datatableReader;
}

The DataTableReader is a light-weight, forward-only set of data that maintains the same structure as a DataTable, i.e., it exposes the same rows and columns as the DataTable. The following listing illustrates how we can read data using a DataTableReader instance.

String connectionString = …; //Some connection string to connect to the database
DataTableReader dataTableReader = GetDataTableReader(connectionString);
Console.WriteLine(“Displaying the codes for all the states:--“);
while (dataTableReader.Read())
{
Console.WriteLine(DataTableReader[“codeID”].ToString());
}

Ability to work with multiple tables seamlessly


When we create a DataTableReader from a DataSet that contains multiple tables, the DataTableReader instance would also contain multiple resultsets, one per each DataTable. We can iterate through all these resultsets using the NextResult method. This is shown in the code example that follows.
String connectionString = …; //Some connection string to connect to the database

DataTableReader dataTableReader = GetDataTableReader(connectionString);
while(dataTableReader.NextResult())  // Iterate through all the resultsets
{
          while(dataTableReader.Read()) //Iterate through all the records in a resultset
          {
              //Some code       
          }
}

Loading Data to a DataTable from a DataReader

The Load method of the DataTable class of ADO.NET 2.0 can be used to load a DataReader instance directly into a DataTable instance. The following listing shows how it can be accomplished.

SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
sqlConnection.Open();
SqlCommand command = new SqlCommand(“Select name, population, area from States”, sqlConnection);
SqlDataReader sqlDataReader = command.ExecuteReader();
DataTable dataTable = new DataTable ();
dataTable.Load(sqlDataReader, LoadOption.OverwriteChanges);

reference

http://www.sql-server-performance.com/jk_ado_2_datable.asp

Yes, but you will probably need to cast the item - Peter Bromberg replied to Billy Leo on 28-Jan-07 03:25 PM

to type String:

Textbox.text = (string)NewsDT.Rows[0].Column["Heading"];


Yes You can do - Rajesh Madhukar replied to Billy Leo on 29-Jan-07 12:16 AM

Yes, You can get it.

Check for null value return also.

        TextBox.Text=if(NewDT.Tables[0].Rows[0].["Heading"]!=null? NewDT.Tables[0].Rows[0].["Heading"].ToString():'');

Rajesh Madhukar


Re :: Getting Data From DataTable - Shailendrasinh Parmar replied to Billy Leo on 29-Nov-08 06:36 AM

See the following modified code

DataSet1.CurrentNewsDataTable NewsDT = NewsAdapter.GetDataByIDPASSED(ID);

The data set has 3 columnsL ArticleID ; HEading ; Paragraph.

txtHeading.text = NewsDT.Rows[0]["Heading"].ToString();
txtParagraph.text = NewsDT.Rows[0]["Paragraph"].ToString();

Hope this helps.

eliza replied to Billy Leo on 18-Mar-10 06:09 AM
In a application if  you wants to take data from two table which have no relation to each other.At this situation you write two separate SQL query to take data from data base there is a work around to take data from multiple table in single a query.
 For more details logon http://www.mindfiresolutions.com/NextResult-in-C-or-ADONET-data-reader-705.php