主题:  .net中这样为什么不行?(见代码)

goodlook

职务:普通成员
等级:1
金币:0.0
发贴:20
#12004/10/11 11:35:27
我想实现在第一个页面里选择查询条件,然后通过第二个页面的DataGrid返回符合条件的数据(使用Northwind数据库中SQL中的Order Details表)。但不成功 :(不知为什么,但我怀疑是参数没有传递到DataGrid中去,能否帮忙看一下代码是哪有问题?谢谢
代码如下:
(查询页的)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Examp05.Ex05_01
{
    /// <summary>
    /// Ex05_11 的摘要说明。
    /// </summary>
    public class Ex05_11 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.DropDownList dl1;
        protected System.Web.UI.WebControls.DropDownList dl2;
        protected System.Web.UI.WebControls.TextBox tx;
        protected System.Web.UI.WebControls.RadioButton RadioButton1;
        protected System.Web.UI.WebControls.RadioButton RadioButton2;
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.WebControls.ListBox ListBox1;
        protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
        protected System.Web.UI.WebControls.Button Button3;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!IsPostBack)
            {
                try
                {
                    DataSet ds=new DataSet();
                    string str="server=localhost;uid=sa;pwd=;database=Northwind";
                    SqlConnection conn=new SqlConnection(str);
                    string command="select name from sysColumns where id=325576198 order by colid";
                    SqlDataAdapter da=new SqlDataAdapter(command,conn);
                    da.Fill(ds,"ta");
                    CheckBoxList1.DataSource=ds.Tables["ta"].DefaultView;
                    CheckBoxList1.DatavalueField=ds.Tables["ta"].Columns[0].ColumnName;
                    CheckBoxList1.DataTextField=ds.Tables["ta"].Columns[0].ColumnName;
                    CheckBoxList1.DataBind();
                    CheckBoxList1.RepeatDirection=RepeatDirection.Horizontal;
                    dl1.DataSource=ds.Tables["ta"].DefaultView;
                    dl1.DatavalueField=ds.Tables["ta"].Columns[0].ColumnName;
                    dl1.DataTextField=ds.Tables["ta"].Columns[0].ColumnName;
                    dl1.DataBind();
                }
                catch
                {
                    Response.Write("<script language=javascript>alert('连接失败!')</script>");
                }

            }
                    
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.Button3.Click += new System.EventHandler(this.Button3_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            string qu;
            string qu1="";
            string qu2;
            if(RadioButton1.Checked==true)
            {
                qu1=""+RadioButton1.Text.ToString()+"";
            }
            if(RadioButton2.Checked==true)
            {
                qu1=""+RadioButton1.Text.ToString()+"";
            }
            qu2=dl1.SelectedItem.Text.ToString()+dl2.SelectedItem.Text.ToString()+tx.Text.ToString();
            if(ListBox1.Items.Count==0)
            {
                qu=qu2;
            }
            else
            {
                qu=qu1+qu2;
            }
            ListBox1.Items.Add(qu);
        }

        private void Button2_Click(object sender, System.EventArgs e)
        {
            if(ListBox1.Items.Count!=0)
            {
                if(ListBox1.SelectedIndex>-1)
                {
                    ListBox1.Items.Remove(ListBox1.SelectedItem);
                }
                else
                {
                    Response.Write("<script language=javascript>alert('没有选中列表项!')</script>");
                }
            }
        }

        private void Button3_Click(object sender, System.EventArgs e)
        {
            string tq="";
            string shc="";
            if(ListBox1.Items.Count==0)
            {
                Response.Write("<script language=javascript>alert<'请选择查询条件!'></script>");
            }
            for(int i=0;i<ListBox1.Items.Count;i++)
            {
                if(ListBox1.Items[0].Text.StartsWith("and"))
                {
                    ListBox1.Items[0].Text=ListBox1.Items[0].Text.Substring(5);
                }
                if(ListBox1.Items[0].Text.StartsWith("or"))
                {
                    ListBox1.Items[0].Text=ListBox1.Items[0].Text.Substring(4);
                }
                tq+=ListBox1.Items[i].Text.ToString();
            }
            if(CheckBoxList1.SelectedIndex>-1)
            {
                for(int j=0;j<CheckBoxList1.Items.Count;j++)
                {
                    if(CheckBoxList1.Items[j].Selected==true)
                    {
                        shc+=CheckBoxList1.Items[j].Text.ToString()+",";
                    }
                }
            }

            else
            {
                Response.Write("<script language=javascript>alert('请选择要显示的字段名称!')</script>");
            }
            shc=" "+shc.TrimEnd(',')+" ";
            Response.Redirect("Ex05_12.aspx?shc="+shc+"&tq="+tq);
        }
        
        

        }

        
    
}







(DataGrid页的)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Examp05.Ex05_01
{
    /// <summary>
    /// Ex05_12 的摘要说明。
    /// </summary>
    public class Ex05_12 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            string showCols=Request["shc"];
            string tq=Request["tq"];
            Label1.Text="查询结果"+tq;
            DataSet ds=new DataSet();
            SqlConnection conn=new SqlConnection("server=localhost;uid=sa;pwd=;database=Northwind");
            string command="select"+showCols+"from [Order Details] where" +tq;
            SqlDataAdapter da=new SqlDataAdapter(command,conn);
            da.Fill(ds,"ta1");
            if(ds.Tables["ta1"].Rows.Count!=0)
            {
                DataGrid1.DataSource=ds.Tables["ta1"].DefaultView;
                DataGrid1.DataBind();
            }
            else
            {
                Label1.Text="没有符合查询条件的数据!";
            }

        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

编辑历史:[此帖最近一次被 allinhands 编辑过(编辑时间:2004-10-12 07:37:31)]


蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#22004/10/11 16:30:43
是否传递数值不正确?SQL语句呢?

编辑历史:[此帖最近一次被 蓝鲸 编辑过(编辑时间:2004-10-11 17:07:42)]

非常大鱼

goodlook

职务:普通成员
等级:1
金币:0.0
发贴:20
#32004/10/11 17:22:34
SqlConnection conn=new SqlConnection("server=localhost;uid=sa;pwd=;database=Northwind";
string command="select"+showCols+"from [Order Details] where" +tq;
SqlDataAdapter da=new SqlDataAdapter(command,conn);
第二段代码里面有呀,不知这样对么?
还有第一段最后的这段
shc=" "+shc.TrimEnd(',')+" ";
Response.Redirect("Ex05_12.aspx?shc="+shc+"&tq="+tq);
不知这样写对么?这样就能从第一个页面把参数传到第二页么?我的错误提示说where附近有错误,我找不到,是不是传递过程中有什么参数错了?请指点!谢谢!



蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#42004/10/11 20:15:56
不能回答你是否是对,因为没见到你的页面文件。
你可以这样检查一下,用retrun先终止一下以后可能会出错部分,先核对一下传过来的两个值,可以用Response.Write写出两个参数,然后再输出一下将要执行的SQL语句。有些微小的不容易察觉,如少一个标点、空格等,输出后容易检查。

Oh,发觉你一个问题,少了Conn.Open();


非常大鱼

goodlook

职务:普通成员
等级:1
金币:0.0
发贴:20
#52004/10/11 23:34:40
我先试试,不行再说,谢谢!