我现在是做一个高级查询功能的,里面有个查找是说当余额小于某个值的时候,查找出来的数据如果在Balance.Text 没有输入任何东西的时候,会报错,即当Balance.Text 为空的时候,无法取得数值啊,这个应该要怎么处理?要对  float balance = (float)Convert.ToSingle(Balance.Text); 加以判断吗?怎么添加判断?各位帮帮忙啊,谢谢了
 protected void searchbtn_Click(object sender, EventArgs e)
    {        string rank = ddlUser.SelectedValue;
        string usertype = ddlUsertype.SelectedValue;
        string paytype = ddlPaytype.SelectedValue;
        string username = userName.Text;
        string companyname = companyName.Text; 
                   
        float balance = (float)Convert.ToSingle(Balance.Text);         string strSql = @"SELECT   *   FROM   Userinfo   WHERE   1=1   ";
        if (rank != "--请选择--")
            strSql += @" and   Rank   like   '%" + rank + "%'   ";
        if (usertype != "--请选择--")
           strSql += @" and   Usertype   like   '%" + usertype + "%'   ";
        if (paytype != "--请选择--")
           strSql += @" and   Paytype   like   '%" + paytype + "%'   ";
        if(companyname !="")
            strSql += @" and   Companyname   like   '%" + companyname + "%'   ";
        if(username !="")
            strSql += @" and   Username   like   '%" + username  + "%'   ";
        if (Balance.Text != "")
         strSql += @" and Balance < " + balance + ""; 
                gvUser.DataSource = DBUser.getDs(strSql, "Userinfo");
        gvUser.DataBind();
        DBUser.clear();    }

解决方案 »

  1.   

     protected void searchbtn_Click(object sender, EventArgs e)
        {        string rank = ddlUser.SelectedValue;
            string usertype = ddlUsertype.SelectedValue;
            string paytype = ddlPaytype.SelectedValue;
            string username = userName.Text;
            string companyname = companyName.Text; 
           if(Balance.Text!=null||Balance.Text!="")
           {
            float balance = (float)Convert.ToSingle(Balance.Text); 
            
            string strSql = @"SELECT   *   FROM   Userinfo   WHERE   1=1   ";
            if (rank != "--请选择--")
                strSql += @" and   Rank   like   '%" + rank + "%'   ";
            if (usertype != "--请选择--")
               strSql += @" and   Usertype   like   '%" + usertype + "%'   ";
            if (paytype != "--请选择--")
               strSql += @" and   Paytype   like   '%" + paytype + "%'   ";
            if(companyname !="")
                strSql += @" and   Companyname   like   '%" + companyname + "%'   ";
            if(username !="")
                strSql += @" and   Username   like   '%" + username  + "%'   ";
            if (Balance.Text != "")
             strSql += @" and Balance < " + balance + ""; 
                    gvUser.DataSource = DBUser.getDs(strSql, "Userinfo");
            gvUser.DataBind();
            DBUser.clear();
           }
           else
           {
               Response.Write("请输入值");
           }    }
      

  2.   

    这还不如容易?
    float balance;
     if (Balance.Text != "" && float.TryParse(Balance.Text,out balance)==true)
    {
         strSql += @" and Balance < " + balance + ""; 
    }