Guest User

Guest User

  • Tech Writer
  • 189
  • 24.7k

how to retrieved data from given id and without using EF?

May 9 2018 2:57 AM
public List<ClientProduct> GetProducts(string id)
{
//
List<ClientProduct> clproduct = new List<ClientProduct>();
using (MySqlConnection con = new MySqlConnection())
{
con.ConnectionString = constr;
using (MySqlCommand cmd = new MySqlCommand())
{
//ClientProduct p = new ClientProduct();
cmd.Connection = con;
cmd.CommandText = "SELECT cm.ClientId,cm.ClientName, pa.ProductName,cp.ClProductId,cp.ProductId,cp.ParentProductId,cp.Act_Date,cp.Exp_Date,cp.ClientProductDetail "
+ "FROM ClientMaster cm inner join ClientProduct cp on cm.ClientId = cp.ClientID "
+ "inner join ProductMaster pa ON pa.ProductId = cp.ProductId where cm.ClientId=@ClientId";
cmd.Parameters.AddWithValue("@ClientId", id);
//cmd.Parameters.AddWithValue("@ClientId",id);
//where cm.ClientId='" + cp.ClientID + "'
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
while (sdr.Read())
{
ClientProduct cp = new ClientProduct();
cp.ClientID = sdr["ClientId"].ToString();
cp.ClientName = sdr["ClientName"].ToString();
cp.CLProductId = sdr["CLProductId"].ToString();
cp.ProductId = sdr["ProductId"].ToString();
cp.ProductName = sdr["ProductName"].ToString();
cp.ClientProductDetail = sdr["ClientProductDetail"].ToString();
cp.ParentProductId = sdr["ParentProductId"].ToString();
if (DBNull.Value != sdr["Act_Date"])
{
cp.Act_Date = sdr["Act_Date"].ToString();
}
else
{
cp.Act_Date = null;
}
if (DBNull.Value != sdr["Exp_Date"])
{
cp.Exp_Date = sdr["Exp_Date"].ToString();
}
else
{
cp.Exp_Date = null;
}
clproduct.Add(cp);
}
}
}
con.Close();
}
}
return clproduct;
}
//Controller
public ActionResult ClientProductList(string id)
{
return View(GetProducts(id).ToList().Find(e => e.ClientID == id));
}
 
 

Answers (3)