Goran Bibic

Goran Bibic

  • 482
  • 2.9k
  • 197.1k

After reader read wrong value insert new row...????

Apr 4 2018 4:01 AM
After reader read wrong value insert new row...????
 
I need to reader do NOTHING
  1. private void prijavaAction()  //testna  
  2. {
  3.     {  
  4.                 SqlConnection con = new SqlConnection(cs);
  5.                 if (textBox1.Text.All(char.IsDigit))
  6.                 {  
  7.                     string queryString = "SELECT [ime_prezime] FROM dbo.radnici WHERE bar_kod = '" + textBox1.Text + "'";// pronaci radnika u bazi            
  8.                     using (SqlConnection connection = new SqlConnection(cs))  
  9.                     {  
  10.                         SqlCommand command = new SqlCommand(queryString, connection);  
  11.                         connection.Open();  
  12.                         SqlDataReader reader = command.ExecuteReader();  
  13.                         try  
  14.                         {  
  15.                             if (reader.Read())  //radi      
  16.                                                 //while (reader.Read())  //radi
  17.                             {  
  18.                                 notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#2EFF36");  
  19.                                 messageLabel.Text = (reader["ime_prezime"].ToString()) + " je prijavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");  
  20.                                 readerresultTextbox.Text = (reader["ime_prezime"].ToString());  
  21.                             }  
  22.                             else  
  23.                             {  
  24.                                 notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");  
  25.                                 messageLabel.Text = "Radnik ne postoji u bazi! \nProverite vašu karticu ili probajte ponovo!";  
  26.                                 textBox1.Text = "";  
  27.                                 readerresultTextbox.Clear();           
  28.                             }  
  29.                         }  
  30.                         finally  
  31.                         {  
  32.                             string prijava = "SELECT count(*) from dbo.prijava_radnika where vrijemeprijave is not null and vrijemeodjave is null and radnik = '" + readerresultTextbox.Text + "' and redni_broj is NULL";  
  33.                             SqlCommand provjeraprijaveradnika = new SqlCommand(prijava, con);  
  34.                             con.Open();  
  35.                             int result = Convert.ToInt32(provjeraprijaveradnika.ExecuteScalar());  
  36.                             //con.Open();           
  37.                             con.Close();  
  38.                             if ((result == 0)) //radi            
  39.                                                //if ((prijava !))            
  40.                             {  
  41.                                 String saveStaff = "INSERT INTO dbo.prijava_radnika (vrijemeprijave, status, radnik,idfakture) VALUES(@vrijemeprijave,'prijavljen', '" + readerresultTextbox.Text + "',@idfakture)";  
  42.                                 using (SqlConnection openCon2 = new SqlConnection(cs))  
  43.                                 {  
  44.                                     using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  45.                                     {  
  46.                                         querySaveStaff.Connection = openCon2;  
  47.                                         querySaveStaff.Parameters.Add("@vrijemeprijave", SqlDbType.DateTime).Value = DateTime.Now;  
  48.                                         querySaveStaff.Parameters.Add("@idfakture", SqlDbType.Int).Value = id_fakture;  
  49.                                         openCon2.Open();  
  50.                                         querySaveStaff.ExecuteNonQuery();  
  51.                                         openCon2.Close();  
  52.   
  53.                                         //notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFE11A");      
  54.                                         // messageLabel.Text = (reader["ime_prezime"].ToString()) + " je prijavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");      
  55.                                     }  
  56.                                 }  
  57.                             }  
  58.                             else  
  59.                             {
  60.                                 String updateStaff = " declare @maxNo integer = 0 select @maxNo = isnull(max(redni_broj), 0) from[dbo].[prijava_radnika] UPDATE[dbo].[prijava_radnika] SET"  
  61.                                     + " [vrijemeodjave] = @vrijemeodjave,[redni_broj] = (@maxNo+1),[status] = 'odjavljen' WHERE radnik = '" + readerresultTextbox.Text + "'and[status] = 'prijavljen'";  
  62.                                 //string queryString = "SELECT [ime_prezime] FROM dbo.radnici WHERE bar_kod = '" + textBox1.Text + "'";// pronaci radnika u bazi            
  63.                                 using (SqlConnection connection1 = new SqlConnection(cs))  
  64.                                 {  
  65.                                     SqlCommand command1 = new SqlCommand(queryString, connection1);  
  66.                                     connection1.Open();  
  67.                                     SqlDataReader reader1 = command1.ExecuteReader();  
  68.                                     connection.Close();  
  69.                                     try  
  70.                                     {  
  71.                                         if (reader1.Read())  
  72.                                         {
  73.   
  74.                                             using (SqlConnection openCon3 = new SqlConnection(cs))  
  75.                                             {  
  76.                                                 using (SqlCommand queryupdateStaff = new SqlCommand(updateStaff))  
  77.                                                 {  
  78.                                                     queryupdateStaff.Connection = openCon3;  
  79.                                                     queryupdateStaff.Parameters.Add("@vrijemeodjave", SqlDbType.DateTime).Value = DateTime.Now;  
  80.                                                     openCon3.Open();  
  81.                                                     queryupdateStaff.ExecuteNonQuery();  
  82.                                                     openCon3.Close();  
  83.   
  84.                                                 }  
  85.                                             }  
  86.   
  87.                                             notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFE11A");  
  88.                                             messageLabel.Text = (reader1["ime_prezime"].ToString()) + " je odjavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");  
  89.                                         }  
  90.                                     }  
  91.                                     catch { }  
  92.   
  93.                                      
  94.                                 }  
  95.                                 // Always call Close when done reading.      
  96.   
  97.                                 reader.Close();  
  98.                             }  
  99.                         }  
  100.                     }  
  101.                 }  
  102.                 else  
  103.                 {  
  104.                     notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");  
  105.                     messageLabel.Text = "Bar kod nije važeci ili nije bar kod";  
  106.                     textBox1.Text = "";  
  107.                 }  
  108.             }  
  109.         }

Answers (19)