Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void button1_Click(object sender, EventArgs e)
    {
        if (comboBoxstaff.Text == string.Empty)
        {
            MessageBox.Show("Please Select gaugeman");  // not to let thecombobox empty
            return;
        }
        else if (comboBoxcompondrubber.Text == string.Empty)
        {
            MessageBox.Show("Please Select Compond Rubber");// not to let thecombobox empty
            return;

        }
        else if (textBox1.Text == string.Empty)
        {
            MessageBox.Show("Please Key in W.S thickness");// not to let thetextbox empty
            return;

        }
        else if (textBox2.Text == string.Empty)
        {
            MessageBox.Show("Please Key in G.S thickness");// not to let thecombobox empty

        }
        SQLiteConnection insertsess = new SQLiteConnection("Data Source=|DataDirectory|\\test1db");
        string insert12 = "INSERT INTO thickness (GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes) VALUES ('" + comboBoxstaff.Text + "','" + label2.Text + "', '" + comboBoxcompondrubber.Text + "', '" + textBox2.Text + "', '" + textBox1.Text + "')";   //insert statment
        SQLiteCommand ins1 = new SQLiteCommand(insert12, insertsess);
        insertsess.Open();
        ins1.ExecuteNonQuery();
        MessageBox.Show("Data had been saved");// showed when the message is being saved

        SQLiteConnection sesscheck = new SQLiteConnection("Data Source=|DataDirectory|\\test1db");
        SQLiteCommand chk1;
        chk1 = sesscheck.CreateCommand();
        chk1.CommandText = "SELECT GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes FROM thickness WHERE CompondRubber = '" + comboBoxcompondrubber.Text.Trim() + "'";
       sesscheck.Open();
        DataTable thicknessTable = new DataTable();
        //DataTable thicknessTable = new DataTable();
        SQLiteDataReader reader = chk1.ExecuteReader();
        thicknessTable.Load(reader);
        //SQLiteDataReader reader1 = chk2.ExecuteReader();
        //thicknessTable.Load(reader1);
        sesscheck.Close();

        dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;

    }


What I have tried:

I had tried using data adapter but it didn't work I tried using data reader but I am not sure on how to go about using it properly.

I want to get the value 12.0 to be saved and displayed into data grid view at the moment if I key 12.1 it also works only like 12.0 it will be entered and displayed as 12 at the data grid view
Posted
Updated 6-Jun-18 21:54pm

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

It doesn't matter if you use a DataReader or a DataAdapter - SQL injection starts when you concatenate strings. So this code is a problem:
C#
string insert12 = "INSERT INTO thickness (GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes) VALUES ('" + comboBoxstaff.Text + "','" + label2.Text + "', '" + comboBoxcompondrubber.Text + "', '" + textBox2.Text + "', '" + textBox1.Text + "')";   //insert statment
SQLiteCommand ins1 = new SQLiteCommand(insert12, insertsess);
insertsess.Open();
ins1.ExecuteNonQuery();
Convert it (all the rest of your app) to a parameterised query:
C#
string insert12 = "INSERT INTO thickness (GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes) VALUES (@GM, @DT, @CR, @GST, @WST)"
SQLiteCommand ins1 = new SQLiteCommand(insert12, insertsess);
ins1.Parameters.AddWithValue("@GM", comboBoxstaff.Text);
ins1.Parameters.AddWithValue("@DT", label2.Text);
ins1.Parameters.AddWithValue("@CR", comboBoxcompondrubber.Text);
ins1.Parameters.AddWithValue("@GST", textBox2.Text);
ins1.Parameters.AddWithValue("@WST", textBox1.Text);
insertsess.Open();
ins1.ExecuteNonQuery();

Note that you are responsible for closing and disposing of all database access objects, us it's also a very good idea to use a using block for teh Connection, Command, and suchlike.
I would also put the Connection.Open call close to the Connection construction, rather than mixed into the code further down. That way it's less likely to cause problems when you modify or copy the code.
 
Share this answer
 
Comments
Member 13809133 7-Jun-18 2:27am    
Do you mean like this @originalGriff

private void button1_Click(object sender, EventArgs e)
{
if (comboBoxstaff.Text == string.Empty)
{
MessageBox.Show("Please Select gaugeman");
return;
}
else if (comboBoxcompondrubber.Text == string.Empty)
{
MessageBox.Show("Please Select Compond Rubber");
return;

}
else if (textBox1.Text == string.Empty)
{
MessageBox.Show("Please Key in W.S thickness");
return;

}
else if (textBox2.Text == string.Empty)
{
MessageBox.Show("Please Key in G.S thickness");

}
SQLiteConnection insertsess = new SQLiteConnection("Data Source=|DataDirectory|\\test1db");
string insert12 = "INSERT INTO thickness (GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes) VALUES (@GM, @DT, @CR, @GST, @WST)";
SQLiteCommand ins1 = new SQLiteCommand(insert12, insertsess);
ins1.Parameters.AddWithValue("@GaugeMan", comboBoxstaff.Text);
ins1.Parameters.AddWithValue("@Dateandtime", label2.Text);
ins1.Parameters.AddWithValue("@CompondRubber", comboBoxcompondrubber.Text);
ins1.Parameters.AddWithValue("@GSthickness", textBox2.Text);
ins1.Parameters.AddWithValue("@WSthicknes", textBox1.Text);
insertsess.Open();
ins1.ExecuteNonQuery();
insertsess.Close();
MessageBox.Show("Data had been saved");



dataGridView1.DataSource = null;
SQLiteConnection conn123 = new SQLiteConnection("Data Source=|DataDirectory|\\test1db");
conn123.Open();
sda = new SQLiteDataAdapter("SELECT GaugeMan,Dateandtime, CompondRubber,GSthickness,WSthicknes FROM thickness WHERE CompondRubber = '" + comboBoxcompondrubber.Text.Trim() + "'", conn123);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
conn123.Close();



}
OriginalGriff 7-Jun-18 2:36am    
No, because the parameter names don't match up with the ones in the INSERT statement. And the second set of code is still vulnerable.
Plus, you open a connect, use it, close it, create a new one, open it, ... which is just silly.
Read what I said about "using" blocks, and don't hard code connection strings either.
Picking up on the other problem you mention
Quote:
I want to get the value 12.0 to be saved and displayed into data grid view at the moment if I key 12.1 it also works only like 12.0 it will be entered and displayed as 12 at the data grid view
The display has nothing to do with the way the number is stored or how you are getting it back from the database. You have to tell the dataGridView how to display it.

For example, right-click dataGridView1 and set the defaultCellStyle of the appropriate column(s) to "N2" (Numeric).

This assumes that you are storing the information on the database in a numeric, non-integer column type. If not then you need to change your table schema so that the column types match the type of data you want to store in them.
 
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