通过在where子句中传递session来绑定gridview [英] Bind a gridview by passing session in where clause

查看:131
本文介绍了通过在where子句中传递session来绑定gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会话[  que] = lb.Text; 
Response.Redirect( DisplayAnswer.aspx);







和DisplayAnswer.aspx档案...



< pre lang =xml> < asp:GridView ID = GridView1 runat = server OnDataBound = GridView1_DataBound >
< / asp:GridView >







和DisplayAnswer.aspx.cs文件......



 受保护  void  Page_Load( object  sender,EventArgs e)
{
< pre lang = cs> String conn = Data Source = localhost; Database = forum; User ID = root;密码= rachana;允许用户变量=真;

MySqlConnection con = new MySqlConnection(conn);
尝试
{
con.Open();
MySqlCommand md = new MySqlCommand( select回答,userid,来自threadid的日期,其中forumid =' + Session [ idq ] .ToString()+ ',con);
MySqlDataAdapter ada = new MySqlDataAdapter(md);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
< pre lang = cs > }
catch (例外情况)
{}

}





.............

这里我试图获得que的会话值lable并重定向到DisplayAnswer.aspx页面...在这个页面上我想只选择问题是Session [que]的数据,我已经在select命令中添加了where子句..它没有给我任何错误但是也不绑定gridview。它显示没有..如何使用where子句中的会话值绑定gridview ??

请解释...

thanx ...

解决方案

这意味着查询在数据库中成功执行但未返回任何行。您的会话值可能是空字符串。放置断点并检查会话值。试试这样:

  if (会话[  idq]!=  
{
con.Open();
MySqlCommand md = new MySqlCommand( select回答,userid,来自threadid的日期,其中forumid =' + Session [ idq ] .ToString()+ ',con);
MySqlDataAdapter ada = new MySqlDataAdapter(md);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
其他
{
// < span class =code-comment>您的会话为空。在此处显示消息。
}



在使用会话值之前,应始终检查其是否存在。在使用之前检查 Session [idq] 是否为空。



--Amit


嗨会员9586157,



另外,如果您的数据库中的''forumid''列是整数类型,那么单个查询中的引号

 forumid = ' + Session [idq]。不需要ToString()+' 





只需将其设置为

 forumid =   + Session [ idq  ]。ToString()+ 





谢谢,

Vamsi


Session["que"] = lb.Text;
            Response.Redirect("DisplayAnswer.aspx");




and in DisplayAnswer.aspx file...

<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound">
        </asp:GridView>




and DisplayAnswer.aspx.cs file...

protected void Page_Load(object sender, EventArgs e)
   {
<pre lang="cs">String conn = "Data Source=localhost;Database= forum;User ID=root;Password=rachana;Allow User Variables=True";

       MySqlConnection con = new MySqlConnection(conn);
       try
       {
           con.Open();
           MySqlCommand md = new MySqlCommand("select answer,userid,date from thread where forumid='"+Session["idq"].ToString()+"' ", con);
            MySqlDataAdapter ada = new MySqlDataAdapter(md);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ada.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
<pre lang="cs">}
        catch (Exception ex)
        { }

    }



.............
here i have tried to get Session value of que from lable and redirected to DisplayAnswer.aspx page...on this page i want to select only data where question is Session["que"] which i have added to where clause in select command..it is not giving me any error but also not binding the gridview. it displays nothing..how to bind gridview using session values in where clause??
please explain...
thanx...

解决方案

This means that query is executing successfully in database but not returning any rows. Your session value might be giving Empty String. Put a breakpoint and check for the session value. Try Like this:

if(Session["idq"]!= "")
{
    con.Open();
    MySqlCommand md = new MySqlCommand("select answer,userid,date from thread where forumid='"+Session["idq"].ToString()+"' ", con);
    MySqlDataAdapter ada = new MySqlDataAdapter(md);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    ada.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
else
{
    //Your session is empty. Show the message here.
}


Before using the session value you should always check for its existence. Check whether Session["idq"] is null or not before using it.

--Amit


Hi Member 9586157,

Also, if the column ''forumid'' in your DB is an integer type, then the single quotes in the query

forumid='"+Session["idq"].ToString()+"' 

is not required.

Just set it as

forumid="+Session["idq"].ToString()+" 



Thank you,
Vamsi


这篇关于通过在where子句中传递session来绑定gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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