Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
IF (@Mode='Insert')
Begin
If NOT Exists (Select ProductName From dbo.tblProduct WHERE ProductName = @ProductName) 
Insert into tblProduct
(
ProductName ,
ProductCode )
Values
(

@ProductName ,
@ProductCode )

this is my insert code now using C# i want to tackle the out put i mean that code is inserted or not i.e., i want to tackle these three thing
1. code insert succefully
2. Product name already exsit
3. sql exception error occured

while my C# code is
C#
int output= objProduct.Insert()

what will output variable will return in according to above three condition
Posted

Make query like this see data inserted or not by message
SQL
If NOT Exists (Select ProductName From dbo.tblProduct WHERE ProductName = @ProductName)
 begin Insert into tblProduct
( ProductName , ProductCode ) Values ( @ProductName , @ProductCode )
Select 'Data Inserted Successfully' end
else
Select 'Data already exists'
 
Share this answer
 
v2
For 1 and 2:

in C#

C#
int rowCount = cmd.ExecuteNonQuery()
if rowCount >0 
{
Record is inserted
}
else
{
	Record already exist.
}


For 3:


SQL
Declare @Error VARCHAR(1000) output
Begin Try
Begin Transaction
IF (@Mode='Insert')
Begin
If NOT Exists (Select ProductName From dbo.tblProduct WHERE ProductName = @ProductName) 
Insert into tblProduct
(
ProductName ,
ProductCode )
Values
(
 
@ProductName ,
@ProductCode )
Commit

End Try

Begin Catch
SET @Error=(SELECT ERROR_MESSAGE())
ROLLBACK TRANSACTION
RAISERROR(@Error,16,1)
End Catch
 
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