Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

How can I go about comparing a textbox value inputted by a user, with a field in a database to check whether that value exists? I want the user to enter a barcode , and the system to check whether that bacode exists or not-and the user may procceed with a task,depending on the result.

Thanks.
Posted
Comments
abbaspirmoradi 7-Aug-13 10:29am    
ok.get text from textbox and search for it with query .Where is your problem?
Srinivas Kalabarigi 7-Aug-13 12:58pm    
have you checked my solution?

Firstly you will have to make a connection to the database. Depending on the type of database it is you can then run a SQL query someting similar to
SQL
SELECT * FROM tblBarcodes WHERE Barcode = '1234567890'

We will need more information on the database being used to assist with such connection. Also is the database on a network or is it running on the local PC.
 
Share this answer
 
v2
Here is the algorithm:

1. Read the textbox value
2. Send the value to database, using stored procedure execute the below query to check whether the values exist or not:

SQL
declare @itexists as int;

if exists(select * from MyTable where Barcode=@barcode)
    set @itexists=1;
else
    set @itexists=0;

print @itexists;


3. Get the result using ExecuteScalar and perform rest of the operations based on this value


In your case. just modify your database code as below:

C#
public bool CompareBarcode(string barcode)
       {
           using (SqlConnection conn = new SqlConnection(ConnectionSTring))
           {
               SqlCommand cmd = new SqlCommand("uspCheckBarcode", conn);
               cmd .CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@barcode", barcode);
               return Convert.ToBoolean(cmd.ExecuteScalar());

           }
 
Share this answer
 
v2
I am using an SQL Server database.. I created a stored procedure ( SELECT Barcode FROM Equipment WHERE barcode = @barcode)
-when I execute that query and type a registered barcode,it returns it and then when i type an unregistered barcode it doesnt return anything.

>>>Data Access layer
public DataTable CompareBarcode(string barcode)
       {
           using (SqlConnection conn = new SqlConnection(ConnectionSTring))
           {

               DataTable dtBarcode = new DataTable();
               SqlCommand cmd = new SqlCommand("uspCheckBarcode'" + barcode + "'", conn);
               dbAdapter = new SqlDataAdapter(cmd);
               dbAdapter.Fill(dtBarcode);
               return dtBarcode;

           }

>>>Presentation layer
C#
private void txtInputBarcode_TextChanged(object sender, EventArgs e)
       {
           //checks whether barcode entered by user is registered, by checking from the database

               BusinessObject bo = new BusinessObject();
               bo.CompareBarcode(txtInputBarcode.Text);

               //lblBarcodeResult.Text = "Barcode registered";



               //lblBarcodeResult.Text = "Barcode is not registered";

       }

I need an "if" statement that's going to check whether barcode,and return one of the lblresult messages
 
Share this answer
 
Comments
Srinivas Kalabarigi 7-Aug-13 11:03am    
Updated my solution, please check and mark as solution and also rate. Thanks
By doing the compare in the Text change event is not a good idea. rather use something like if text.length = x then run your query. By using the text change event will create a request each time you type a character in the textbox. For a barcode of 13 characters (EAN 13) this will create 13 requests to the database. What does your stored procedure look like.
 
Share this answer
 
here is my stored procedure
ALTER PROCEDURE [dbo].[uspCheckBarcode]
@EquipmentBarcode nvarchar (50)
AS
BEGIN
BEGIN TRY
	BEGIN TRANSACTION
	SELECT [Equipment Barcode]
	FROM Equipment
	WHERE [Equipment Barcode]= @EquipmentBarcode
	COMMIT TRANSACTION;
	END TRY
			BEGIN CATCH
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION
			END
				END CATCH
END
 
Share this answer
 

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