Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
Hi all,

I have a text box in a windows form. My question is, how to check existing text which is entered in text box through database? Please reply me with an example.
Posted
Updated 17-Jan-11 4:47am
v2
Comments
Ravi Sant 17-Jan-11 10:49am    
By this do u mean -
You can easily get txtBox.Text anytime to check data in it.
can you rephrase your question clearly please with few code lines?
OriginalGriff 17-Jan-11 12:15pm    
PLease do not post replies to answers using the "Add and Answer" feature: instead use the "Add Comment" at teh bottom right of each answer. That way the appropriate person gets an email to say you have more to say. With an answer, they don't, so they do not find out unless they revisit your question. I have moved your answer to a comment, and deleted the answer.
MeftahDAKHEEL 29-Mar-15 12:20pm    
i need to have a help with my code, i need to check if value exists in database and show a message...
=============================
my code
=============
private void btnSearch_Click(object sender, EventArgs e)
{


if (searchtext.Text == "")
{
MessageBox.Show("Plese Inter Employee's ID you are searching for");
searchtext.Focus();
}
else
{

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Employees.mdb");
OleDbDataAdapter ad = new OleDbDataAdapter("select * from Employee where EmpID=@EmpID", con);
ad.SelectCommand.Parameters.Add("@EmpID", OleDbType.Integer);
ad.SelectCommand.Parameters["@EmpID"].Value = int.Parse(searchtext.Text);


DataSet ds = new DataSet();
ad.Fill(ds, "Emp");
DGV1.DataSource = ds.Tables["Emp"];


if (ds == null)
{

MessageBox.Show("not found!!");

}
else
{
MessageBox.Show("The Employee you are searching for is listed down, if you want to update the data click the arrow sign on the lift side of the record");
}
}

Hi,
Make a stored procedure in SQL Server for example:


Create	Procedure	FindString(<br />
@MyString	nvarchar(50))<br />
As<br />
Begin<br />
		Select	*	From	MyTable<br />
					Where		Value = @MyString<br />
End


Make a Class in .NET For example :


public class ReadData
{
    public bool FindString(string myString)
    {
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Server=..."; //Your connection string
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "FindString";
        command.Parameters.AddWithValue("@MyString", myString);
        try
        {
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }                
        return false;
    }
}


Use the class. for example :

C#
ReadData r = new ReadData();

if (r.FindString("Shahingg"))
    MessageBox.Show("I Found it!");
else
    MessageBox.Show("I can't Find it!");


Do not forget that you need below namespaces for your Class :

C#
using System.Data.SqlClient;
using System.Data;



Good Luck
 
Share this answer
 
v4
Comments
Espen Harlinn 17-Jan-11 14:10pm    
5+ Good effort
ks ravi 18-Jan-11 0:21am    
hi thank u for reply,
i got confused at below code
ReadData r = new ReadData();

if (r.FindString("Shahingg"))//since i using textbox,how to include textbox value instead
// "Shahingg".
MessageBox.Show("I Found it!");
else
MessageBox.Show("I can't Find it!");
Shahin Khorshidnia 18-Jan-11 5:50am    
Ok,
It's very simple.

If you want to check that a string exists in your database. Do below :
1. You may get an instance of your class (ReadData):
ReadData r = new ReadData();

2. Pass your string (For example: "Shahin") to "FindString" method.
if(r.Findstring("Shahin"))...

The mehtod returns a boolean : True if Exists and False if doesn't exist.

The method (FindString) calls a stored procedure form your sql server.
hi,
CREATE PROCEDURE Procedure_Name
@mystring varchar(100),
@isExist bit out
AS
BEGIN
	if exists(select column1 from tblTable1 where column1=@mystring)
	begin
	select @isExist=1
	end
	else
	begin
	select @isExist=0
	end
	
	
END
GO



then write the method to check whether the value exist or not
thatis if @isExist=1 that means the value exist.otherwise not..
Hope this may help you..
 
Share this answer
 
If(exists(select * from table where column=@clounm))
Begin
Selct 'true'
END
ELSE
BEGIN
select 'false'
END
 
Share this answer
 
You can read about Count() function in sql here.http://www.w3schools.com/sql/sql_func_count.asp[^].You write the appropriate query to find the count from your table with value in textbox as condition in "where" of sql.if count is greater than 0,it means record is existing,else you can proceed with insertion as a new value.
 
Share this answer
 
v2
Comments
RaviRanjanKr 18-Jan-11 0:42am    
My vote of 5!
there is nothing wrong in your answer.
I think OP and others haven't notice about what Count() can do.
This http://www.c-sharpcorner.com/forums/ShowMessages.aspx?ThreadID=81120 link has some best answer for same question which is using Count() function.
Anupama Roy 18-Jan-11 0:51am    
Thank You!
Blesson Mathew 18-Jan-11 7:29am    
Your answer is a good one..So I am giving you 5+
Retrieve the text, send it to a stored proc, and let the stored proc search the applicable column for the text.
 
Share this answer
 
Comments
OriginalGriff 17-Jan-11 12:12pm    
The OP Wrote:
"please send me an example boz i'm new to this"
Yusuf 17-Jan-11 14:18pm    
The new reply to comment feature is Cool! Sorry to hijack your comment @OriginalGriff, can't resist the temptations.

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