Total number of columns in current row
How can I get the total number of columns in the current row of a SqlDataReader instance?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted May 2, 2012, 1:48 AM EDT
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.fieldcount.aspx
Ilkin East100 reputationPosted May 2, 2012, 1:53 AM EDT
You can do your problem with below code.
DataTable dt = new DataTable();
dt.Columns.Add("Test", typeof(String));
dt.Rows.Add(10);
dt.Rows.Add(20);
dt.Rows.Add(30);
// Loop
int sum = 0;
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState != DataRowState.Deleted)
sum += Convert.ToInt32(dr["Test"]);
}
Console.WriteLine("Sum is {0}.", sum); // Prints 60
Thanks
SenthilkumarRank #387 · 6.5K reputationPosted May 2, 2012, 3:04 AM EDT
There is an property called FieldCount, which gets the number of columns in the current row.
Hope this will help you.
MuthuMari MRank #5,738 · 288 reputationPosted May 2, 2012, 3:21 AM EDT
use fieldcount property.
private void orderIDsList_SelectedIndexChanged
(object sender, System.EventArgs e)
{
SqlCommand storedProcCommand =
new SqlCommand ("CustOrdersDetail", con);
storedProcCommand.CommandType = CommandType.StoredProcedure;
storedProcCommand.Parameters.Add("@OrderID",orderID);
con.Open ();
ArrayList rowList = new ArrayList();
SqlDataReader reader = storedProcCommand.ExecuteReader();
while (reader.Read())
{
// Create an array big enough to hold the column values
object[] values = new object[reader.FieldCount];
// Get the column values into the array
reader.GetValues(values);
// Add the array to the ArrayList
rowList.Add(values);
}
}
if you want more pls visit this url
http://www.akadia.com/services/dotnet_data_reader.html
thanks.