Search code examples
c#datasettry-catchtry-catch-finally

the name ds does not exit in the content 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;

i write my code above but show this error whenever i use same code without try catch its work great

the name ds does not exit in the content


Solution

  • You must define ds outside the try{} block

     DataSet ds = new DataSet();
        try
        {
           //...
        }
        catch(Exception e)
        {
           //...
        }