Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a timer in my form, it starts when camera button is click. I want to check if the user have expired ID or not and insert the ID in (checkin) table Given below is my code so far, kindly review and give feedback.

What I have tried:

C#
private void Timer1_Tick(object sender, EventArgs e)
        {
            BarcodeReader Reader = new BarcodeReader();
            Result result = Reader.Decode((Bitmap)pictureBox1.Image);
            try
            {
                string decoded = result.ToString().Trim();
                if (decoded != "")
                {
                    timer1.Stop();
                    MessageBox.Show(decoded);

                    using (SqlConnection con = new SqlConnection("Data Source=SQL5037.site4now.net;Initial Catalog=DB_A448D1_Dragon;User Id=***********;Password=******"))
                    {
                        con.Open();
                        try
                        {
                            using (SqlCommand com = new SqlCommand("select count(*)from enddate where ID=@ID and startdate <=@C1 and endDate >=@C2", con))
                            {

                                com.Parameters.AddWithValue("@ID", decoded.Trim());
                                com.Parameters.AddWithValue("@C1", DateTime.Now);
                                com.Parameters.AddWithValue("@C2", DateTime.Now);
                                
                                int count = (int)com.ExecuteScalar();
                                if (count > 0)
                                {
                                    using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time) VALUES (@ID,@time)", con))
                                    {
                                        com1.Parameters.AddWithValue("@ID", decoded.Trim());

                                        com1.Parameters.AddWithValue("@time", txttime.Text);


                                        com1.ExecuteNonQuery();
                                    }
                                    Form2 form2 = new Form2();
                                    form2.Show();
                                    //MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                else
                                {
                                    //MetroFramework.MetroMessageBox.Show(this, "this ID not Exist ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    Form3 form3 = new Form3();
                                    form3.Show();

                                }
                            }
                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            if (con.State == ConnectionState.Open)
                                con.Close();
                        }
                       
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


I think might be the problem with (Decode) ,What will be the right way to do that .

Posted
Updated 18-Mar-19 3:00am
v4
Comments
MadMyche 18-Mar-19 7:04am    
What Barcode reader are you using?
el_tot93 18-Mar-19 7:14am    
do you have any idea
MadMyche 18-Mar-19 9:23am    
BarcodeReader is not a native class, a project/package would need to have been added to your solution to allow this class to be called (along with Result)
el_tot93 18-Mar-19 7:06am    
Qr code

How many times have we told you now? 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?
 
Share this answer
 
Comments
el_tot93 18-Mar-19 4:49am    
plz how can i get the result and us it
OriginalGriff 18-Mar-19 5:14am    
How many times have we told you about SQL Injection now?
And you are still doing it. So you aren't listening to us, you aren't learning anything. You are still finding bits of code that sort of do what you want, but instead of trying to f=work it out, you just bolt them into your app and ask us to sort it out.

If you are going to ignore what we say instead of listening or learning, why should we want to help you?
el_tot93 18-Mar-19 5:37am    
check now
OriginalGriff 18-Mar-19 5:37am    
So why didn't you do that in the first place?
el_tot93 18-Mar-19 5:45am    
i learned using that way bout now i have another problem all of that is testing only
C#
private void Timer1_Tick(object sender, EventArgs e)
       {
           BarcodeReader Reader = new BarcodeReader();
           Result result = Reader.Decode((Bitmap)pictureBox1.Image);
           try
           {
               //string decoded = result.ToString().Trim();
               if (result != null)
               {

                   //MessageBox.Show(result.Text);

                   using (SqlConnection con = new SqlConnection("Data Source=SQL5037.site4now.net;Initial Catalog=DB_A448D1_Dragon;User Id=************;Password=**********"))
                   {
                       con.Open();
                       try
                       {
                           using (SqlCommand com = new SqlCommand("select count(*)from enddate where ID=@ID and startdate <=@C1 and endDate >=@C2", con))
                           {

                               com.Parameters.AddWithValue("@ID", result.Text);
                               com.Parameters.AddWithValue("@C1", DateTime.Now);
                               com.Parameters.AddWithValue("@C2", DateTime.Now);

                               int count = (int)com.ExecuteScalar();
                               if (count > 0)
                               {
                                   using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time) VALUES (@ID,@time)", con))
                                   {
                                       com1.Parameters.AddWithValue("@ID", result.Text);

                                       com1.Parameters.AddWithValue("@time", txttime.Text);


                                       com1.ExecuteNonQuery();
                                   }
                                   timer1.Stop();
                                   Form2 form2 = new Form2();
                                   form2.Show();
                                   this.Close();
                                   //MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                               }
                               else
                               {
                                   timer1.Stop();
                                   //MetroFramework.MetroMessageBox.Show(this, "this ID not Exist ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                   Form3 form3 = new Form3();
                                   form3.Show();
                                   this.Close();

                               }

                           }
                       }

                       catch (Exception ex)
                       {
                           MessageBox.Show(ex.Message);
                       }
                       finally
                       {
                           if (con.State == ConnectionState.Open)
                               con.Close();
                       }

                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
 
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