Goran Bibic

Goran Bibic

  • 454
  • 2.9k
  • 180.8k

Data Reader error c#

Apr 3 2018 4:40 AM
If type value non bar code active is line 49 and no have problems multiple times...
 
Whene coplue times wrong bar code value I have error  line 25 on code
 
If is bar code from database ok working fine, if is bar code not ok firs time is ok, if wronf two times bar code errorr...
 
Error is INVALID ATTEMP TO READ WHEN NO DATA IS PRESENT
  1. private void prijavaAction()  
  2. {  
  3.     SqlConnection con = new SqlConnection(cs);
  4.     if (textBox1.Text.All(char.IsDigit))
  5.     {
                string queryString = "SELECT [ime_prezime] FROM dbo.radnici WHERE bar_kod = '" + textBox1.Text + "'";       
  6.        using (SqlConnection connection = new SqlConnection(cs))  
  7.        {  
  8.             SqlCommand command = new SqlCommand(queryString, connection);  
  9.             connection.Open();  
  10.             SqlDataReader reader = command.ExecuteReader();  
  11.             try  
  12.             {  
  13.                 if (reader.Read())  
  14.                 {  
  15.                     notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#2EFF36");  
  16.                     messageLabel.Text = (reader["ime_prezime"].ToString()) + " je prijavljen u \n " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");  
  17.                     readerresultTextbox.Text = (reader["ime_prezime"].ToString());  
  18.                 }  
  19.                 else  
  20.                 {  
  21.                     notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");  
  22.                     messageLabel.Text = "Radnik ne postoji u bazi! \nProverite vašu karticu ili probajte ponovo!";  
  23.                     textBox1.Text = "";  
  24.                 }  
  25.             }  
  26.             finally  
  27.             {
  28.                 if (reader.Read())  
  29.                 {  
  30.                     //Insert into  
  31.                 }  
  32.                 else  
  33.                 {  
  34.                     //update  
  35.                 }
  36.                 reader.Close();  
  37.             }  
  38.         }  
  39.    }  
  40.    else  
  41.    {  
  42.         notificationPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FD7400");
  43.         messageLabel.Text = "Bar kod nije važeci ili nije bar kod";  
  44.         textBox1.Text = "";  
  45.     }  
  46. }

Answers (26)