Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
My goal is to create a new database for new brands that the user will add on the website. I've created a code for doing such task but it is giving me this exception: An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near 'char'.

I already looked at the code several times but i still can't find the root cause of the problem.

What I have tried:

C#
public static void CreateGuitarBrandsDatabase(string brand)
{
    SqlConnection createBrandData = new SqlConnection(@"Data Source=Y560\SQLEXPRESS;Initial Catalog=GuitarItemsDB;Integrated Security=True");
    createBrandData.Open();
    SqlCommand cmdBrandData = new SqlCommand("CREATE TABLE guitarItem" + brand + "(id int,type char(50),model char(50),price float,image1 char(255),image2 char(255),description text,neck type char(100),body char(100), fretboard char(100),fret char(50),bridge char(100),neck pickup char(100),bridge pickup char(100),hardware_color char(50));", createBrandData);
    cmdBrandData.ExecuteNonQuery();//The exception seems to be pointing right here
    createBrandData.Close();
}
Posted
Updated 16-Feb-17 18:34pm
Comments
dan!sh 16-Feb-17 23:24pm    
Does your query work in SSMS?

This is very very basic, "NO space in an identifier", be it a variable name, object name, function name, class name, table name, or field name. You may get away with it by enclosing such an identifier with back tick in MySQL or [] in SQL Server, but what the heck. Just don't include a space.
If a name consists of multiple words, use underscore to separate them or Camel case - Wikipedia[^].
 
Share this answer
 
Assuming you want to keep the space in the column name.

neck type should be [neck type]
neck pickup should be [neck pickup]
bridge pickup should be [bridge pickup]

I also suggest to add bracket to description = [description] (not an issue)

Also, please take a look at the article SQL Injection and Cross-Site Scripting[^] when you have time.
 
Share this answer
 
v2
as an extension to 'Solution 1', you also have

'neck pickup'
'bridge pickup'

which shouldnt have a space between them
 
Share this answer
 
neck type should be [neck type] or necktype
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 17-Feb-17 0:39am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900