Sourav Kumar Das

Sourav Kumar Das

  • 527
  • 2.5k
  • 171.5k

Get Maximum Value of maxid from database in MVC

Jan 22 2019 4:01 AM
How to get maximum value of 'max id' from database in Asp.Net MVC?
 
Sample Reference Code:
 
In Controller:
  1. public ActionResult getClientId()  
  2. {  
  3. ClientMaster objCliMas = new ClientMaster();  
  4. DataSet ds = new DataSet();  
  5. string sQuery = " SELECT ISNULL(MAX(ClientId), 0)+1 'MaxClientId' FROM PMS_ClientMaster ";  
  6. SqlDataAdapter da = new SqlDataAdapter(sQuery, con);  
  7. con.Open();  
  8. da.Fill(ds);  
  9. con.Close();  
  10. if (ds.Tables[0].Rows.Count > 0)  
  11. {  
  12. objCliMas.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["MaxClientId"].ToString());  
  13. }  
  14. return View(objCliMas);  
  15. }  
In View:
  1. @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })  
  2. @Html.ValidationMessageFor(model => model.Id, ""new { @class = "text-danger" })  
At here in the view it's not displaying the maxid value.
 
If there's any solution please let me know it.

Answers (4)