Using a datagrid to update values in sql server (best practice)

M

marc.cueto

Hello,
I want to pull information from a table in sql server, bind it to a
grid and update those values. I would like to know the best way to do
it. I used the configure data adapter wizard and it came up with a
crazy stored proc. As in this example I would only update [start date]
and [description]:

CREATE PROCEDURE [mok].[NewUpdateCommand]
(
@Description varchar(1000),
@Param2 datetime,
@Original_ID int,
@Original_Description varchar(1000),
@Original_Start_Date datetime,
@ID int
)
AS
SET NOCOUNT OFF;
UPDATE dbo.tblTasks SET Description = @Description, [Start Date] =
@Param2 WHERE (ID = @Original_ID) AND (Description =
@Original_Description OR @Original_Description IS NULL AND Description
IS NULL) AND ([Start Date] = @Original_Start_Date OR
@Original_Start_Date IS NULL AND [Start Date] IS NULL);
SELECT ID, Description, [Start Date] FROM dbo.tblTasks WHERE (ID =
@ID);
GO

Basically all this query does is receive start date and description. If
one of the values wasn't modified in the datagrid, then this query
won't modify that field. I'm thinking that since this query came out of
a generator, it may not be the most efficient. For example, always
sending the old values as parameters..

My question is, what is the best way to code the stored proc and the
application level code keeping in mind that I only want to call 1
update stored proc? From examining the datagrid tutorials where updates
are performed, they usually assume all the fields in the grid will be
updated (which is not always the case).
 
M

Mok

Thanks for your reply. I sort of guessed not to use the code generator
and am now using my own stored procs. So far I have this one:
CREATE PROCEDURE [dbo].[spUpdateTask]
@UserID int,
@Completed bit = null,
@Description varchar(1000) = null,
@Title varchar(100),
@StartDate datetime = null,
@DateCompleted datetime = null,
@DueDate datetime = null,
@ID int



AS


UPDATE tblTasks
SET UserID =@UserID, Completed =@Completed, [Description]
=@Description, [Due Date] =@DueDate,
[Date Completed] =@DateCompleted, [Start Date] =@StartDate, Title
=@Title

where ID = @ID
GO

How do I fix this stored proc so that if the parameter is not sent the
stored proc will exclude it from this update statement?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top