0

i am trying to use MySQL 5 and asp.net 4

i succeed withe the linking and display the data from MySQL DB but the problem is within the DML Commands !!

please take a look to my code

Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyConnection As String = "server=localhost;User Id=root;password=123;database=cms"

        Dim Connection As New MySqlConnection(MyConnection)

        Connection.Open()

        Dim Sql As String = "INSERT INTO [CMS.tbimages] ( Title,Description) VALUES (parm2,parm3);"
        Dim cmd As New MySqlCommand(Sql, Connection)

        cmd.Parameters.Add(New MySqlParameter("parm2", "hajjaj"))
        cmd.Parameters.Add(New MySqlParameter("parm3", "hajjaj"))

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class

what i did is a simple page for testing the insert command , I added a button when i click on it it should do the insert command!!

but when i click it it gives me error :

#42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[CMS.tbimages] ( Title,Description) VALUES (parm2,parm3)' at line 1

not : i tried many other ways to change the insert statement :

"INSERT INTO [tbimages] ( [Title], [Description]) VALUES ( ? , ?)"
"INSERT INTO [tbimages] ([Title], [Description])
VALUES (?Title , ?Description)"
INSERT INTO [tbimages] ( [Title], [Description]) VALUES (Title, Description)

1
  • Be sure to use the functions at above the post text and the preview window below it to ensure that your post is going to appear the way you want it. :) Commented Jan 23, 2011 at 7:49

2 Answers 2

3

Try this:

Dim Sql As String = "INSERT INTO CMS.tbimages ( Title,Description) VALUES (?parm2,?parm3);"
Dim cmd As New MySqlCommand(Sql, Connection)

cmd.Parameters.Add(New MySqlParameter("?parm2", "hajjaj"))
cmd.Parameters.Add(New MySqlParameter("?parm3", "hajjaj"))

Also, this tutorial may be of help.

Sign up to request clarification or add additional context in comments.

1 Comment

oooh ya ,that is it. thank you so much. my problem is solved
1

MySQL doesn't use brackets around identifiers, it uses backticks:

Dim Sql As String = "INSERT INTO `CMS.tbimages` (Title,Description) VALUES (?,?)"

If the table names doesn't cause any conflicts, you don't need to enclose it in backticks either:

Dim Sql As String = "INSERT INTO CMS.tbimages (Title,Description) VALUES (?,?)"

I'm not sure how your database driver handles parameters, but I think they should be unnamed.

1 Comment

yes you are right !! but for my application i used this and it's working: Dim Sql As String = "INSERT INTO CMS.tbimages ( Title,Description) VALUES (?parm2,?parm3);" thank you for your help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.