C# 名称ds不存在于内容try catch中

C# 名称ds不存在于内容try catch中,c#,dataset,try-catch,try-catch-finally,C#,Dataset,Try Catch,Try Catch Finally,我在上面写了我的代码,但每当我使用相同的代码而没有尝试捕捉它的工作时,就会显示这个错误 名称ds不存在于内容中您正在返回ds,这意味着您需要在开始尝试之前定义ds。不能保证trycatch块中的特定行会存在 尝试在Try-catch之前定义变量,看看这是否适合您 try { string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString; SqlConnection

我在上面写了我的代码,但每当我使用相同的代码而没有尝试捕捉它的工作时,就会显示这个错误


名称ds不存在于内容中

您正在返回ds,这意味着您需要在开始尝试之前定义ds。不能保证
try
catch
块中的特定行会存在

尝试在Try-catch之前定义变量,看看这是否适合您

try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.Fill(ds);

}
catch (Exception e)
{
    //throw new Exception( message.ex);

    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);

}


return ds;

您正在返回ds,这意味着需要在开始尝试之前定义ds。不能保证
try
catch
块中的特定行会存在

尝试在Try-catch之前定义变量,看看这是否适合您

try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.Fill(ds);

}
catch (Exception e)
{
    //throw new Exception( message.ex);

    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);

}


return ds;

您正在返回ds,这意味着需要在开始尝试之前定义ds。不能保证
try
catch
块中的特定行会存在

尝试在Try-catch之前定义变量,看看这是否适合您

try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.Fill(ds);

}
catch (Exception e)
{
    //throw new Exception( message.ex);

    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);

}


return ds;

您正在返回ds,这意味着需要在开始尝试之前定义ds。不能保证
try
catch
块中的特定行会存在

尝试在Try-catch之前定义变量,看看这是否适合您

try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    da.Fill(ds);

}
catch (Exception e)
{
    //throw new Exception( message.ex);

    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);

}


return ds;

必须在try{}块之外定义ds

DataSet ds = null;
try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    ds = new DataSet();
    da.Fill(ds);
}
catch (Exception e)
{
    //throw new Exception( message.ex);
    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);
}


return ds;

必须在try{}块之外定义ds

DataSet ds = null;
try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    ds = new DataSet();
    da.Fill(ds);
}
catch (Exception e)
{
    //throw new Exception( message.ex);
    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);
}


return ds;

必须在try{}块之外定义ds

DataSet ds = null;
try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    ds = new DataSet();
    da.Fill(ds);
}
catch (Exception e)
{
    //throw new Exception( message.ex);
    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);
}


return ds;

必须在try{}块之外定义ds

DataSet ds = null;
try
{
    string str = ConfigurationManager.ConnectionStrings["e_con_connection"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("GetProduct",con);

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ProductId", productid);

    SqlDataAdapter da = new SqlDataAdapter();
    ds = new DataSet();
    da.Fill(ds);
}
catch (Exception e)
{
    //throw new Exception( message.ex);
    HttpContext.Current.Response.Redirect("~/Error.aspx?err=" + e.Message);
}


return ds;