搞了好久,没办法了,求各位高手相助!
用C#做的web网站,VS2005,Sql Server2005,数据库都已经连好了。
要实现在文本框中输入一个文本,然后点击按钮,读取数据库CerID字段(在数据库中该字段为Varchar类型)为该文本的所有记录,如果读取成功,就转到ShowAll.aspx页面,否则弹出对话框“未找到相关信息”。代码为:          …………
          …………
         SqlConnection MyConnection = DB.creatConnection();
        MyConnection.Open();
        SqlCommand MyCommand = new SqlCommand("select * from Cer_Table where CerID='"+CerIDTxt.Text+"'", MyConnection);        SqlDataReader dr = MyCommand.ExecuteReader();
       
        if (dr.Read())
             {//在数据库中找到了相应数据
                  Response.Redirect("ShowAll.aspx");
                dr.Close();
                 MyConnection.Close();
              }
         else
            { 
            Response.Write("<Script Language=JavaScript>alert('未找到相关信息!');</Script>");
            }
在数据库CerID字段中有一个值为“222222”,但是在文本框中输入“222222”后,提示“未找到相关信息!”
但是把SqlCommand命令改为
SqlCommand MyCommand = new SqlCommand("select * from Cer_Table where StaID='222222'", MyConnection);时却能转到ShowAll.aspx页面。
怎么回事啊?哪的问题哦?

解决方案 »

  1.   

    trim一下,然后转换成字符转类型,试试
      

  2.   

    SqlCommand MyCommand = new SqlCommand("select * from Cer_Table where CerID='"+CerIDTxt.Text.Trim()+"'", MyConnection); 
      

  3.   

    SqlCommand MyCommand = new SqlCommand("select * from Cer_Table where CerID='"+CerIDTxt.Text.ToString()+"'", MyConnection); 
    试试
      

  4.   

    这样解决不了呀!改成CerIDTxt.Text.ToString()也不行……
      

  5.   

     这样就不会了
    SqlCommand MyCommand = new SqlCommand("select * from Cer_Table where CerID='"+CerIDTxt.Text.Trim()+"'", MyConnection); 
      

  6.   

    调试一下,看CerIDTxt.Text返回的值,或且输出MyCommand.CommandText的值是不是select * from Cer_Table where CerID="22222"。然后在数据库那边运行一下,看一下效果。。调试很重要
      

  7.   

    谢谢!我好像不太会调试,不过现在问题解决了。我的该网页中pageload事件里写的语句将文本框的内容设为空值了,我把那个语句去掉,问题就解决了。但是不明白为什么,难道按钮的click事件会调用pageload吗?(我的按钮click事件只是查询数据库CerID字段是不是有文本框内容一样的数据,如果有就转到另一页面)问题解决了,又出来一个问题……
      

  8.   

     任何服务器事件其实都刷新了页面,刷新时无条件执行page_load,如果你想不执行可以写在
    if(!Page.IsPostBack)
    {
       //语句块
    }