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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
MuthuMari MPosted May 2, 2012, 7:21 AM
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.
SenthilkumarPosted May 2, 2012, 7:04 AM
There is an property called FieldCount, which gets the number of columns in the current row.
Hope this will help you.
Ilkin EastPosted May 2, 2012, 5:53 AM
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
VulpesPosted May 2, 2012, 5:48 AM
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.fieldcount.aspx