Click here to Skip to main content
15,352,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
create proc data
@product varchar(50)

as begin
if exists(select product from Tablename where product=@product)
select '1' as status

else
insert into tablename(product) values(@product)
select '2' as status

end


i wrote procedure like......how to write c# code for displaying message "record already exixts"......
Posted
Updated 21-May-15 1:35am
v2
Comments
Arun Kumar_Tech 21-May-15 6:36am
   
use cmd.ExecuteScalar(); to get the output from SQL, use if loop and check if it's 1 "Already Exists" else and so on....
Andy Lanng 21-May-15 7:42am
   
I use Raiserror (yes it is one 'e') in my sql and catch the exception in code. I control my transaction scope in code so this might not be your best option
jaket-cp 21-May-15 7:51am
   
Can you show the c# code you have tried.
Also does the product field have a unique constraint put on it?
Abhipal Singh 21-May-15 7:58am
   
Instead of '1' or '2' you can write your string 'Record already Exist' or 'Record Inserted' in your select statement if you just want to display it on UI.

create proc data
@product varchar(50)

as begin
if exists(select product from Tablename where product=@product)
select 'Record Already Exist' as status

else
insert into tablename(product) values(@product)
select 'Record Inserted' as status

end

However, if you want to use that status in your code then I would suggest, try to define a proper enumeration in C# code and map your SP outputs in the enum.
F-ES Sitecore 21-May-15 8:06am
   
We don't know how you're calling the SP so your question is impossible to answer.
vemsoft 22-May-15 0:59am
   
i am using three architecture and getting thar status in data datble........
please help me how to compare tha status and display message "record already exist..."
F-ES Sitecore 22-May-15 3:33am
   
Are you using ado.net? Entity Framework? LinqToSql? nHibernate? Something else? The answer to your question depends on what you are using to call the SP.

1 solution

You can use an output parameter and set value for the same
   

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