Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to read multiple data from a table. Say there are five rows and five columns.
Now , will this piece of code work, what i have implemented?

C#
if (reader.HasRows)
          {
              while (reader.Read())
              {
                  var dataRead = reader.GetValues().ToString().Trim();
              }
          }
          else
          {
              reader.Close();
          }

Any suggestions ??
Posted
Updated 2-Jul-14 21:26pm
v3
Comments
George Jonsson 3-Jul-14 2:41am    
Why not use the debugger and check yourself? :)
Jörgen Andersson 3-Jul-14 2:47am    
Yes it will work.
But will it do what you expect it to do?

To answer that you will have to tell us what you wanted it to do.
Anshumaan Chaturvedi 3-Jul-14 2:53am    
I have to save it in a list or may be in an array

1 solution

Learn to find your answer from: Retrieving Data Using a DataReader[^]
 
Share this answer
 
Comments
Anshumaan Chaturvedi 3-Jul-14 3:27am    
public SqlDataReader GetRegistrationInfoSetting(string categoryName, int instanceID)
{
string sqlCmdText = "usp_GetRISettingByInstanceIdAndCategoryId";
SqlParameter[] param = new SqlParameter[1];
SqlParameter InstanceId = new SqlParameter("@InstanceId", instanceID);
SqlParameter Category_Name = new SqlParameter("@CategoryName", categoryName);
param[0] = InstanceId;
param[1] = Category_Name;

SqlDataReader reader=SqlHelper.ExecuteReader (Dsn, System.Data.CommandType.StoredProcedure, sqlCmdText,param);


if (reader.HasRows)
{
while (reader.Read())
{
var dataRead = reader.GetValue(0).ToString().Trim();

}
}
else
{
reader.Close();
}
return reader;

}
}
This reader which is of SqlDatareader type, will it hold all the records present in table?
Nguyen.H.H.Dang 3-Jul-14 5:16am    
Eer... may be an impolite question, but do you know how to debug your code ?

Anshumaan Chaturvedi 3-Jul-14 5:37am    
I do. I should attach it to .exe project but i am not allowed to do that. It is working with unknowing thing.

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