C# 如何在标签中设置捕获异常?

C# 如何在标签中设置捕获异常?,c#,exception,exception-handling,C#,Exception,Exception Handling,我的班级 public string Countryadd(string country, string id) { string data = "0"; try { string qry1 = "select Country from Country where Country='" + country + "'";//Checking weather txtcountry(Country Nam

我的班级

 public string Countryadd(string country, string id)
     {

         string data = "0";
         try
         {


             string qry1 = "select Country from Country where Country='" + country + "'";//Checking weather txtcountry(Country Name) value is already exixst or not. If exist return 1 and not exists go to else condition
             SqlDataReader dr = conn.query(qry1);
             if (dr.Read())
             {
                 return data = "1";
             }
             else 
             {

                 string qry = "insert into Country values('" + id + "','" + country + "')";
                 conn.nonquery(qry);
                 return data = "3";

             }

         }

         catch (Exception ex)
         {
             string x = ex.Message();
         }

         return data;
     }
这个字符串值如何在标签中设置

我的按钮点击功能是

protected void Button1_Click(object sender, EventArgs e)
    {

        string str = mas.Countryadd(txtcountry.Text, txtid.Text);
        if (str == "1")
        {
            Response.Write("<script>alert('Country Already Exist!!!!')</script>");

        }
        else if (str == "3")
        {

            Response.Write("<script>alert('Country Added Succesfully')</script>");
        }
        else
        {
            Label1.Text = str;
        }
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串str=mas.Countryadd(txtcountry.Text,txtid.Text);
如果(str==“1”)
{
回答。写下(“警告(‘国家已经存在!!!!’)”);
}
否则如果(str==“3”)
{
回复。填写(“警报(‘已成功添加国家’)”;
}
其他的
{
Label1.Text=str;
}
}

这不是最漂亮的代码。将字符串作为一种状态代码返回通常是不好的做法,因为您不知道可以返回的可能值的范围以及它们的含义。至少考虑整数或EnUM(命名为)。< /P> 也就是说,我将在单独的方法中处理检查和插入,并在click事件处理程序中捕获异常-让单个方法承担单个责任:

    private void AddCountry(string country, string id)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            string sql = string.Format("INSERT INTO Country (Id, Country) VALUES ('{0}', '{1}')", id, country);
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }
    }

    private bool Exists(string country, string id)
    {
        using (SqlConnection conn = new SqlConnection())
        {
            string sql = "SELECT Count(*) FROM Country WHERE Country='" + country + "'";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                int count = (int)cmd.ExecuteScalar();

                return count >= 1;
            }
        }
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (Exists(txtcountry.Text, txtid.Text))
            {
                Response.Write("<script>alert('Country Already Exist!!!!')</script>");
            }
            else
            {
                AddCountry(txtcountry.Text, txtid.Text);
                Response.Write("<script>alert('Country Added Succesfully')</script>");
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }           
    }
private void AddCountry(字符串国家,字符串id)
{
使用(SqlConnection conn=newsqlconnection())
{
stringsql=string.Format(“插入国家(Id,Country)值('{0}','{1}')”,Id,Country);
使用(SqlCommand cmd=newsqlcommand(sql,conn))
{
cmd.ExecuteNonQuery();
}
}
}
存在私有布尔值(字符串国家/地区,字符串id)
{
使用(SqlConnection conn=newsqlconnection())
{
string sql=“选择Count(*)FROM Country=”+Country+”;
使用(SqlCommand cmd=newsqlcommand(sql,conn))
{
int count=(int)cmd.ExecuteScalar();
返回计数>=1;
}
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
如果(存在(txtcountry.Text,txtid.Text))
{
回答。写下(“警告(‘国家已经存在!!!!’)”);
}
其他的
{
AddCountry(txtcountry.Text,txtid.Text);
回复。填写(“警报(‘已成功添加国家’)”;
}
}
捕获(例外情况除外)
{
Label1.Text=例如消息;
}           
}

您不能,
按钮1\u单击
永远无法看到异常消息;你不会还的。
Catch(Exception e)
{
Label.Text= e.Message;
}