Answered by:
session variable from a SQL query in _layout page

Question
-
User982203039 posted
Is it possible to set a session variable from a sql query (select top1 support from table) and set it as a session variable to display on a layout page?
Thanks,
EB
Monday, September 9, 2019 8:23 PM
Answers
-
User665608656 posted
Hi Baze,
My sql query wil ALWAY only return 1 result that is why I thought I could use a session variable. I am using another session variable just fine just not setting with a sql query.If so, you can use session variable like this :
public ActionResult I() { List<IModel> icore = new List<IModel>(); string query = "SELECT ID, SupportCode FROM I"; string constr = ConfigurationManager.ConnectionStrings["PBTConn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { icore.Add(new IModel { SupportCode = Convert.ToInt32(sdr["SupportCode"]), }); Session["SupportCode"] = sdr["SupportCode"]; } } con.Close(); return View(icore); } } }
In you _layout view, you can use this session variable as provided in previous replies:
<p>SupportCode is @Session["SupportCode"].ToString() </p>
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 11, 2019 1:15 AM
All replies
-
User475983607 posted
Is it possible to set a session variable from a sql query (select top1 support from table) and set it as a session variable to display on a layout page?
If course. Please share your code if you need support querying a database and setting Session.
Monday, September 9, 2019 8:25 PM -
User982203039 posted
Well I am not sure really how to even start - but here is what I am maybe starting with. In my controller: I want the SupportCode to be a session variable that I can display on the _layout view. Let me know if you need anything else!!
public ActionResult I() { List<IModel> icore = new List<IModel>(); string query = "SELECT ID, SupportCode FROM I"; string constr = ConfigurationManager.ConnectionStrings["PBTConn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { icore.Add(new IModel { SupportCode = Convert.ToInt32(sdr["SupportCode"]), }); } } con.Close(); return View(icore); } } }
Monday, September 9, 2019 9:31 PM -
User665608656 posted
Hi Baze,
According to your description, I found that there will be many SupportCodes in your code.
I want to ask you, do you want to store these SupportCodes in the same session? Or does it show only one SupportCode to the _layout page?
If you want to display a session variable in _layout view, here has a simple way to achieve what you want , you can refer to this link :
https://stackoverflow.com/a/37281092
Best Regards,
YongQing.
Tuesday, September 10, 2019 5:15 AM -
User475983607 posted
The SQL returns every record from table I. What are you trying to persist in Session? Can you explain your design?
Anyway, adding a value to Session is very simple.
Session["MySessionItem"] = "This is a Test";
Displaying Session in _layout using Razor is just as easy.
@if(Session["MySessionItem"] != null) { <div>@Session["MySessionItem"]</div> }
It is up to you to determine what SQL script you need to write to get the value(s) you want.
Tuesday, September 10, 2019 10:15 AM -
User982203039 posted
My sql query wil ALWAY only return 1 result that is why I thought I could use a session variable. I am using another session variable just fine just not setting with a sql query.
Tuesday, September 10, 2019 12:20 PM -
User665608656 posted
Hi Baze,
My sql query wil ALWAY only return 1 result that is why I thought I could use a session variable. I am using another session variable just fine just not setting with a sql query.If so, you can use session variable like this :
public ActionResult I() { List<IModel> icore = new List<IModel>(); string query = "SELECT ID, SupportCode FROM I"; string constr = ConfigurationManager.ConnectionStrings["PBTConn"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { icore.Add(new IModel { SupportCode = Convert.ToInt32(sdr["SupportCode"]), }); Session["SupportCode"] = sdr["SupportCode"]; } } con.Close(); return View(icore); } } }
In you _layout view, you can use this session variable as provided in previous replies:
<p>SupportCode is @Session["SupportCode"].ToString() </p>
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 11, 2019 1:15 AM