Hi

Once I complie below coding part,it give me error meassage.The Error Meassage is Error

. The name 'CurrentOutLet' does not exist in the current context

BelowI write my coding

.
public SqlDataReader Current_outletReport(string code, string startdate, string enddate)
       {

           try
           {
               SqlDataReader CurrentOutLet;
               SqlConnection con;

               string str = "SELECT SUM(Active_Outlet) AS ............";
               con = DbCon.create_sqlconnection();
               con.Open();
               SqlCommand cmd = new SqlCommand(str, con);
               CurrentOutLet = cmd.ExecuteReader();
               con.Close();

           }
           catch (Exception e)
           {

           }
           return CurrentOutLet;
       }

Thanks
Tank50

Dani AI

Generated

As pointed out, the compile error in 's code comes from scoping: a variable declared inside the try block is not visible at the return statement. Beyond that quick fix there are three important issues to address so the method is correct and robust: avoid swallowing exceptions with an empty catch, do not close the SqlConnection before the caller finishes reading a SqlDataReader, and ensure resources are always disposed.

A safer pattern is to load results into a disconnected container (DataTable) and return that. This lets the method close the connection immediately and avoids lifetime issues with the reader:

public DataTable GetOutletReport(string param1, string param2)
{
    using (var con = DbCon.create_sqlconnection())
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandText = "/* SQL */";
        con.Open();
        using (var rdr = cmd.ExecuteReader())
        {
            var dt = new DataTable();
            dt.Load(rdr);
            return dt;
        }
    }
}

If returning a SqlDataReader is required, do not close the connection before returning; use CommandBehavior.CloseConnection so closing the reader also closes the connection:

public SqlDataReader GetOutletReader(string p1, string p2)
{
    var con = DbCon.create_sqlconnection();
    var cmd = new SqlCommand("/* SQL */", con);
    con.Open();
    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}

Checklist:

  • Declare the reader variable outside the try or return directly; initialize to null if needed.
  • Never swallow exceptions—log and rethrow (throw;) or return a clear error result.
  • Prefer using and returning a DataTable or DTO for simpler resource management.
  • If returning a reader, use CommandBehavior.CloseConnection and document that the caller must close the reader.

See Microsoft docs for details on ExecuteReader and CommandBehavior and for guidance on using and DataTable.Load:
SqlCommand.ExecuteReader,
CommandBehavior.CloseConnection,
using statement,
DataTable.Load.

Recommended Answers

All 3 Replies

You need to declare "CurrentOutLet" before the try...catch block. Copy and paste the code below; Good luck!

public SqlDataReader Current_outletReport(string code, string startdate, string enddate)
       {
         SqlDataReader CurrentOutLet;

           try
           {
              SqlConnection con;

               string str = "SELECT SUM(Active_Outlet) AS ............";
               con = DbCon.create_sqlconnection();
               con.Open();
               SqlCommand cmd = new SqlCommand(str, con);
               CurrentOutLet = cmd.ExecuteReader();
               con.Close();

           }
           catch (Exception e)
           {

           }
           return CurrentOutLet;
       }
.
public SqlDataReader Current_outletReport(string code, string startdate, string enddate)
       {

           try
           {
               SqlDataReader CurrentOutLet;
               SqlConnection con;

               string str = "SELECT SUM(Active_Outlet) AS ............";
               con = DbCon.create_sqlconnection();
               con.Open();
               SqlCommand cmd = new SqlCommand(str, con);
               CurrentOutLet = cmd.ExecuteReader();
               con.Close();

           }
           catch (Exception e)
           {

           }
           return CurrentOutLet;
       }

You need to declare "CurrentOutLet" before the try...catch block. Good luck!

HI

Thanks

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.