Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The below code returns a single row from table,how to get multiple row from db as a list using the below code

C#
    public BusinessObj.Tables.GetLocal GetItem(object TransId)
    {
     try
      {
        BusinessObj.Tables.GetLocal objGetLocal;
        string spName = "spGetLocal";
        Database db = DatabaseFactory.CreateDatabase(ConnectionString);
        DbCommand dbCommand = db.GetStoredProcCommand(spName);
        db.AddInParameter(dbCommand, "@billId", DbType.Int32, ((int)TransId));
        DataTable dt = new DataTable();
        dt = db.ExecuteDataSet(dbCommand).Tables[0];
        objGetLocal = new BusinessObj.Tables.GetLocal();                
        if (dt.Rows.Count != 0)
         {
          objGetLocal=(BusinessObj.Tables.GetLocal)FillRecords(dt.Rows[0], objGetLocal);
         }
    return objGetLocal;
      }
}
Posted
Updated 17-Jun-14 18:50pm
v4
Comments
Nandakishore G N 18-Jun-14 1:35am    
Try this article
http://www.codeproject.com/Articles/38253/Converting-a-DataSet-to-a-Generic-List

1 solution

Hope the below modified code will definitely of help to you :-

First of all include the header
C#
System.Collections.Generic;



Then here is the modified code:

C#
public List<businessobj.tables.getlocal> GetItem(object TransId)
{
 try
 {
   List<businessobj.tables.getlocal> objGetLocalList = new List<businessobj.tables.getlocal>();
   string spName = "spGetLocal";
   Database db = DatabaseFactory.CreateDatabase(ConnectionString);
   DbCommand dbCommand = db.GetStoredProcCommand(spName);
   db.AddInParameter(dbCommand, "@billId", DbType.Int32, ((int)TransId));
   DataTable dt = new DataTable();
   dt = db.ExecuteDataSet(dbCommand).Tables[0];
   foreach(DataRow dr in dt.Rows)
   {
	BusinessObj.Tables.GetLocal objGetLocal = new BusinessObj.Tables.GetLocal();
	objGetLocal=(BusinessObj.Tables.GetLocal)FillRecords(dr, objGetLocal);
	objGetLocalList.Add(objGetLocal);
   }
   return objGetLocalList;
 }
}


Tried my best give you a working copy of code, still if it is not working i hope you will get the proper idea on how to accomplish your goal.

Happy coding.
 
Share this answer
 
v3
Comments
MAGuru 18-Jun-14 1:13am    
An error Occured

Foreach statement cannot operate on variables of type System.Data.DataTable Because It does not contain a public definition for GetEnumerator

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