Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
>string txtQuery = "INSERT INTO Client (ID,fName,lName,address,contact,age,gender) Values('"+txtID.Text+"','"+txtFname.Text+"', '"+txtLname.Text+"','"+txtAddress.Text+"','"+txtContact.Text+"',+'"+txtAge.Text+"','"+cbGender.Text+"')"; 
```
cannot find the error in this line of code
```


      private void setConnection()
        {
            sql_con = new SQLiteConnection("Data Source = carRentalDB.db;Version = 3; New = False; Compress = True;");

        }

        private void LoadData()
        {
            setConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "SELECT * FROM Client";
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DS.Reset();
            DB.Fill(DS);
            DT = DS.Tables[0];
            UserControl2 user = new UserControl2();
            user.dgvUser2.DataSource = DT;
            sql_con.Close();

        }


        private void ExecuteQuery(String txtQuery)
        {
            setConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            sql_cmd.CommandText = txtQuery; 
            sql_cmd.ExecuteNonQuery();
            sql_con.Close();
        }


        private void addClient_Load(object sender, EventArgs e)
        {
            LoadData();
        }


```


    CREATE TABLE "Client" (
    	"ID"	INTEGER NOT NULL UNIQUE,
    	"fName"	TEXT NOT NULL,
    	"lName"	TEXT NOT NULL,
    	"address"	TEXT NOT NULL,
    	"contact"	INTEGER NOT NULL,
    	"age"	INTEGER NOT NULL,
    	"gender"	TEXT NOT NULL,
    	PRIMARY KEY("ID" AUTOINCREMENT)
    )


What I have tried:

still looking for typo because of the error im my data type in my DB
Posted
Updated 2-Mar-22 5:09am

You have a + character inside the single quotes just before txtAge.Text. But more importantly you are leaving yourself vulnerable to SQL injection: see bobby-tables.com: A guide to preventing SQL injection[^]. You should always use parameterised queries for SQL, and also actually validate the answers before you add them to the database. As it is your users can type any garbage in those fields and it will all be inserted. And finally, storing a person's age in a database is totally pointless; it could be their birthday tomorrow so the value will be wrong.
 
Share this answer
 
Comments
Ernest Jan Sandoval 2-Mar-22 10:22am    
string txtQuery = "INSERT INTO Client (ID,fName,lName,address,contact,age,gender) Values('" + txtID.Text + "','" + txtFname.Text + "', '" + txtLname.Text + "','" + txtAddress.Text + "','" + txtContact.Text + "','" + dtpClient.Value.ToShortDateString() + "','" + cbGender.Text+"')"; i change the age to dateTimePicker.still data type mismatch
Richard MacCutchan 2-Mar-22 10:37am    
Because you are trying to store text strings into fields that are not text types. And you should store Dates as Date or DateTime types, not as strings. And, as I said above, you should not be doing it like this in the first place; you should be using proper parameterised queries.
Ernest Jan Sandoval 2-Mar-22 10:23am    
CREATE TABLE "Client" (
"ID" INTEGER NOT NULL UNIQUE,
"fName" TEXT NOT NULL,
"lName" TEXT NOT NULL,
"address" TEXT NOT NULL,
"contact" INTEGER NOT NULL,
"age" INTEGER NOT NULL,
"gender" TEXT NOT NULL,
PRIMARY KEY("ID" AUTOINCREMENT)
)
Maciej Los 2-Mar-22 10:58am    
5ed!
In addition to solution #1 by Richard MacCutchan, i'll show you how to properly insert data into SqLite database:

C#
//create command
using var cmd = new SQLiteCommand(con);
//define insert statement - using parameters
cmd.CommandText = "INSERT INTO cars(name, price) VALUES(@name, @price)";
//add parameters with values
cmd.Parameters.AddWithValue("@name", "BMW");
cmd.Parameters.AddWithValue("@price", 36600);
cmd.Prepare();
//go!
cmd.ExecuteNonQuery();


In very similar way, you have to delete and select data.
 
Share this answer
 
Comments
Richard MacCutchan 2-Mar-22 11:33am    
+5
Maciej Los 2-Mar-22 13:42pm    
Thank you, Richard.

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

  Print Answers RSS


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