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?
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.
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);
}
}
}
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.
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.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
29 Points
127 Posts
session variable from a SQL query in _layout page
Sep 09, 2019 08:23 PM|Baze72|LINK
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
All-Star
53051 Points
23634 Posts
Re: session variable from a SQL query in _layout page
Sep 09, 2019 08:25 PM|mgebhard|LINK
If course. Please share your code if you need support querying a database and setting Session.
Member
29 Points
127 Posts
Re: session variable from a SQL query in _layout page
Sep 09, 2019 09:31 PM|Baze72|LINK
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!!
Contributor
3710 Points
1043 Posts
Re: session variable from a SQL query in _layout page
Sep 10, 2019 05:15 AM|Yongqing Yu|LINK
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.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
53051 Points
23634 Posts
Re: session variable from a SQL query in _layout page
Sep 10, 2019 10:15 AM|mgebhard|LINK
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.
Displaying Session in _layout using Razor is just as easy.
It is up to you to determine what SQL script you need to write to get the value(s) you want.
Member
29 Points
127 Posts
Re: session variable from a SQL query in _layout page
Sep 10, 2019 12:20 PM|Baze72|LINK
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.
Contributor
3710 Points
1043 Posts
Re: session variable from a SQL query in _layout page
Sep 11, 2019 01:15 AM|Yongqing Yu|LINK
Hi Baze,
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.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.