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. SQL insert from ASP class

SQL insert from ASP class

Scheduled Pinned Locked Moved ASP.NET
databasesysadminsecurityhelpquestion
6 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.
  • A Offline
    A Offline
    AaronM_NZ
    wrote on last edited by
    #1

    I'm stumped. I have a function in a class module that can be called from within the project, which will insert an event into a database. I call it ok, but it fails with: Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'localhostname'. I suspect its something to do with the connection string, but I just cant work it out. I dont know how else it could get my local pc host name in the error. Have I taken the wrong approach completely for this? Public Shared Function CreateEvent( ByVal strMessage, ByVal strCategory, Optional ByVal strEnvironment = "0", Optional ByVal strNodeId = "0") Dim sqlConn = New System.Data.SqlClient.SqlConnection sqlConn.ConnectionString = "Server=;Initial Catalog=eventlog;User Id=user;password=password;Trusted_Connection=False" Dim SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand Dim strEventTime As Date = DateTime.Now Dim strUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name 'Try Dim cmdInsert As New SqlCommand("INSERT INTO eventlog (enviromnent_id, category, node_id, event, username, event_time) VALUES (" _ & "'" & strEnvironment & "', " _ & "'" & strCategory & "', " _ & "'" & strNodeId & "', " _ & "'" & strMessage & "', " _ & "'" & strUserName & "', " _ & "'" & strEventTime & "', ", sqlConn) sqlConn.Open() cmdInsert.ExecuteNonQuery() 'Catch ex As Exception 'Finally sqlConn.Close() 'End Try End Function

    G C 2 Replies Last reply
    0
    • A AaronM_NZ

      I'm stumped. I have a function in a class module that can be called from within the project, which will insert an event into a database. I call it ok, but it fails with: Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'localhostname'. I suspect its something to do with the connection string, but I just cant work it out. I dont know how else it could get my local pc host name in the error. Have I taken the wrong approach completely for this? Public Shared Function CreateEvent( ByVal strMessage, ByVal strCategory, Optional ByVal strEnvironment = "0", Optional ByVal strNodeId = "0") Dim sqlConn = New System.Data.SqlClient.SqlConnection sqlConn.ConnectionString = "Server=;Initial Catalog=eventlog;User Id=user;password=password;Trusted_Connection=False" Dim SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand Dim strEventTime As Date = DateTime.Now Dim strUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name 'Try Dim cmdInsert As New SqlCommand("INSERT INTO eventlog (enviromnent_id, category, node_id, event, username, event_time) VALUES (" _ & "'" & strEnvironment & "', " _ & "'" & strCategory & "', " _ & "'" & strNodeId & "', " _ & "'" & strMessage & "', " _ & "'" & strUserName & "', " _ & "'" & strEventTime & "', ", sqlConn) sqlConn.Open() cmdInsert.ExecuteNonQuery() 'Catch ex As Exception 'Finally sqlConn.Close() 'End Try End Function

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      It's more likely that the data you are using is corrupting the sql query. You don't encode the strings properly when you put them in the query, so any string containing an apostrophe would break it. Replace apostrophes in the strings with double apostrophes, or better yet, use a parameterized query. --- b { font-weight: normal; }

      C A 2 Replies Last reply
      0
      • G Guffa

        It's more likely that the data you are using is corrupting the sql query. You don't encode the strings properly when you put them in the query, so any string containing an apostrophe would break it. Replace apostrophes in the strings with double apostrophes, or better yet, use a parameterized query. --- b { font-weight: normal; }

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Guffa wrote:

        Replace apostrophes in the strings with double apostrophes

        Aiiii!! :eek: I would never recommend that. I'd go straight for the paramerised query and not even tell them how to escape apostrophes. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

        1 Reply Last reply
        0
        • A AaronM_NZ

          I'm stumped. I have a function in a class module that can be called from within the project, which will insert an event into a database. I call it ok, but it fails with: Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'localhostname'. I suspect its something to do with the connection string, but I just cant work it out. I dont know how else it could get my local pc host name in the error. Have I taken the wrong approach completely for this? Public Shared Function CreateEvent( ByVal strMessage, ByVal strCategory, Optional ByVal strEnvironment = "0", Optional ByVal strNodeId = "0") Dim sqlConn = New System.Data.SqlClient.SqlConnection sqlConn.ConnectionString = "Server=;Initial Catalog=eventlog;User Id=user;password=password;Trusted_Connection=False" Dim SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand Dim strEventTime As Date = DateTime.Now Dim strUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name 'Try Dim cmdInsert As New SqlCommand("INSERT INTO eventlog (enviromnent_id, category, node_id, event, username, event_time) VALUES (" _ & "'" & strEnvironment & "', " _ & "'" & strCategory & "', " _ & "'" & strNodeId & "', " _ & "'" & strMessage & "', " _ & "'" & strUserName & "', " _ & "'" & strEventTime & "', ", sqlConn) sqlConn.Open() cmdInsert.ExecuteNonQuery() 'Catch ex As Exception 'Finally sqlConn.Close() 'End Try End Function

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Your connection string does not specify a server. You must specify a server. If you want to use the local machine you can use (local), if you know you are going to use TCP/IP as the transport you can also specify localhost, but I don't recommend that if it is sitting on the same machine. Next, as Guffa already pointed out, the way your SQL Query is being built up is poorly designed. In fact it would allow a SQL Injection Attack to take place quite easily. You may like to read SQL Injection Attacks and Tips on How to Prevent Them[^] in order to defend yourself, and your database from a mallicious attack via your web application. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          A 1 Reply Last reply
          0
          • G Guffa

            It's more likely that the data you are using is corrupting the sql query. You don't encode the strings properly when you put them in the query, so any string containing an apostrophe would break it. Replace apostrophes in the strings with double apostrophes, or better yet, use a parameterized query. --- b { font-weight: normal; }

            A Offline
            A Offline
            AaronM_NZ
            wrote on last edited by
            #5

            Oh dear, the most obvious thing I over looked. The query was working fine, it was throwing the error as strMessage contained apostrophes... I feel like a right dork now! Thanks for your help, issue resolved.

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              Your connection string does not specify a server. You must specify a server. If you want to use the local machine you can use (local), if you know you are going to use TCP/IP as the transport you can also specify localhost, but I don't recommend that if it is sitting on the same machine. Next, as Guffa already pointed out, the way your SQL Query is being built up is poorly designed. In fact it would allow a SQL Injection Attack to take place quite easily. You may like to read SQL Injection Attacks and Tips on How to Prevent Them[^] in order to defend yourself, and your database from a mallicious attack via your web application. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

              A Offline
              A Offline
              AaronM_NZ
              wrote on last edited by
              #6

              I deleted the server name when I posted the code up, I should have replaced it with something generic. As per above, it was the strMessage that contained apostrophies, which caused the error. Thanks for the link, Ill have a good read of it.

              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