Asyraf MN

Asyraf MN

  • 2.2k
  • 28
  • 6.6k

How to check id in database and display the name on label

Mar 22 2018 1:43 AM
I'm working on the Registration page for Lucky Draw system.So the process for registration is user need to scan their barcode using usb barcode scanner. I stuck on that part which i cannot check the employee id on database after user scan or type in the textbox. I also want to display the employee name after scan the barcode.Can anyone give me some ideas/solution.
 
Below is my database:
 
 
 
Code : 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;
  10. public partial class Attendance : System.Web.UI.Page  
  11. {  
  12.     SqlCommand cmd = new SqlCommand();  
  13.     SqlConnection con = new SqlConnection();  
  14.     string str;  
  15.     private string connectionString;  
  16.   
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.         con.ConnectionString = @"Data Source= (LocalDB)\MSSQLLocalDB; AttachDbFilename =   
  20.           C:\Users\Nor  Asyraf  Mohd No\source\repos\LuckyDraw\LuckyDraw\App_Data\ticket.mdf;   
  21.             Integrated Security = True";
  22.         txtText.Focus();  
  23.     }  
  24.   
  25.     protected void btnSave_Click(object sender, EventArgs e)  
  26.     {  
  27.         idcheck();  
  28.         using (SqlConnection connection = new SqlConnection(connectionString))  
  29.         {  
  30.             using (SqlCommand command = connection.CreateCommand())  
  31.             {  
  32.                 command.CommandText = "UPDATE EMPLOYEES SET Attendance = 'Present' WHERE EMP_ID = @id";  
  33.                 command.Parameters.AddWithValue("@id", txtText.Text);  
  34.                 connection.Open();  
  35.   
  36.                 command.ExecuteNonQuery();  
  37.   
  38.                 connection.Close();  
  39.             }  
  40.         }
  41.     }  
  42.   
  43. public void idcheck()  
  44.         {
  45.         string query = "select EMP_ID from EMPLOYEES where EMP_ID='" + txtText.Text + "'";  
  46.         SqlDataAdapter ada = new SqlDataAdapter(query, con);  
  47.         DataTable dt = new DataTable();  
  48.         ada.Fill(dt);  
  49.         if (dt.Rows.Count > 0)  
  50.         {  
  51.             lblError.Text = dt.Rows[0]["EMP_NAME"].ToString();  
  52.         }  
  53.     }
  54.     protected void Button1_Click1(object sender, EventArgs e)  
  55.     {  
  56.         Response.Redirect("Default.aspx");  
  57.     }  
  58. }

Answers (4)