问题:编了一个aspx页面,目的是按学号或名字查询学生资料  界面里有个DropDownList1用来选择是按名字查还是按学号查;有个textbox1  用来选定查询方式后给查询者输入学号或名字   最后按button1在下面的DataGrid1里显示table_inf表里此学号学生的所有信息 前面有声明 string str_tb=textbox1.text.tostring();
按昨天各位高手的指点,那么查询语句应该是  
1>当选按学号查时
string str_command="select * from table_inf where study_no="str_tb;
2>当选按名字查时
string str_command="select * from table_inf where name="str_tb;试了一下,按学号查没有问题(已经给分,谢谢!)但是刚才试了下用名字查,报错比如这里我在textbox1里填"王刚"   查询的时候   就会报错
 ---------列"王刚"不存在 不解! 请求帮助! 
另: 此页面上做的DropDownList1  希望里面有两个下拉项 "按学号查询"和"按名字查询"我是在page_load 里面加的两句代码
DropDownList1.Items.Add("按学号查询");
DropDownList1.Items.Add("按名字查询");这样运行进入页面后,下拉菜单有两项"按学号查询"和"按名字查询" ,很正常!但是每查询完一次,DropDownList下拉菜单下都会又多两项"按学号查询"和"按名字查询"  这样查询个几次,DropDownList1 就变成很长的一条选项了~~怎么会这样??晕死了昨天是第一次问问题,分数设置不多,回答的兄弟好几个,所以每位给的很少~今天分数加一倍,以感谢热心的朋友们!

解决方案 »

  1.   

    string str_command="select * from table_inf where name='"str_tb+"'";
      

  2.   

    上面笔误,是1>当选按学号查时
    string str_command="select * from table_inf where study_no="+str_tb;
    2>当选按名字查时
    string str_command="select * from table_inf where name="+str_tb;不好意思!
      

  3.   

    楼上的兄弟是写的 str_command="select * from table_inf where name='"str_tb+"'";里
    name='"str_tb+"'"; 的解释是?
      

  4.   

    DropDownList1.Items.Add("按学号查询");
    DropDownList1.Items.Add("按名字查询");这样运行进入页面后,下拉菜单有两项"按学号查询"和"按名字查询" ,很正常!但是每查询完一次,DropDownList下拉菜单下都会又多两项"按学号查询"和"按名字查询"  这样查询个几次,DropDownList1 就变成很长的一条选项了~~怎么会这样??晕死了
    -----------------------------------------------
    DropDownList1.Items.Clear();
    DropDownList1.Items.Add("按学号查询");
    DropDownList1.Items.Add("按名字查询");
      

  5.   

    现在在外面  下午或者晚上可以帖全代码  不好意思啊另外有个过分的请求  我是初学者,希望回答的兄弟除了写出代码以外,能稍微文字性地帮我分析下原因.谢谢!BTW: 另个非技术问题
    1.这里不能编辑自己的帖子?
    2. 分数我想给多少给多少?我自己怎么看自己有多少分可以给?
    很弱,见笑了~~~
      

  6.   

    刚回家,在外面跑了一天,手头也没代码随便写一个最简单的页面吧 问题是一样的页面上ONLY 3 个控件:DropDownList1;
    Label1;
    Button1;代码为:
    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;namespace WebApplication1
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button Button1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");


    // 在此处放置用户代码以初始化页面
    } #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.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
      
    Label1.Text +="ha";

    }
    }
    }结果是每按一次button1 , DropDownList1 里的下拉菜单数目都会加两个~`晕我用了以下两种方法,都没用1.  private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.CleanSelection();
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
    }2.    bool flag=true;private void Page_Load(object sender, System.EventArgs e)
    {
    if (flag==true)
                           { 
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
                                flag=false;
                           }
    }
    wenhuiyan 站友告诉我的
    在Page_Load事件加
    if(!IsPostBack)
        this.DataBind();
    的办法也试过了~还是解决不了困惑中....求解!!!!再谢!!
      

  7.   

    1>当选按学号查时
    string str_command="select * from table_inf where study_no="+str_tb;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    string str_command="select * from table_inf where study_no='"+str_tb + "'";
    2>当选按名字查时
    string str_command="select * from table_inf where name="+str_tb;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    string str_command="select * from table_inf where name='" + str_tb + "'";
    原因很简单,就是你的学号或姓名没有加上单引号,这样,数据库会把它当作是列名
    比如这里我在textbox1里填"王刚"   查询的时候   就会报错
     ---------列"王刚"不存在 
    这个提示信息不是已经很明显了吗?
      

  8.   

    第一个问题我搞明白了,谢谢大家.现在是第二个问题没法解决,DropDownList1还是页面每刷新一次就加两行选项,请各位高手帮忙!谢谢!随便写一个最简单的页面吧 用来说明问题
    页面上就 3 个控件:1.DropDownList1;
    2.Label1;
    3.Button1;代码为:
    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;namespace WebApplication1
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button Button1;private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
    // 在此处放置用户代码以初始化页面
    }#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.Load += new System.EventHandler(this.Page_Load);}
    #endregionprivate void Button1_Click(object sender, System.EventArgs e)
    {
      
    Label1.Text +="ha";}
    }
    }结果是每按一次button1 , DropDownList1 里的下拉菜单数目都会加两个~`晕我用了以下两种方法,都没用1.  private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.CleanSelection();
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
    }2.    bool flag=true;private void Page_Load(object sender, System.EventArgs e)
    {
    if (flag==true)
                           { 
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
                                flag=false;
                           }
    }
    wenhuiyan 站友告诉我的
    在Page_Load事件加
    if(!IsPostBack)
        this.DataBind();
    的办法也试过了~还是解决不了困惑中....求解!!!!再谢!!
      

  9.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
    // 在此处放置用户代码以初始化页面
    }改成
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
    }
    // 在此处放置用户代码以初始化页面
    }
      

  10.   

    问题解决Reeezak(坚持信念) 兄    谢谢你的解答!!!!放分
      

  11.   

    我用了以下两种方法,为什么都没用呢?1.  private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.CleanSelection();
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
    }2.    bool flag=true;private void Page_Load(object sender, System.EventArgs e)
    {
    if (flag==true)
                           { 
    DropDownList1.Items.Add ("按学号号查询");
    DropDownList1.Items.Add ("按姓名查询");
                                flag=false;
                           }
    }
      

  12.   

    你的
    bool flag=true;
    会在每次页面刷新的时候编程true因为asp.net的运行机制跟winform不一样请使用Page.IsPostBack
      

  13.   

    最后问还一下 Reeezak兄弟说的:
    "原因很简单,就是你的学号或姓名没有加上单引号,这样,数据库会把它当作是列名"
    那为什么1>当选按学号查时,我用
    string str_command="select * from table_inf where study_no="+str_tb;
    查询时没出现这个问题呢?
    2>当选按名字查时
    string str_command="select * from table_inf where name="+str_tb; 就会把"王刚"当列名?
    在我建的inf表里面  study_no 和 name都是varchar啊