Results 1 to 6 of 6

Thread: [RESOLVED] erro in insert colon from textbox to DBaccess

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Resolved [RESOLVED] erro in insert colon from textbox to DBaccess

    Here is a code to insert data to DBaccess from textbox. When I write a colon(') in the textbox to add to DB it caused error and show me this Message "Syntax error in string in query expression '''')'

    vb Code:
    1. Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
    2.        & "Data Source =" & Application.StartupPath & "\Store.mdb ")
    3.         Dim cmd As New OleDbCommand
    4.         cmd.Connection = conn
    5.  
    6.         'Add text to DB
    7.         cmd.CommandText = "INSERT INTO TBinMaterial(inMaterial)  " & "values ('" & txtAddM.Text & "')  "
    8.         cmd.CommandType = CommandType.Text
    9.         conn.Open()
    10.         cmd.ExecuteNonQuery()
    11.         conn.Close()
    Last edited by nader; Oct 12th, 2008 at 09:02 PM.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: erro in insert colon from textbox to DBaccess

    Hey,

    You are going to want to avoid putting your variables directly into your query string like that, you are going to want to use Parameters on the command object instead:

    Code:
    Dim SqlString As String = "Insert Into TBinMaterial(inMaterial) Values (?)"
    Using conn As New OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & Application.StartupPath & "\Store.mdb ")
      Using cmd As New OleDbCommand(SqlString, conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("inMaterial", txtAddM.Text)
        conn.Open()
        cmd.ExecuteNonQuery()
      End Using
    End Using
    Hope that helps!!

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: erro in insert colon from textbox to DBaccess

    I couldn't understand but here is your code I tried and casue same problem.
    vb Code:
    1. Dim SqlString As String = "INSERT INTO TBinMaterial(inMaterial)  " & "values ('" & Form2.txtAddM.Text & "')  "
    2.         Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
    3.              & "Data Source =" & Application.StartupPath & "\Store.mdb ")
    4.         Dim cmd As New OleDbCommand(SqlString, conn)
    5.         cmd.CommandType = CommandType.Text
    6.         cmd.Parameters.AddWithValue("inMaterial", Form2.txtAddM.Text)
    7.         conn.Open()
    8.         cmd.ExecuteNonQuery()
    9.         conn.Close()

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: erro in insert colon from textbox to DBaccess

    Hey,

    I causes the same problem, because you are still putting the textbox.text directly into your query string. Doing this means that your query is subject to SQL injection, and that is a security risk.

    In the example that I posted, the query became:

    Code:
    Insert Into TBinMaterial(inMaterial) Values (?)
    Rather than what you have:

    Code:
    INSERT INTO TBinMaterial(inMaterial)  " & "values ('" & Form2.txtAddM.Text & "')
    By doing it the way that I have suggested, when the query executes, the ? in the query is replaced with the value in the corresponding parameter. i.e. if txtAddM.Text had "nader" in it, then when the query is executed, it would actually have:

    INSERT INTO TBinMaterial(inMaterial) values (nader)

    You see what I mean.

    Try directly replacing your code with what I have posted, and let me know if the same thing in happening.

    Also, I notice that you have not used the Using Statements that I suggested. Using these statements mean that you don't have to explicitly call the conn.Close() as once the Using Statement finishes, it disposes on the connection. Just an idea, but in my opinion it makes the code easier to read.

    Gary

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    Re: erro in insert colon from textbox to DBaccess

    Thank you for help!

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] erro in insert colon from textbox to DBaccess

    Not a problem at all. Happy to help.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width