Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Martin Rud 103 posts 295 karma points
    Jan 23, 2010 @ 15:59
    Martin Rud
    0

    Usercontrol: "error in your SQL syntax" - hint needed

    Hi forum,

    I am a newbie and need a hint on one of my first usercontrols. I want to store some data in a custom table (not one of Umbraco´s but still in the Umbraco database).

    My usercontrol is like this:


    --------------------
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using umbraco.DataLayer;

    namespace mbeHelloWorld.Usercontrols
    {
        public partial class mbeHello : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ISqlHelper SqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
                SqlHelper.ExecuteNonQuery(@"INSERT INTO 'my_custom_table' ('title') VALUES ('some value')");
            }
        }
    }
    -------------------------

    I get this error:

     Exception Details: MySql.Data.MySqlClient.MySqlException: 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 ''my_custom_table' ('title') VALUES ('some value')' at line 1

     


    Notice that I run the Umbraco on a MySql-database.

     

    Regards,

    Martin Bernt Rud

  • Finn 86 posts 50 karma points
    Jan 23, 2010 @ 17:25
    Finn
    0

    Hi

    I don't know anything about the sqlHelper thing, but a normal sql insert statement would be: INSERT INTO my_custom_table (title) VALUES ('some value')

    hth

  • Martin Rud 103 posts 295 karma points
    Jan 23, 2010 @ 21:15
    Martin Rud
    0

    Hi,

    Thanks for your help, but it didn´t help me. I am afraid my problem is that I don´t know much of .NET. So mayby my whole code is build wrong. I just found an example code and build upon it. Maybe my imported namespaces isn´t useful. Maybe I should not use SqlHelper. i don´t know.

     

    Could anyone give me an example on a usercontrol (all the code) that inserts a row in a table. Like the INSERT INTO my_custom_table (title) VALUES ('some value')

     

    regards, Martin

  • Richard Soeteman 3875 posts 12037 karma points MVP
    Jan 24, 2010 @ 07:38
    Richard Soeteman
    0

    Hi in your sample code you have put single quotes around the table and column name, remove them and it will work I think. So in other words, copy the query that you made bold in your last reply and use that in your sample code.

    Cheers,

    Richard

  • Martin Rud 103 posts 295 karma points
    Jan 24, 2010 @ 11:13
    Martin Rud
    0

    Hi Richard,

    I have tried several ways including the query in my last reply: SqlHelper.ExecuteNonQuery("INSERT INTO my_custom_table (title) VALUES ('some value')");

    But it gives this error: MySql.Data.MySqlClient.MySqlException: Table 'database_name.MY_CUSTOM_TABLE' doesn't exist

    But the table do exist! Only its name is not in capital letters but lowercase. Don´t know why the table name seems to be processed with capital letters by .NET....

     

  • Finn 86 posts 50 karma points
    Jan 24, 2010 @ 11:16
    Finn
    0

    Without sqlHelper:

    using System.Data;
    using System.Data.SqlClient;

    ~~~~~

    string strConn = ConfigurationManager.ConnectionStrings("Your_ConnectionString").ToString();
    SqlConnection DBConnection = default(SqlConnection);
    SqlCommand DBCommand = default(SqlCommand);
    string strSQL = string.Empty;
    Int32 rowsaffected = 0;


    DBConnection = new SqlConnection(strConn);
    strSQL = "INSERT INTO my_custom_table (title) VALUES ('some value')";

    DBCommand = new SqlCommand(strSQL, DBConnection);
    DBCommand.CommandType = CommandType.Text;
    DBConnection.Open();
       
    rowsaffected = DBCommand.ExecuteNonQuery();
       
    DBConnection.Close();
    DBConnection.Dispose();

    if (rowsaffected < 1)
    {
    Response.Write("Error!");
    }

     

    Hope it helps you get going!

    /Finn

  • Paul Sørensen 302 posts 648 karma points
    Jan 24, 2010 @ 12:54
    Paul Sørensen
    0

    Hi Martin

    It's because the Umbraco datalayer converts it to uppercase. I had the same problem when I first started using MySQL a year ago and the MySQL I was using wasn't case-insensitive. If you look at the tables Umbraco creates during installation I'm pretty sure they are all Uppercased. I don't know if this problem has been solved in the current version.

    I think your solution is to create your custom tables uppercase - or do as I did - start using MSSQL.

    /Paul S

  • Martin Rud 103 posts 295 karma points
    Jan 25, 2010 @ 19:59
    Martin Rud
    0

    Hi Finn and Paul,

     

    Thanks for your replies. It seems to be the right way to do it the way Finn writes. I found another code last evening - in the essense it was like Finn´s last example. And now it works!

    And Pauls explanation is what I have discovered too. I will keep on using MySql - my hosting has that as default. :)

     

    Regards,

    Martin

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies