如何从数据库中检索数据并将其显示在标签上 [英] How to retrieve data from database and display it on label

查看:92
本文介绍了如何从数据库中检索数据并将其显示在标签上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用幸运抽奖系统的注册页面。因此,注册过程是用户需要使用USB条形码扫描器扫描他们的条形码。我坚持使用用户扫描后无法检查数据库上的员工ID或输入文本框的那部分。我还希望在扫描条形码后显示员工姓名。任何人都可以给我一些想法/解决方案。



我尝试过:



以下是我的代码:



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.

What I have tried:

Below are my code:

using System;  
using System.Collections.Generic;  
using System.Data;  
using System.Data.SqlClient;  
using System.IO;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;
public partial class Attendance : System.Web.UI.Page  
{  
    SqlCommand cmd = new SqlCommand();  
    SqlConnection con = new SqlConnection();  
    string str;  
    private string connectionString;  
  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        con.ConnectionString = @"Data Source= (LocalDB)\MSSQLLocalDB; AttachDbFilename =   
          C:\Users\Nor  Asyraf  Mohd No\source\repos\LuckyDraw\LuckyDraw\App_Data\ticket.mdf;   
            Integrated Security = True";
        txtText.Focus();  
    }  
  
    protected void btnSave_Click(object sender, EventArgs e)  
    {  
        idcheck();  
        using (SqlConnection connection = new SqlConnection(connectionString))  
        {  
            using (SqlCommand command = connection.CreateCommand())  
            {  
                command.CommandText = "UPDATE EMPLOYEES SET Attendance = 'Present' WHERE EMP_ID = @id";  
                command.Parameters.AddWithValue("@id", txtText.Text);  
                connection.Open();  
  
                command.ExecuteNonQuery();  
  
                connection.Close();  
            }  
        }
    }  
  
public void idcheck()  
        {
        string query = "select EMP_ID from EMPLOYEES where EMP_ID='" + txtText.Text + "'";  
        SqlDataAdapter ada = new SqlDataAdapter(query, con);  
        DataTable dt = new DataTable();  
        ada.Fill(dt);  
        if (dt.Rows.Count > 0)  
        {  
            lblError.Text = dt.Rows[0]["EMP_NAME"].ToString();  
        }  
    }
    protected void Button1_Click1(object sender, EventArgs e)  
    {  
        Response.Redirect("Default.aspx");  
    }  
}

推荐答案

考虑到您的ID列是整数,那么您必须更正这些行

从EMPLOYEES中选择EMP_ID,其中EMP_ID ='+ txtText.Text +';



as从EMPLOYEES中选择EMP_ID,其中EMP_ID =+ txtText.Text +;删除单引号。
Considering your ID column is interger then you have to correct these line
"select EMP_ID from EMPLOYEES where EMP_ID='" + txtText.Text + "'"; "

as "select EMP_ID from EMPLOYEES where EMP_ID= " + txtText.Text + " "; " remove single quotes.


在select查询中添加EMP_NAME,如果EMP_ID是整数,则从where where中删除单引号
add EMP_NAME in select query and if EMP_ID is integer remove single quotes from where condition


从您的数据库表中执行SqlCommand字符串select *

尝试使用

datareader = cmd.ExecuteReader()执行并从数据库中读取数据,如

if(datareader.Read())

label.InnerText = datareader [EmpID]。ToString();



这里我使用< label>标签代替< asp:label>标签....它适用于我尝试这个
Execute the SqlCommand string select * from the database table of yours
Try to execute with the
datareader = cmd.ExecuteReader() and read the data from the database like
if(datareader.Read())
label.InnerText = datareader["EmpID"].ToString();

Here i use <label> tag instead of <asp:label> tag.... It worked for me try this


这篇关于如何从数据库中检索数据并将其显示在标签上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆