如果发生异常,想执行catch{}中的代码,但是不灵,请问我应该如何做,或是结构本身就有问题?那应该怎样去构思?谢谢!
程序代码如下(变红的代码就是程序没有执行的代码):
public partial class ShowResult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["order"] = "ASC";
            ViewState["SortItem"] = "contactName";
            this.bind();
            Label1.Visible = false;
        }        btnClose.Attributes.Add("onclick", "window.close()");
        if (GridView1.Rows.Count <= 0)
        {
            Label1.Text = "对不起,没有查到相关数据!";
            Label1.Visible = true;
        }
    }
    private void bind()
    {
        try
        {
            string selectedRBL = Request.QueryString["RBL"].ToString();
            string Text = Request.QueryString["Text"].ToString();            SqlConnection con = DB.CreateCon();            string sql = "select * from customers where " + selectedRBL + " like   '%" + Text + "%'";
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "Result");
            DataView dv = ds.Tables["Result"].DefaultView;
            dv.Sort = ViewState["SortItem"].ToString() + " " + ViewState["order"].ToString();
            GridView1.DataKeyNames = new string[] { "pid" };
            GridView1.DataSource = dv;
            GridView1.DataBind();
        }        catch
        {
            Label1.Visible = false;            Response.Write("程序出错或非法登入!");
            
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='MistyRose'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.bind();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if(ViewState["order"].ToString()=="ASC")
        {
            ViewState["order"]="DESC";
            this.bind();
        }
        else
        {
            ViewState["order"]="ASC";
            this.bind();
        }
    }
}

解决方案 »

  1.   

    你确定你try里的代码一定异常吗
      

  2.   

    是,如果直接打开此页就会发生异常,因为我传递了两个值过来.
    string selectedRBL = Request.QueryString["RBL"].ToString(); 
    string Text = Request.QueryString["Text"].ToString(); 
      

  3.   

    报错也许不是try里的错 请检查仔细
      

  4.   

    用if判断么
    if(Request.QueryString["RBL"]==null||Request.QueryString["Text"]==null)
      

  5.   

    可能我刚才没说清楚.这是一个显示查询结果的页面,查询内容在上一页输入,直接带值跳转到这页.把查询结果用这页的GridView显示出来。
    这段代码正常使用情况下不会发生异常,但一旦有人直接在IE里输入这页地址打开,就会发生异常.
    如果直接打开这页,肯定抛出异常,因为传不过来数据。
    string selectedRBL = Request.QueryString["RBL"].ToString();   
    string Text = Request.QueryString["Text"].ToString();   我想要的效果是:这页已经放置了一个Label标签,用于在没有查询到结果时显示“对不起,没有查到相关数据”。当直接输入这页地址发生异常时,改变Label标签的显示内容。但改变不了.response.write()却能执行。所以请求帮助!!
    catch 

      Label1.Visible   =   false;
      Response.Write("程序出错或非法登入!"); 
                            

      

  6.   

    Response.Write("程序出错或非法登入!");   去掉这个语句
    或者直接改成Response.Write("<script language=javascript>alert('程序出错或非法登入');</script>");
      

  7.   

    对你的代码提出几点改进
    1 精简下代码,你自己看看你的label.visible设置了几次?
    2 bind方法try的范围太大了!首先可能是取不到QueryString,还有可能是connection建立错误,其次还有可能是sql语句组合错误(有sql注入的危险)。
    3 代码的异常处理不够(比如某些变量有可能为null)
      

  8.   

    代码的逻辑顺序有问题啊
        if   (GridView1.Rows.Count   <=   0) 
                    { 
                            Label1.Text   =   "对不起,没有查到相关数据!"; 
                            Label1.Visible   =   true; 
                    } 和下面
       catch 
                    { 
                            Label1.Visible   =   false;                         Response.Write("程序出错或非法登入!"); 
                            
                    } 冲突
    那个红色并不是没执行,只是因为bind出错GridView1.Rows.Count <0也成立所以 Label1.Visible   =   true;也同时执行了
    实际上bind前就应该执行 Request.QueryString["RBL"]==null 的判断,或给初值或直接给非法提示实际上这个是一个编程习惯问题
      

  9.   

    实际上一般性的防御代码是下面这样的    public string Name
            {
                get
                {
                    object name = Request.QueryString["Name"]
                    if (name != null)
                        return (string)name;
                    else
                        return string.Empty;
                }
                      
            }