Riddhi Valecha

Riddhi Valecha

  • 444
  • 3.3k
  • 412.8k

Access values in list

Oct 16 2019 9:21 AM

Dear Team,

I am returning the following message from a stored procedure -

 
SELECT '1' AS Code, 'Success' AS Message;
 
------------
In my MVC Project , I need to access this value "1" from a list ,
 
I am sharing my code.
 
Please guide -
-------------
public List<Dictionary<string, object>> AddEquipMaster(AddEquipment equipmaster)
{
List<Dictionary<string, object>> o = null;
DataTable dt = null;
string message = string.Empty;
DBManager db = new DBManager();
try
{
var parameters = new List<IDbDataParameter>();
parameters.Add(db.CreateParameter("@EquipmentCodeParam", equipmaster.EquipmentCode , DbType.String));
parameters.Add(db.CreateParameter("@EquipmentNameParam", equipmaster.EquipmentName, DbType.String));
parameters.Add(db.CreateParameter("@LocationCodeParam", equipmaster.LocationCode, DbType.String));
parameters.Add(db.CreateParameter("@ServiceCodeParam", equipmaster.ServiceCode, DbType.String));
parameters.Add(db.CreateParameter("@IsActiveParam", equipmaster.IsActive, DbType.String));
parameters.Add(db.CreateParameter("@CreatedByParam", equipmaster.CreatedBy, DbType.String));
IDbDataParameter[] arrParams = parameters.ToArray();
dt = db.ExecuteDataTableWithOutParameter(CommandType.StoredProcedure, "SP_SW_Add_EquipmentMaster", ref arrParams);
if (dt.Rows.Count > 0)
o = GenerateList.ConvertDataTableToList(dt);
else
o = GenerateList.ConvertDataTableToList(dt);

}
catch (Exception err)
{
err.Message.ToString();
throw;
}
finally
{
db.Dispose();
}
return o;
}
--------------------------
Issue is here -
[HttpPost]
[Route("AddEquipment")]
public IActionResult AddEquipment(AddEquipment equipment)
{
Response response = new Response();
try
{
if (ModelState.IsValid)
{
var lstRecords = new DAL.DALEquipment().AddEquipMaster(equipment);
lstRecords[0].Keys.ToString();
foreach(var k in lstRecords )
{

}
if (lstRecords != null && lstRecords.Count > 0)
{
response.Data = lstRecords;

response.Code = (int)HttpStatusCode.OK;
}
else
{
response.Code = (int)HttpStatusCode.BadRequest;
response.Message = ResponseMessage.InsertErrorMessage;
}
}
}
catch (Exception ex)
{
response.Code = (int)HttpStatusCode.BadRequest;
response.Message = ResponseMessage.InternalServerError;
throw new CustomException(ex.Message, ex);
}
return StatusCode(response.Code, response);
}
--------------
public static List<Dictionary<string, object>> ConvertDataTableToList(DataTable dt)
{
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach(DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach(DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return rows;
}
 
 
 

Answers (1)