Reputation: 99
I am trying to show the session value by drop down list. This is what the code i made, but its not work well. correct me
//session created
public void SAN()
{
cn.Open();
string sq = "select Sitealiasname from tbl_Sitemaster where sitename in (select sitename from tbl_emploeedetails where employeestatus='L' and employeeid='" + Session["EMPID"].ToString() + "') and status='A' order by Sitealiasname asc";
SqlCommand d = new SqlCommand(sq, cn);
SqlDataReader r;
r = d.ExecuteReader();
while (r.Read())
{
Label4.Text = r["Sitealiasname"].ToString().Trim();
Session["Sitealiasname"] = Label4.Text;
}
cn.Close();
}
//call the session to dropdownlist
protected void Page_Load(object sender, EventArgs e)
{
ddlsite.Text = Session["Sitealiasname"].ToString();
}
Upvotes: 0
Views: 2943
Reputation: 1448
try this
// Add this line ------ Edited-------------
using System.Linq;
public void SAN()
{
cn.Open();
string sq = "select Sitealiasname from tbl_Sitemaster where sitename in (select `sitename from tbl_emploeedetails where employeestatus='L' and employeeid='" + Session["EMPID"].ToString() + "') and status='A' order by Sitealiasname asc";`
SqlCommand d = new SqlCommand(sq, cn);
SqlDataReader r;
r = d.ExecuteReader();
List<string> sitnames = new List<string>();
while (r.Read())
{
sitename.Add(r["Sitealiasname"].ToString().Trim());
}
cn.Close();
Session["Sitealiasname"] = sitename
}
protected void Page_Load(object sender, EventArgs e)
{
///Bind data here
if(Session["Sitealiasname"] != null){
ddlsite.DataSource = Session["Sitealiasname"].ToList();
ddlsite.DataBind();
}
}
Upvotes: 0
Reputation: 3295
Check this code if i understand exactly what you need then this will work for you.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SAN();
bind();
}
}
private void bind()
{
ArrayList ar = new ArrayList();
ar.Add("first");
ar.Add("Second");
ar.Add("Third");
ar.Add("Four");
ar.Add("Five");
ar.Add("Six");
ar.Add("Seven");
DropDownList1.DataSource = ar;
DropDownList1.DataBind();
DropDownList1.SelectedValue = Session["Sitealiasname"].ToString();
}
public void SAN()
{
cn.Open();
string sq = "select Sitealiasname from tbl_Sitemaster where sitename in (select sitename from tbl_emploeedetails where employeestatus='L' and employeeid='" + Session["EMPID"].ToString() + "') and status='A' order by Sitealiasname asc";
SqlCommand d = new SqlCommand(sq, cn);
SqlDataReader r;
r = d.ExecuteReader();
while (r.Read())
{
Label4.Text = r["Sitealiasname"].ToString().Trim();
Session["Sitealiasname"] = Label4.Text;//Suppose here session value "first"
}
cn.Close();
}
let me know if not what you want.
Upvotes: 0
Reputation: 1785
You can try
For SAN Function Use
List<String> sitenames=null;
using (DataReader r = d.ExecuteReader())
{
sitenames = r.AutoMap<string>().ToList();
}
Session["Sitealiasname"] = sitenames;
For Binding to Dropdownlist you can use
ddlsite.DataSource = Session["Sitealiasname"];
ddlsite.DataBind();
Upvotes: 1
Reputation: 698
Dropdownlist does not have text property which you have used here. You have to first populate the dropdown and then select the value which you want to display.
First you should add item in the dropdown by
items.add()
List of property suppoted by dropdown link
Upvotes: 0
Reputation: 7
trying this code in Pageload for Bind Session Value into Dropdowlist
ddlsite.Items.Add(Session["Sitealiasname"].Tostring());
Upvotes: 0