Brian Ng Brian Ng - 1 year ago 209
SQL Question

C# How to get DataTable value

I still new to C# and just have 3 months plus of learning process. I would like to seeking advice on how to extract data value from my DataTable I have create for validation checking.

private void checker
string sqlSelect2 = "SELECT a.AccNo, a.CompanyName , a.CreditLimit, FROM Debtor a JOIN";

which i have named TableCehecker, i do not need to put it to gridview, just for checking purposes.

In the private void process how can I extract dataTable TableCehecker and the value?

Thank you,

Answer Source

You need to follow the Docs from MSDN:

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;


reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

DataTable dt = new DataTable();


You can then check whatever you want of data inside the DataTable either by querying or by looping through the rows and checking the column values.

Based on your comments, to extract values from single row:

DataRow drow = dt.Rows[0];
string value = drow.Field<string>("CompanyName");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download