Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a form with one SAVE button and I already used it for a button_click event inside which I am using a stored procedure to add data to a database table.

Now I want to go to edit data in the form and click that same SAVE button. This should run the edit related code not the add related code.

How do I write two stored procedure functions for the save button_click Event?

Please reply with an example.
Posted
Updated 17-Jan-11 23:53pm
v2
Comments
Slacker007 18-Jan-11 5:53am    
Edited for spelling, grammar, and readability.

Edit and Add are very similar, they both need to Save data to the server. The major difference between the two is that for Add you won't have a primary key for your data yet.

So you could have a single stored procedure called 'save' that you call on either edit or add, but the operation the procedure takes depends on whether or not you pass a primary key to it.

SQL
CREATE PROCEDURE  myarea_SaveData

	(
		@PrimaryKey			INT, 
		@FirstName			VARCHAR(50), 
		@LastName			VARCHAR(50)
	)

AS

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON

/*
__________________________________________________________________________________________
Author:			Dylan Morley 
Description:		Either adds a new record or updates an existing one

__________________________________________________________________________________________
*/

IF EXISTS(SELECT FirstName FROM MyTable (NOLOCK) WHERE PrimaryKeyID = PrimaryKey)
BEGIN
	UPDATE
		MyTable
	SET
		FirstName = @FirstName, 
		LastName = @LastName, 
	WHERE
		PrimaryKeyID = @PrimaryKey
END
ELSE
BEGIN
	INSERT INTO
		MyTable (PrimaryKey, FirstName, LastName)
	VALUES		
		(@PrimaryKey, @FirstName, @LastName)

END


GO
 
Share this answer
 
v3
Comments
Аslam Iqbal 18-Jan-11 6:41am    
Is this the schema that ravi has? But it is nice. It may solve his problem. SO my 5
Dylan Morley 18-Jan-11 7:11am    
Just an example, obviously he would need to change it to reflect his database. But it shows how you can wrap up ADD \ EDIT into one stored procedure and control the behaviour using the primary key.

This means all your client code has to do is present the data & call is single stored procedure. No need to keep track of boolean flags or strings, just the primary key (which you'd maybe be displaying anyway
MCY 18-Jan-11 7:48am    
yep, a most commonly used way
ks ravi 18-Jan-11 7:48am    
hi Dylan Morley,thank you for reply. I got one thing that Editing ie Updating BUT Inserting is not happing. i dont insert below statement,reply me if it needs

IF (@@ERROR != 0)
BEGIN
RAISERROR('Failed to save the data!', 18, 18) WITH SETERROR
RETURN @@ERROR
END
Dylan Morley 18-Jan-11 7:54am    
No, you don't really need that in this case - it's just part of some template I have.

Use your debugger, before you call the stored procedure, check the values you're passing to it. All look OK?

The logic in the stored procedure is so simple there's not much that can go wrong. It'll do one of 3 things - add if you don't supply a primary key, edit it you provide one, or error of some sort on either case.

Check the value of the 'PrimaryKey' you're passing since this is the important controlling variable.
Ok I misunderstood the question and let me modify my answer accordingly.

for accomplishing you can keep one global variable.

Like

String Operation = "Insert";


And accordingly in button click event you can modify accordingly like

Public void Button_click(object sender,EventArgs e)
{
if(Operation == "Insert")
{
InsertData();
Operation = "Edit";
}
else
{
EditData();
Operation="Insert";
}
}

InsertData()
{
//Perform insert stored procedure here
}
EditData()
{
//Perform edit stored procedure here
}
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 19-Jan-11 16:04pm    
Moved from OP's answer:
hi Hiren Solanki,

thank you for reply,what ur saying is right But if i call same event (Button_click)to two operations,both Functions will operate,Right?. But i want like First Function to one operation AND Second to another operation with same event. reply me with an example
Manfred Rudolf Bihy 19-Jan-11 16:07pm    
I'll spread a little sunshine while I'm cleaning up this thread! 5+
You can have a boolean flag, or some logic to decide what to do.

For example,
You can have a label which holds ID of the record. If it is blank, it is a new record. Otherwise it is an update operation.
VB
If Label1.Text = "" Then
  doAdd()
Else
  doEdit()
End If
 
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