Click here to Skip to main content
16,008,954 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,


I've a text box in windows form and its name is txtbx and database table name is table1 in which 3 columns and 1 column name is column1.My question is how to compare textbox value with column1 existed value,if same should say 'Found' else 'Not Found'. please reply with an example

MOVED FROM ANSWER:

hi thank u for reply, i got confused at below code
ReadData r = new ReadData();
if (r.FindString("Shahingg"))//? (my question is below)
  MessageBox.Show("I Found it!");
else 
  MessageBox.Show("I can't Find it!");



? is since i using textbox,how to include textbox value instead "Shahingg".
Posted
Updated 17-Jan-11 18:35pm
v3
Comments
Baji Jabbar 18-Jan-11 0:34am    
First I have to say no effort!
You can read the values of the column using 'datareader' and compare it with the textbox value.
JF2015 18-Jan-11 0:36am    
I moved you answer to the question and updated my answer to solve your code issue.
Baji Jabbar 18-Jan-11 0:38am    
texbx.Text will give you the value

Hi,

you already got some solutions here:
How to check if value exists in database, [^]
Have you tried some of those? If yes, show us your code! If you don't tell us what problems you face we can't help you any further.

Edited after updated question:

Here is the solution:
C#
ReadData r = new ReadData();
if (r.FindString(textBox1.Text))
  MessageBox.Show("I Found it!");
else
  MessageBox.Show("I can't Find it!");
 
Share this answer
 
v2
Comments
Anupama Roy 18-Jan-11 0:31am    
Exactly!My Vote of 5! I had clearly mentioned in the post that he has to put value of textbox in where condition of sql in order to compare.He downvoted my answer!
ks ravi 18-Jan-11 1:50am    
thank you its working
Espen Harlinn 18-Jan-11 8:53am    
5+ Well answered
Hi ks ravi.

You will need to get a couple of things in order.

You need:
a) a connection
b) a way to query the database
c) a way to encapsulate and evaluate the results

First, you need to make the application you're developing aware of the database. The configuration is expressed as a connection string, which is often constructed and stored for you through the VS tools.

The easiest way to do what you're looking for might be to use LINQ to SQL. This is very well documented. The steps are along the lines of:
1) Use the server window to add a data connection to the database in question
2) Add a LINQ to SQL file to the database
3) Drag a table from the server explorer onto the LINQ to SQL designer
4) Compile your application

At this point, your application is fully aware of the database and the connection bits are constructed and ready to use. The configuration is stored in your app.config file. The result of the designer is a DataContext and its related classes.

Now, in your code, you're going to need to do the following:
1) Create an instance of the datacontext
2) "Ask" if your table contains the data

It might look something like this:

DesignerNameDataContext dc = new DesignerNameDataContext();
var matchingRecord = dc.table1.Where(c => c.column1 == txtbx.Text).FirstOrDefault();
if(matchingRecord == null)
{
  // there was no match
}


I've given you a bunch of tools and vocabulary here. A Google search will get you further along the way. If you have any specific questions or need help with a particular concept, please post another question!

Hope this helps.

Cheers.
 
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