名称"数据"在当前上下文中不存在

Sem*_*ian 1 c#

我正在尝试在类中生成try catch方法,但我正面临这个错误消息,请帮我解决这个问题.我的班级是

public string Countryadd(string country, string id)
{

     try
     {
         string data="0";
         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//If Country Name Not Exists
         {
             string qry = "insert into Country values('" + id + "','" + country + "')";//Insertin data into database Table Country
             conn.nonquery(qry);
         }

     }
     catch (Exception Ex)
     {
         ShowPopUpMsg(Ex.Message);
     }
     return data;
 }
Run Code Online (Sandbox Code Playgroud)

thu*_*eys 5

您需要在try块之前放置数据的定义:

   string data="0";

   try {
Run Code Online (Sandbox Code Playgroud)

{}括号定义一个变量的范围.

您只能访问该范围内的变量.