代码如下
 protected void Button2_Click(object sender, EventArgs e)
    {
        string connstring = "Data Source=.\\sqlexpress;Initial Catalog=library;Integrated Security=True";        SqlConnection conn = new SqlConnection(connstring);
        conn.Open();     
        string sel1, sel2;
        sel1 = "select bookname from books where bookid =  '" + TextBox3.Text + "'";
        sel2 = "select quantity from books where bookid =  '" + TextBox3.Text + "'";
        SqlCommand cmd1 = new SqlCommand(sel1, conn);
        SqlDataReader libDa1 = cmd1.ExecuteReader();
        if (libDa1.Read())
        {
 
           Response.Write("成功!");        }        else
        {
            Response.Write("<script language=javascript>alert (不存在此书目,返回!');</script>");
        
        }        conn.Close();       }       
****************************************************************************************************
if语句还嵌套别的语句,为了调试,我把它改成 Response.Write("成功!");可是每次都都不执行else

解决方案 »

  1.   


    能查出数据来。。它就不会进else
      

  2.   

    我当然是用数据库里没有的数据去试了,可是 还是不执行else
      

  3.   

    我当然是用数据库里没有的数据去试了, 就是不执行else····
      

  4.   

    觉得挺简单的if else可是不执行啊 是VS2008的问题吗 ?
      

  5.   

    肯定是满足了IF的条件的。你在仔细看下,跟VS没关系的
      

  6.   

    换言之你把"select quantity from books where bookid = '" + TextBox3.Text + "'";
    用值带进去看看在SQL数据库里能不能查出数据就知道了
      

  7.   


    试试,看执行else不!!if (false)
      {
      
      Response.Write("成功!");  }  else
      {
      Response.Write("<script language=javascript>alert (不存在此书目,返回!');</script>");
        
      }
      

  8.   

    我的数据库很简单的 我输的就是不存在的bookid我的TextBox2也是用的这样的if判断语句就能执行
    偏偏button2不行,怎么回事啊 ——!
    ************************************************************
    TextBox2代码
     protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
             SqlConnection libCon = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=library;Integrated Security=True");
            libCon.Open();
            string BorSQL;
            BorSQL = "select rName from Reader where readerid = '" + TextBox2.Text + "';";
            SqlCommand StuIns = new SqlCommand(BorSQL, libCon);
            SqlDataReader libDa = StuIns.ExecuteReader();
            //libDa.Read();
            if (libDa.Read())
            {
                Label3.Text = "读者姓名:" + libDa.GetString(0);
                Label5.Text = "读者借图情况";
            }        else
            {
                Label3.Text = "不存在该读者";
                Label5.Text = "";
            }
            libDa.Close();
            libCon.Close();
        }
      

  9.   

    代码很简单,问题肯定出在检索到了数据。
    messagebox.show(libDa1.Read().toString())
      

  10.   

    Response.Write("<script language=javascript>alert ('不存在此书目,返回!');</script>");
    少个',调试下看看,我觉得应该是执行了,但是页面你没看到执行结果,因为js错误
      

  11.   

    楼主用断点调试的话,应该就能发现是ELSE里面有问题了
      

  12.   


    呵呵,没仔细看。 
    不过LZ一直说的是Else没被执行,因此也就没去看else中的内容。
    但很显然,else是被执行了的。