Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 private void LoadData()
        {
            setConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "SELECT * FROM Car";
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DS.Reset();
            DB.Fill(DS);
            dgvCar.DataSource = DT
};

loads data.

C#
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 btnAdd_Click(object sender, EventArgs e)
       {
           string txtQuery = "INSERT INTO Car (car_id,plate_number,car_model,
                              car_brand,prev_maintenance,
                              next_maintenance,rate,description,carImage)
                              Values( '" + txtCarId.Text + "','" +
                              txtPlateNum.Text + "','"
                              + txtModel.Text + "','" + txtBrand.Text + "','" +
                              dtpPrev.Value.ToShortDateString() + "','"
                              + dtpNext.Value.ToShortDateString() + "','" +
                              numericPrice.Text + "','" + txtDesc.Text + "','" +
                              picBoxCar.Image + "')";

           ExecuteQuery(txtQuery);
           LoadData();
           MessageBox.Show("Car Added");
       }

adds data in my database.

What I have tried:

Nothing yet. Don't know what to write.
Posted
Updated 24-Mar-22 0:47am
v2

1 solution

Quote:
Nothing yet. Don't know what to write

Then you need to do some research. Try starting with . Work with Image Columns in DataGridView Control - Windows Forms .NET Framework | Microsoft Docs[^].

To get data from a database, you will need to use a SELECT statement - see SELECT (Transact-SQL) - SQL Server | Microsoft Docs[^].

The code you posted is not even attempting to populate the datagridview, but does have a problem you should address immediately. Your code is vulnerable to SQL Injection attack because you are creating the SQL statement by concatenating user input. See SQL Injection | OWASP Foundation[^] for why this is a bad idea and
SQL Injection Prevention - OWASP Cheat Sheet Series[^] for how to get around it using parameterized queries.

Have an attempt at doing this for yourself and we will be very happy to help you when you get stuck - but we will not write your code for you - even if we could work out what the problem is from so little information you have provided.
 
Share this answer
 
v2

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