如何使用session在页面加载上编写查询 [英] how to write query on page load using session

查看:121
本文介绍了如何使用session在页面加载上编写查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载中编写查询,就像我们在会话变量中有john时那样,当smith来到他的数据显示时,与john相关的数据进入gridview ..

i不会怎么写查询页面加载..



i want to write query in page load like when we have john in session variable then data related to john comes into the gridview when smith come his data showss..
i dont no how to write query on page load..

protected void Page_Load(object sender, EventArgs e)
       {


           string username = (string)(Session["UserAuthentication"]);
               if(Session["UserAuthentication"]!=null)
               {
                   Label2.Text=username;

               }
           else
               {
                   Response.Redirect("Login.aspx");
               }
           if (!Page.IsPostBack)
           {
               data();
               tasks();

           }
       }

推荐答案

喜欢这个:





public void gridpop()

{

string constr1 = ConfigurationManager.ConnectionStrings [connstr]。ToString ();

SqlConnection con1 = new SqlConnection(constr1);

string sql1 =SELECT COUNT(emp)FROM leave WHERE emp =''+ Session [user_name ] .ToString()+'';

SqlCommand cmd1 =新的SqlCommand(sql1,con1);

con1.Open();

int temp1 = Convert.ToInt32(cmd1.ExecuteScalar()。ToString());

con1.Close();

if(temp1> 0)

{

string str = ConfigurationManager.ConnectionStrings [connstr]。ToString();

SqlConnection con = new SqlConnection(str);

string sql =SELECT * FROM leave where emp =''+ Session [ user_name]。ToString()+''由id desc命令;

SqlCommand cmd = new SqlCommand(sql,con);

con.Open() ;

SqlDataReader rdr = cmd.ExecuteReader();

ASPxGridView1.DataSource = rdr;

ASPxGridView1.DataBind();

}

其他

{

lblmsg.Visible = true;

lblmsg.Text =你没有申请任何叶子。;

ASPxGridView1.Visible = false;

}



} < br $>




protected void Page_Load(object sender,EventArgs e)

{

if(Session [emp]!= null)

{

lblmsg.Visible = false;

gridpop();

}

其他

{

Response.Redirect(login.aspx);

}

}
Like This:


public void gridpop()
{
string constr1 = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection con1 = new SqlConnection(constr1);
string sql1 = "SELECT COUNT (emp) FROM leave WHERE emp = ''" + Session["user_name"].ToString() + "'' ";
SqlCommand cmd1 = new SqlCommand(sql1, con1);
con1.Open();
int temp1 = Convert.ToInt32(cmd1.ExecuteScalar().ToString());
con1.Close();
if (temp1 > 0)
{
string str = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection con = new SqlConnection(str);
string sql = "SELECT * FROM leave where emp=''" + Session["user_name"].ToString() + "'' order by id desc";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
ASPxGridView1.DataSource = rdr;
ASPxGridView1.DataBind();
}
else
{
lblmsg.Visible = true;
lblmsg.Text = "You Havent Applied For Any Leaves.";
ASPxGridView1.Visible = false;
}

}


protected void Page_Load(object sender, EventArgs e)
{
if (Session["emp"] != null)
{
lblmsg.Visible = false;
gridpop();
}
else
{
Response.Redirect("login.aspx");
}
}


这篇关于如何使用session在页面加载上编写查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆