Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, when i am inserting a data through text box in database. i want to check if data is already exist in the table just shows a message "Data is already exist".
In table there is only two columns id and documentname.
I am using sql server and vs 2005.
Please give me a querry for that.


Thanks in Advance
Posted

C#
SELECT * FROM TABLENAME WHERE FIELDNAME = '" + YOURSEARCHSTRING + "';

Get the data from this query to datatable and

C#
if(MYDATATABLE.Rows.Count > 0)
{
    Messagebox.Show("Record Already Exists");
}else
{
    //Fire your insert query
}


Hope you will get an idea through this.
 
Share this answer
 
Hai fried,
1.The best method is create a unique key in database, and track the error code for displaying error.
2.Another method


SQL
IF EXISTS(SELECT   * FROM  tablename WHERE condition )
	BEGIN
		RAISERROR ('already entered',11,1)
	END
	ELSE
	BEGIN
	--		Insert query
	END
 
Share this answer
 
Comments
mayankshrivastava 25-Feb-12 3:36am    
thanks fro reply Adersh
your querry works fine in sql query analyzer.
problem is that how to write in asp.net.
what is use executenonquerry or something else.
nd how to use it.
how to use return value?
i am new in asp.net
Adersh Ram 25-Feb-12 3:48am    
The best method is create a stored procedure for insert the data,and put the condition above, there is row exists in database it return the error message, else it is insert into the database.use normal executenonquery method
if any problem reply me

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