Click here to Skip to main content
16,005,080 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How To Use If exists in WFA

I want when anyone insert the same data
messagebox.show"This Data exist";

the stored Proc. Code is :-



SQL
create proc AddCustomer
@name varchar(50),
@address varchar(50),
@phone varchar(50),
@date datetime
as
begin
if (exists(select cphone from Customer where cphone=@phone))
begin
   print 'This Data exist'
End
else
begin
insert into Customer (cname,cadress,cphone,cdate) 
values (@name,@address,@phone,@date)
end
end
Posted

C#
string something;
if(something != null)
{
//it exists
}
else
{
//not exist
}
 
Share this answer
 
Comments
Mostafa Metwally 24-Feb-13 13:54pm    
No this solution for textbox when it's null

But I want not to insert data for the same Id into database
Hasham Ahmad 24-Feb-13 20:33pm    
then you need to return specific values from the procedure, then check for those values and display msg accordingly
Create your stored procedure as so (but format the indentation as you'd like):
Create Procedure AddCustomer
	@name varchar(50),
	@address varchar(50),
	@phone varchar(50),
	@date datetime
as
begin
  Set NoCount On
	Declare 
			@CustExists 				Int

	Set @CustExists = 0 -- False
	if (exists(select cphone from Customer where cphone=@phone))
	  Set @CustExists = 1
		
	if @CustExists = 0 
		insert into Customer (cname,cadress,cphone,cdate) 
		values (@name,@address,@phone,@date)
		
	return(@CustExists)
end


Then you can execute the proc from your C# code, examining the return code.
Alternatively, you could also use an output parameter instead of the return code, and check that after calling the proc.

Does that work for you?

Hope that helps.

Richard
 
Share this answer
 
I think it is better to check the condition from the front-end. Stop the user there itself.

Use Dataset

Count rows, if it is > 0 then give your Message as "Data Already exists."
Else Write your Insert query OR CAll Procedure for Inserting Data.
 
Share this answer
 
You can achieve this using output parameter in Stored procedures...

Example below..
SQL
create proc AddCustomer
@name varchar(50),
@address varchar(50),
@phone varchar(50),
@date datetime,
@output varchar(50) output // output variable
as
begin
if (exists(select cphone from Customer where cphone=@phone))
begin
   /*print 'This Data exist'*/
   set @output='This Data exist'
End
else
begin
insert into Customer (cname,cadress,cphone,cdate) 
values (@name,@address,@phone,@date)
end
end

c# code below..
C#
        // input parameters...

        SqlParameter message = new SqlParameter("@output",SqlDbType.VarChar,50);// output parameter
        message.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(message);
        cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>alert('" +message.value.ToString() +"');</script>", false);

Hope it helps..
 
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