Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 857557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:25:02+00:00 2026-05-15T08:25:02+00:00

What is wrong with the scope in the following code? namespace t2 { public

  • 0

What is wrong with the scope in the following code?

namespace t2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////// string connectionString = "Data Source=A-63A9D4D7E7834\\SQLEXPRESS;Initial Catalog=Test1;Integrated Security=True";
            //connection string i taken from the file class1.cs static class.

            try
            {
                string commandString = "Select * from Table_1";

                SqlConnection conn = new SqlConnection(Class1.connection);
                SqlCommand command = new SqlCommand(commandString, conn);
                conn.Open();

                // Start a local transaction.
                SqlTransaction sqlTran = conn.BeginTransaction();
                command.Transaction = sqlTran;

                try
                {
                    SqlDataReader reader = command.ExecuteReader();
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                }//try ends here.
                catch (SqlException ex)
                {
                    //Console.WriteLine(ex.Message);
                    try
                    {
                        // Attempt to roll back the transaction.
                        sqlTran.Rollback();
                    }
                    catch (Exception exRollback)
                    {
                        // Throws an InvalidOperationException if the connection
                        // is closed or the transaction has already been rolled
                        // back on the server.
                        Response.Write(exRollback.Message.ToString());
                    }
                    //    <snipped> build error message
                }//sql catch ends here
                catch (Exception all)
                {
                    try
                    {
                        // Attempt to roll back the transaction.
                        sqlTran.Rollback();
                    }
                    catch (Exception exRollback)
                    {
                        // Throws an InvalidOperationException if the connection
                        // is closed or the transaction has already been rolled
                        // back on the server.
                        Response.Write(exRollback.Message.ToString());
                    }

                    Response.Write(all.Message.ToString());
                }//catch all ends
            }//main try
            finally
            {
                // Commit the transaction.
                sqlTran.Commit();
                reader.Close();
                reader.Dispose();
                conn.Close();
                conn.Dispose();
             }//finally ends
        }//page load method ends

Errors displayed are:

Error   1   The name 'sqlTran' does not exist in the current context    D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 162 17  t2
Error   2   The name 'reader' does not exist in the current context D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 166 17  t2
Error   3   The name 'reader' does not exist in the current context D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 167 17  t2
Error   4   The name 'conn' does not exist in the current context   D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 169 17  t2

Error   5   The name 'conn' does not exist in the current context   D:\DOCUMENTSS\Visual Studio 2008\Projects\t2\t2\Default.aspx.cs 170 17  t2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:25:02+00:00Added an answer on May 15, 2026 at 8:25 am

    Those variables are declared inside the try portions of your code. Inside the catch {} portions, those variables do not exist. To fix the problem, you need to declare the variables outside the try block (setting them to null), and then you can instantiate them in try block. Be sure to check for null values in the catch blocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What did I do wrong? Here is an excerpt from my code: public void
I am trying to create instance of class javax.servlet.ServletException with following code public class
Whats wrong with the following c# code? Compiler reports this error: Inconsistent accessibility: parameter
Whats wrong with this code? -(void) drawRect:(CGRect) rect { CGContextRef c = UIGraphicsGetCurrentContext(); if
I have the following code inside a class. (It's coffeescript-- and it's for a
I have the following class: public abstract class AbstractParent { static String method() {
I have a 'strange' problem, the following code converts the location lat value into
I've got the following bit of code, which I've narrowed down to be causing
I am trying to compile the following very very simple piece of source code:
I have the following code: QPair<QSharedPointer<unsigned int>, int> someclass::somefunction() { int siz = data_size();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.