Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am practicing my own project, but I have syntax error that I can't figure it out.
This is Course Class that has courseID, courseName, description, and creditHour properties. DBSetup() method is connecting to the database. InsertDB() method is inserting the 4 values into the database.
CourseID, CourseName, Description are strings. CreditHour is int.

public string cmd;

public void InsertDB()
        {
            DBSetup();

            cmd = "INSERT into Patient values(" + getCourseID() + "," +
                             "'" + getCourseName() + "'," +
                             "'" + getDescription() + "'," +
                             getCreditHour() + ")";


            OleDbDataAdapter2.InsertCommand.CommandText = cmd;
            OleDbDataAdapter2.InsertCommand.Connection = OleDbConnection2;
            Console.WriteLine(cmd);

            try
            {
                OleDbConnection2.Open();
                int n = OleDbDataAdapter2.InsertCommand.ExecuteNonQuery();

                if (n == 1)
                    Console.WriteLine("Data Inserted...");
                else
                    Console.WriteLine("ERROR: Inserting Data");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                OleDbConnection2.Close();
            }
        } // end InsertDB()


What I have tried:

I think the syntax of the string cmd is wrong. I may put quotes and double qoutes wrong way. I was trying to figure this out for hours and hours. I cannot figure this out.
Posted
Updated 24-Sep-18 10:18am

Quote:
but I have syntax error that I can't figure it out.

The syntax of your query depend on the value of CourseName and Description, a simple apostrophe in those fields is enough to completely change the syntax. That problem is 'SQL Injection'.
C#
cmd = "INSERT into Patient values(" + getCourseID() + "," +
"'" + getCourseName() + "'," + "'" + getDescription() + "'," + getCreditHour() + ")";

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
The second best thing about doing it correctly (use parameters, not concatenated strings) is that you don't have to worry about all the quotes and single quotes; and it makes it a lot cleaner and easier to check the values.

Five minutes of my time went into this parameterized version:
C#
cmd = "INSERT into Patient values(@CourseID, @CourseName, @Description, @CreditHour)";
 
string CourseID = getCourseID();
string CourseName = getCourseName();
string Description = getDescription();
int CreditHour = getCreditHour();

// insert breakpoint somewhere in here to check the incoming values

OleDbDataAdapter2.InsertCommand.CommandText = cmd;
OleDbDataAdapter2.InsertCommand.Connection = OleDbConnection2;
OleDbDataAdapter2.InsertCommand.Parameters.AddWithValue("@CourseID", CourseID);
OleDbDataAdapter2.InsertCommand.Parameters.AddWithValue("@CourseName", CourseName);
OleDbDataAdapter2.InsertCommand.Parameters.AddWithValue("@Description", Description);
OleDbDataAdapter2.InsertCommand.Parameters.AddWithValue("@CreditHour", CreditHour);
 
Share this answer
 

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