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
  1. Home
  2. Web Development
  3. ASP.NET
  4. VB to C# SQL Server conversion error

VB to C# SQL Server conversion error

Scheduled Pinned Locked Moved ASP.NET
csharpdatabasesql-serversysadminhelp
18 Posts 3 Posters 0 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.
  • W wildfiction

    Can anyone see what I'm doing wrong here? I have translated some (simple) VB code to C# but the original works while the C# doesn't. Here it is: VB Code: (works) Dim oDR as System.Data.SQLClient.SQLDataReader Dim oCom As System.Data.SQLClient.SqlCommand Dim oConn as System.Data.SQLClient.SQLConnection try oConn = New System.Data.SQLClient.SQLConnection ("server=MACH2\vsdotnet; initial catalog=tablexxx; uid=test2; pwd=test2") oConn.Open() ... C# Code: (Gives error: SQL Server does not exist or access denied) System.Data.SqlClient.SqlDataReader oDR; System.Data.SqlClient.SqlCommand oCom; System.Data.SqlClient.SqlConnection oConn; try { oConn = new System.Data.SqlClient.SqlConnection("server=MACH2\vsdotnet; initial catalog=tablexxx; uid=test2; pwd=test2"); oConn.Open(); ... Any ideas?

    A Offline
    A Offline
    Albert Pascual
    wrote on last edited by
    #2

    try this: server=MACH2\vsdotnet;user id=test2;password=test2;database=tablexxx Good luck Al

    W 1 Reply Last reply
    0
    • A Albert Pascual

      try this: server=MACH2\vsdotnet;user id=test2;password=test2;database=tablexxx Good luck Al

      W Offline
      W Offline
      wildfiction
      wrote on last edited by
      #3

      Thanks for the idea Al. I've tried that but no luck - unfortunately. Exactly the same error. Thanks again for taking the time to look at the problem!

      A 1 Reply Last reply
      0
      • W wildfiction

        Thanks for the idea Al. I've tried that but no luck - unfortunately. Exactly the same error. Thanks again for taking the time to look at the problem!

        A Offline
        A Offline
        Albert Pascual
        wrote on last edited by
        #4

        No allow to thank me until we find the problem. Please can you test this for me? SqlConnection myConnection = new SqlConnection(YOURCONNECTIONSTRING); SqlCommand myCommand = new SqlCommand(sQuery, myConnection); myCommand.CommandTimeout = 120; // 60 Seconds Timeout myConnection.Open(); SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

        W 1 Reply Last reply
        0
        • A Albert Pascual

          No allow to thank me until we find the problem. Please can you test this for me? SqlConnection myConnection = new SqlConnection(YOURCONNECTIONSTRING); SqlCommand myCommand = new SqlCommand(sQuery, myConnection); myCommand.CommandTimeout = 120; // 60 Seconds Timeout myConnection.Open(); SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

          W Offline
          W Offline
          wildfiction
          wrote on last edited by
          #5

          That didn't help either Al - exactly the same error. I've just had a thought. Remember with C/C++ you can choose the calling parameter convention and the order in which to push and pop parameters off the stack? i.e. PASCAL or CDECL or something like that? Well I'm just wondering if this is the problem now... What I'm doing to test this is switching between a testCS.aspx page as the start page and a testVB.aspx start page. Does the project need to know a calling convention or some other global setting to make one work? Or can you just mix and match VB and C# in a project. I'm aware that you can't mix C# and VB in a file but I was under the impression that an ASPX file can be in either or and you can mix the files together in a project. Thanks again for your help.

          A 1 Reply Last reply
          0
          • W wildfiction

            That didn't help either Al - exactly the same error. I've just had a thought. Remember with C/C++ you can choose the calling parameter convention and the order in which to push and pop parameters off the stack? i.e. PASCAL or CDECL or something like that? Well I'm just wondering if this is the problem now... What I'm doing to test this is switching between a testCS.aspx page as the start page and a testVB.aspx start page. Does the project need to know a calling convention or some other global setting to make one work? Or can you just mix and match VB and C# in a project. I'm aware that you can't mix C# and VB in a file but I was under the impression that an ASPX file can be in either or and you can mix the files together in a project. Thanks again for your help.

            A Offline
            A Offline
            Albert Pascual
            wrote on last edited by
            #6

            :doh: NO!! If you got a project in C# you cannot add a Page is aspx.vb!!! You can only have a C# project or a VB project. Al

            W 1 Reply Last reply
            0
            • A Albert Pascual

              :doh: NO!! If you got a project in C# you cannot add a Page is aspx.vb!!! You can only have a C# project or a VB project. Al

              W Offline
              W Offline
              wildfiction
              wrote on last edited by
              #7

              The VB and C# code is working side by side in the same project because it is in an ASPX file and not in the code behind file. In fact, when I initially tried to do this conversion from VB to C# there was no code behind file. I've subsequently tried all sorts of combinations: I've completely removed the ASPX file holding the VB code. I've used the C# code only in the aspx file (i.e. inline) and also in a code behind file but same results: SQL Server error with C# but not when using VB. As you can see from my initial post, the code is very simple and straight forward and should translate without a problem.

              A 1 Reply Last reply
              0
              • W wildfiction

                The VB and C# code is working side by side in the same project because it is in an ASPX file and not in the code behind file. In fact, when I initially tried to do this conversion from VB to C# there was no code behind file. I've subsequently tried all sorts of combinations: I've completely removed the ASPX file holding the VB code. I've used the C# code only in the aspx file (i.e. inline) and also in a code behind file but same results: SQL Server error with C# but not when using VB. As you can see from my initial post, the code is very simple and straight forward and should translate without a problem.

                A Offline
                A Offline
                Albert Pascual
                wrote on last edited by
                #8

                If you put the code is the ASPX page instead of the code behind you are correct should compile. Now, I can't belive that does not work. Any chance you can send me the complete project to albert@autojockey.com? Cheers Al

                W 1 Reply Last reply
                0
                • A Albert Pascual

                  If you put the code is the ASPX page instead of the code behind you are correct should compile. Now, I can't belive that does not work. Any chance you can send me the complete project to albert@autojockey.com? Cheers Al

                  W Offline
                  W Offline
                  wildfiction
                  wrote on last edited by
                  #9

                  The email is on its way. Thanks Al.

                  A 2 Replies Last reply
                  0
                  • W wildfiction

                    The email is on its way. Thanks Al.

                    A Offline
                    A Offline
                    Albert Pascual
                    wrote on last edited by
                    #10

                    We will figure out. Sometimes is late at night and something that we over looked is the problem, but we need another sets of eyes to figure it out. Cheers Al

                    1 Reply Last reply
                    0
                    • W wildfiction

                      The email is on its way. Thanks Al.

                      A Offline
                      A Offline
                      Albert Pascual
                      wrote on last edited by
                      #11

                      Think I got it, intead of calling the public function Page_Data() put the whole thing in the page ASPX so ... I did not compile it yet and test it, I would do it if this does not work. <% System.Data.SqlClient.SqlDataReader oDR; System.Data.SqlClient.SqlCommand oCom; System.Data.SqlClient.SqlConnection oConn; //string connString = "server=MACH2\vsdotnet; initial catalog=chatlogs; uid=test2; pwd=test2"; string connString = "server=MACH2\vsdotnet; Database=chatlogs; User ID=test2; Password=test2"; oConn = new System.Data.SqlClient.SqlConnection(connString); try { oConn.Open(); oCom = new System.Data.SqlClient.SqlCommand(); oCom.Connection = oConn; oCom.CommandText = "SELECT * FROM cl_channel"; oDR = oCom.ExecuteReader(); while(oDR.Read()) { Response.Write(oDR["network"] + ", " + oDR["channel"]); Response.Write(" "); } oConn.Close(); } catch(Exception err) { Response.Write("Error: " + err.Message); } finally { oDR = null; oCom = null; oConn.Close(); oConn = null; } %>

                      W 1 Reply Last reply
                      0
                      • A Albert Pascual

                        Think I got it, intead of calling the public function Page_Data() put the whole thing in the page ASPX so ... I did not compile it yet and test it, I would do it if this does not work. <% System.Data.SqlClient.SqlDataReader oDR; System.Data.SqlClient.SqlCommand oCom; System.Data.SqlClient.SqlConnection oConn; //string connString = "server=MACH2\vsdotnet; initial catalog=chatlogs; uid=test2; pwd=test2"; string connString = "server=MACH2\vsdotnet; Database=chatlogs; User ID=test2; Password=test2"; oConn = new System.Data.SqlClient.SqlConnection(connString); try { oConn.Open(); oCom = new System.Data.SqlClient.SqlCommand(); oCom.Connection = oConn; oCom.CommandText = "SELECT * FROM cl_channel"; oDR = oCom.ExecuteReader(); while(oDR.Read()) { Response.Write(oDR["network"] + ", " + oDR["channel"]); Response.Write(" "); } oConn.Close(); } catch(Exception err) { Response.Write("Error: " + err.Message); } finally { oDR = null; oCom = null; oConn.Close(); oConn = null; } %>

                        W Offline
                        W Offline
                        wildfiction
                        wrote on last edited by
                        #12

                        I tried that before Al. That's how it was originally when it didn't work. Only when it didn't work did I move it into the code behind page to see if that would make it work. But I will try again anyway and report back...

                        A 1 Reply Last reply
                        0
                        • W wildfiction

                          I tried that before Al. That's how it was originally when it didn't work. Only when it didn't work did I move it into the code behind page to see if that would make it work. But I will try again anyway and report back...

                          A Offline
                          A Offline
                          Albert Pascual
                          wrote on last edited by
                          #13

                          Don't try again, I'll stop being lazy and test your app tomorrow morning. I'll report the results to you. I am just being lazy. I started working this morning at 7:30 and now at 9:30 PM I am still in the computer, so I cannot take more code :wtf: However tomorrow morning I'll be fresh again to help you with. Last thought did you try to put the IP address instead of the name? localhost if its the same computer Talk to you tomorrow morning Al

                          W 1 Reply Last reply
                          0
                          • A Albert Pascual

                            Don't try again, I'll stop being lazy and test your app tomorrow morning. I'll report the results to you. I am just being lazy. I started working this morning at 7:30 and now at 9:30 PM I am still in the computer, so I cannot take more code :wtf: However tomorrow morning I'll be fresh again to help you with. Last thought did you try to put the IP address instead of the name? localhost if its the same computer Talk to you tomorrow morning Al

                            W Offline
                            W Offline
                            wildfiction
                            wrote on last edited by
                            #14

                            I haven't tried that Al but will do - I'm about to crash as well. Thanks for all your help today. Perhaps tomorrow we'll crack it :)

                            1 Reply Last reply
                            0
                            • W wildfiction

                              Can anyone see what I'm doing wrong here? I have translated some (simple) VB code to C# but the original works while the C# doesn't. Here it is: VB Code: (works) Dim oDR as System.Data.SQLClient.SQLDataReader Dim oCom As System.Data.SQLClient.SqlCommand Dim oConn as System.Data.SQLClient.SQLConnection try oConn = New System.Data.SQLClient.SQLConnection ("server=MACH2\vsdotnet; initial catalog=tablexxx; uid=test2; pwd=test2") oConn.Open() ... C# Code: (Gives error: SQL Server does not exist or access denied) System.Data.SqlClient.SqlDataReader oDR; System.Data.SqlClient.SqlCommand oCom; System.Data.SqlClient.SqlConnection oConn; try { oConn = new System.Data.SqlClient.SqlConnection("server=MACH2\vsdotnet; initial catalog=tablexxx; uid=test2; pwd=test2"); oConn.Open(); ... Any ideas?

                              D Offline
                              D Offline
                              Daniel Santillanes
                              wrote on last edited by
                              #15

                              I haven't followed all this thread... but just a small check... in C#, the '\' character has to be written as '\\' in a string constant right? so for example, if your server name is MACH2\vsdotnet, your string connection for C# should be oConn = new System.Data.SqlClient.SqlConnection("server=**MACH2\\vsdotnet**; initial catalog=tablexxx; uid=test2; pwd=test2"); Just wondering, because porting from one programming language to another shouldn't make such a problem :doh:. daniero

                              W 1 Reply Last reply
                              0
                              • D Daniel Santillanes

                                I haven't followed all this thread... but just a small check... in C#, the '\' character has to be written as '\\' in a string constant right? so for example, if your server name is MACH2\vsdotnet, your string connection for C# should be oConn = new System.Data.SqlClient.SqlConnection("server=**MACH2\\vsdotnet**; initial catalog=tablexxx; uid=test2; pwd=test2"); Just wondering, because porting from one programming language to another shouldn't make such a problem :doh:. daniero

                                W Offline
                                W Offline
                                wildfiction
                                wrote on last edited by
                                #16

                                daniero - you are a genius. I knew it was going to be something simple like that that I'd overlooked and another pair of eyes would spot. That fixed the problem - thanks again daniero. Also thanks to Al for all the time and effort he put in helping me here. The solution was so simple it alluded us for an entire day.

                                A D 2 Replies Last reply
                                0
                                • W wildfiction

                                  daniero - you are a genius. I knew it was going to be something simple like that that I'd overlooked and another pair of eyes would spot. That fixed the problem - thanks again daniero. Also thanks to Al for all the time and effort he put in helping me here. The solution was so simple it alluded us for an entire day.

                                  A Offline
                                  A Offline
                                  Albert Pascual
                                  wrote on last edited by
                                  #17

                                  :doh: and :doh: again We both need to catch up in our sleep. Thanksgiving is tomorrow, can't wait to get off the computer Cheers Al

                                  1 Reply Last reply
                                  0
                                  • W wildfiction

                                    daniero - you are a genius. I knew it was going to be something simple like that that I'd overlooked and another pair of eyes would spot. That fixed the problem - thanks again daniero. Also thanks to Al for all the time and effort he put in helping me here. The solution was so simple it alluded us for an entire day.

                                    D Offline
                                    D Offline
                                    Daniel Santillanes
                                    wrote on last edited by
                                    #18

                                    I'm glad I could help :). Those kind of things always get on my nerves as well :doh:, I always try to think of the easiest mistakes. daniero

                                    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