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;
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.
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')
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.
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....
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.
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. :)
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
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
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
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
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....
Without sqlHelper:
Hope it helps you get going!
/Finn
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
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
is working on a reply...
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.