Click here to Skip to main content
15,885,983 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on c# console application

I need to add data table rows to list using c#

so How to do that please ?

so How to add rows from data table to object list ?

meaning

How to store
row[0].ToString()
on list .

What I have tried:

C#
string query = @"SELECT WFAN8 FROM CRPDTA.F600004A WHERE (LTRIM(WFROLE) = 'PLNG')";
 DataTable dt = JdeDataAccess.GetRecords(query);
 if (dt.Rows.Count > 0)
 foreach (DataRow row in dt.Rows)
row[0].ToString();
Posted
Updated 4-Feb-23 12:54pm
Comments
Dave Kreskowiak 4-Feb-23 20:04pm    
You cannot call .ToString on a DataRow object. You'll get a string that says "System.Data.DataRow", not some representation of your data.

You're not providing enough information to give you any sensible answer. What EXACTLY do you want in the List and what does the source data look like?

1 solution

Why not something like:
C#
string connString = @"your connection string here";
string query = "select * from table";

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();

// create a data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);

// this will query your database and return the result to your data table
da.Fill(dataTable);

conn.Close();
da.Dispose();

NOTES:
* The data table field must be initialized before calling da.Fill(dataTable)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900