在下一页中获取会话值并填充到gridview中 [英] Fetching session values in next page and populating into gridview

查看:54
本文介绍了在下一页中获取会话值并填充到gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是asp.net的新手,正在从事一个项目..

在第一页的场景中,我有一个下拉菜单,两个文本框和一个按钮

Hi, i m pretty new to asp.net and working with a project..

In my scenario in the first page i have a dropdown, 2 textboxes n a button

protected void Button2_Click(object sender, EventArgs e)
   {
       Session["date"] = Request["TextBox1"];
       Session["duedate"] = Request["TextBox2"];
       Session["status"] = Request["DropDownList1"];
       Response.Redirect("Default6.aspx");

   }


上面的代码在第一页.然后在第二页中,我有一个gridview,其填充如下:-


the above code is in the first page. Then in the second page i have a gridview which m populating as follows:-

string conn = System.Configuration.ConfigurationManager.ConnectionStrings["CrystalConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(conn);
            string que = "select * from Table4 where ( (status='" + Session["status"] + "' and date='" + Session["date"] + "' and duedate='" + Session["duedate"] + "') or ((status='" + Session["status"] + "')   )  ";
            SqlDataAdapter sd = new SqlDataAdapter(que, connection);
            DataSet dsa = new DataSet();
            sd.Fill(dsa, "Table4");
            GridView1.DataSource = dsa;
            GridView1.DataBind();



在这里,查询的只有一部分可以正常运行,而另一部分则无法正常运行.请帮助我.

在此先感谢...



here only one part of the query runs fine not the other. please help me with this.

Thanks in advance...

推荐答案

可能您在查询末尾缺少了一个括号.尝试替换为以下文本

Probably you are missing one bracket at the end of the query. try replacing with the below text

string que = "select * from Table4 where ( (status='" + Session["status"] + "' and date='" + Session["date"] + "' and duedate='" + Session["duedate"] + "') or ((status='" + Session["status"] + "')   )  )";



希望对您有帮助!



Hope this helps !!!


由于您缺少(或有多余的)括号,因此查询应生成异常.

也不要将文字值连接到查询中.例如,尝试如果在TextBox1中输入"some"文本,会发生什么情况.使用 SqlParameter [
The query should generate an exception since you''re missing (or having an extra) parenthesis.

Also never concatenate literal values to your query. For example, try what happens if you input "some''text" to TextBox1. Use SqlParameter[^] with your queries. So the query could look like:
...
string que = @"
select * 
from Table4 
where (status=@status and [date]=@date and duedate=@duedate) or (status=@status)";
command.Parameters.AddWithValue("@status", Session["status"]);
command.Parameters.AddWithValue("@date ", Session["date"]);
command.Parameters.AddWithValue("@duedate", Session["duedate"]);
...


由于当前仅使用SQL字符串,因此需要
SqlCommand [^ ]正确分配参数.另请注意,如果您使用保留字,请用[]
括起来
而且看起来您表中的所有列都是字符串,但是您使用的是日期数据.始终尝试使用正确的数据类型(例如,日期列使用date datetime2).这样就消除了不必要的类型转换等.


Since you have currently used just a SQL string, you would need a SqlCommand[^] to properly assign parameters. Also note that if you use reserved words, enclose them with []

Also it looks like all your columns in the table are strings but you use date data. Always try to use proper data types (for example date or datetime2 for date columns). This eliminates unnecessary type conversions etc.




试试以下

Hi,

Try the following

string que = "select * from Table4 where status='"
                + Session["status"] + "' and date='" + Session["date"] + "' and duedate='" + Session["duedate"] + "' or status='" + Session["status"] + "'";




但是,请尝试使用存储过程而不是内联查询....




But try to use stored procedure instead of inline queries....


这篇关于在下一页中获取会话值并填充到gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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