Reputation: 1021
I am new to mvc and i want to build new system , i made login screen and i save the login informations in Session parameter then redirect to another window ,
when i use SELECT statement without compare session value its working without any error and this is the controller code:
public ActionResult Index()
{
string sql= @"SELECT PATIENT_NO , LAB_ORDER_NO , PATIENT_NAME_A , SERV_REQUEST_DATE_G ,
SERV_REQUEST_DOCTOR_NAME
FROM LAB_ORDERS_STS where PATIENT_NO=10 ";
DataTable dt = func.fireDatatable(string.Format(sql));
return View(dt);
}
but when i use the session and get data depending on session["MRN"] i got the error "Object reference not set to an instance of an object" and this is the code :
public ActionResult Index()
{
string sql= @"SELECT PATIENT_NO , LAB_ORDER_NO , PATIENT_NAME_A , SERV_REQUEST_DATE_G ,
SERV_REQUEST_DOCTOR_NAME
FROM LAB_ORDERS_STS where 1=1 ";
string condition = "";
string orderby = "LAB_ORDERS_STS.LAB_ORDER_NO desc";
condition += " and LAB_ORDERS_STS.PATIENT_NO ='" + Session["MRN"] + "'";
DataTable dt = func.fireDatatable(string.Format(sql+condition+orderby));
return View(dt);
}
This is also the login controller code :
[HttpPost]
public ActionResult Authorise(kaashtaif.Models.WEBSITE_USERS usermodel)
{
using (Entities db = new Entities())
{
var userdetails = db.WEBSITE_USERS.Where(x => x.ID_NO == usermodel.ID_NO && x.MOBILE == usermodel.MOBILE && x.PATIENT_NO == usermodel.PATIENT_NO).FirstOrDefault();
if (userdetails == null)
{
usermodel.LoginErrorMessage = "Entered Data Not Exist Please Update your Data - البيانات المدخلة غير صحيحة ارجو تحديث بيانات الملف الطبي";
return View("Login", usermodel);
}
else
{
Session["IDNO"] = usermodel.ID_NO;
Session["MOBILE"] = usermodel.MOBILE;
Session["MRN"] = usermodel.PATIENT_NO;
return RedirectToAction("Index", "Result");
}
}
}
I debug the code and session parameters save value and not null .
This is the Index.cshtml code :
@model System.Data.DataTable
@{
ViewBag.Title = "Index";
}
<h2>نتائج تحاليل المختبر </h2>
<table class="table">
<tr>
<th> PATIENT NO </th>
<th>
ORDER NO
</th>
<th>
PATIENT NAME
</th>
<th>
REQUEST DATE
</th>
<th>
DOCTOR NAME
</th>
</tr>
@foreach (System.Data.DataRow dr in Model.Rows)
{
<tr>
<td>@dr["PATIENT_NO"].ToString()</td>
<td>@dr["LAB_ORDER_NO"].ToString()</td>
<td>@dr["PATIENT_NAME_A"].ToString()</td>
<td>@dr["SERV_REQUEST_DATE_G"].ToString()</td>
<td>@dr["SERV_REQUEST_DOCTOR_NAME"].ToString()</td>
<td>@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) </td>
</tr>
}
</table>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
Finally the error appeared on this line foreach loop and when i use Session["MRN"] but when i select data direct without session parameter its working , what is the mistake ? Also i checked the posts in the site but no solution to my case.
@foreach (System.Data.DataRow dr in Model.Rows)
Upvotes: 1
Views: 261
Reputation: 4903
You miss the Order by
in the string orderby = "LAB_ORDERS_STS.LAB_ORDER_NO desc";
To solve the problem, change the orderby
to:
string orderby = " Order by LAB_ORDERS_STS.LAB_ORDER_NO desc";
Upvotes: 2