Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Try-Catch problem in C# console app

Try-Catch problem in C# console app

Scheduled Pinned Locked Moved C#
helpcsharpquestion
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 4645801
    wrote on last edited by
    #1

    I have the following code snippet in a C# console application and am getting the following error in VS2008 on the last line (bool recExists = myReader.HasRows): Error 2 Use of unassigned local variable 'myReader' Here's the code: string sResponse; SqlDataReader myReader; // Check existence of tracking record sQuery = "Select * from tracking where shipper_id = '" + sShipperId + "' AND shipper_ack = '" + sShipperAck + "'"; myCommand1 = new SqlCommand(sQuery, conn); try { myReader = myCommand1.ExecuteReader(); } catch (Exception e2) { conn.Close(); conn.Dispose(); sResponse = e2.GetBaseException().ToString() + " " + sQuery; } **bool recExists = myReader.HasRows**; I use this same identical code in a Windows Client app with no problems. What am I missing???? Thanks, Dave

    C L Z C 4 Replies Last reply
    0
    • M Member 4645801

      I have the following code snippet in a C# console application and am getting the following error in VS2008 on the last line (bool recExists = myReader.HasRows): Error 2 Use of unassigned local variable 'myReader' Here's the code: string sResponse; SqlDataReader myReader; // Check existence of tracking record sQuery = "Select * from tracking where shipper_id = '" + sShipperId + "' AND shipper_ack = '" + sShipperAck + "'"; myCommand1 = new SqlCommand(sQuery, conn); try { myReader = myCommand1.ExecuteReader(); } catch (Exception e2) { conn.Close(); conn.Dispose(); sResponse = e2.GetBaseException().ToString() + " " + sQuery; } **bool recExists = myReader.HasRows**; I use this same identical code in a Windows Client app with no problems. What am I missing???? Thanks, Dave

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If that's a copy and paste of the code, it should work fine. I'm assuming there's something else that's not clear from what you copied and pasted. Is all of this an exact copy and paste of the whole function ? Of course, it's not good code. You should check if myreader is null, because if execute reader blows up, it could well be.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      1 Reply Last reply
      0
      • M Member 4645801

        I have the following code snippet in a C# console application and am getting the following error in VS2008 on the last line (bool recExists = myReader.HasRows): Error 2 Use of unassigned local variable 'myReader' Here's the code: string sResponse; SqlDataReader myReader; // Check existence of tracking record sQuery = "Select * from tracking where shipper_id = '" + sShipperId + "' AND shipper_ack = '" + sShipperAck + "'"; myCommand1 = new SqlCommand(sQuery, conn); try { myReader = myCommand1.ExecuteReader(); } catch (Exception e2) { conn.Close(); conn.Dispose(); sResponse = e2.GetBaseException().ToString() + " " + sQuery; } **bool recExists = myReader.HasRows**; I use this same identical code in a Windows Client app with no problems. What am I missing???? Thanks, Dave

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Change SqlDataReader myReader; to SqlDataReader myReader = null; and you should be fine

        M C 2 Replies Last reply
        0
        • L Lost User

          Change SqlDataReader myReader; to SqlDataReader myReader = null; and you should be fine

          M Offline
          M Offline
          Member 4645801
          wrote on last edited by
          #4

          Excellent.... That worked great, but why do I need to set the value to null in the console app and not in the Windows Forms app????

          L 1 Reply Last reply
          0
          • M Member 4645801

            Excellent.... That worked great, but why do I need to set the value to null in the console app and not in the Windows Forms app????

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            (Just a guess since I don't know all your code) Maybe the SqlDataReader used to be a field instead of a local? Fields are initialized automatically whereas locals aren't so they often need explicit initialization

            1 Reply Last reply
            0
            • L Lost User

              Change SqlDataReader myReader; to SqlDataReader myReader = null; and you should be fine

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Oh - yeah, I see the issue now. That is really dumb, IMO, I've had that happen too.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              1 Reply Last reply
              0
              • M Member 4645801

                I have the following code snippet in a C# console application and am getting the following error in VS2008 on the last line (bool recExists = myReader.HasRows): Error 2 Use of unassigned local variable 'myReader' Here's the code: string sResponse; SqlDataReader myReader; // Check existence of tracking record sQuery = "Select * from tracking where shipper_id = '" + sShipperId + "' AND shipper_ack = '" + sShipperAck + "'"; myCommand1 = new SqlCommand(sQuery, conn); try { myReader = myCommand1.ExecuteReader(); } catch (Exception e2) { conn.Close(); conn.Dispose(); sResponse = e2.GetBaseException().ToString() + " " + sQuery; } **bool recExists = myReader.HasRows**; I use this same identical code in a Windows Client app with no problems. What am I missing???? Thanks, Dave

                Z Offline
                Z Offline
                Zoki Manas
                wrote on last edited by
                #7

                Try your code like this: string sResponse; SqlDataReader myReader; bool recExists = false; // Check existence of tracking record sQuery = "Select * from tracking where shipper_id = '" + sShipperId + "' AND shipper_ack = '" + sShipperAck + "'"; myCommand1 = new SqlCommand(sQuery, conn); try { myReader = myCommand1.ExecuteReader(); bool recExists = myReader.HasRows; } catch (Exception e2) { conn.Close(); conn.Dispose(); sResponse = e2.GetBaseException().ToString() + " " + sQuery; } // for example return recExists; Hope this helps

                1 Reply Last reply
                0
                • M Member 4645801

                  I have the following code snippet in a C# console application and am getting the following error in VS2008 on the last line (bool recExists = myReader.HasRows): Error 2 Use of unassigned local variable 'myReader' Here's the code: string sResponse; SqlDataReader myReader; // Check existence of tracking record sQuery = "Select * from tracking where shipper_id = '" + sShipperId + "' AND shipper_ack = '" + sShipperAck + "'"; myCommand1 = new SqlCommand(sQuery, conn); try { myReader = myCommand1.ExecuteReader(); } catch (Exception e2) { conn.Close(); conn.Dispose(); sResponse = e2.GetBaseException().ToString() + " " + sQuery; } **bool recExists = myReader.HasRows**; I use this same identical code in a Windows Client app with no problems. What am I missing???? Thanks, Dave

                  C Offline
                  C Offline
                  carlecomm
                  wrote on last edited by
                  #8

                  change "SqlDataReader myReader;" to "SqlDataReader myReader = null;" and try again.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups