473,789 Members | 3,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mok
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].[NewUpdateComman d]
(
@Description varchar(1000),
@Param2 datetime,
@Original_ID int,
@Original_Descr iption 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_Descr iption OR @Original_Descr iption 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).

Nov 19 '05 #1
1 2777
We have the same problem..(Assum ing ASP.NET 2.0)
The trick is to write on a specific sproc for the grid you are updating
from.
Make sure the fields you don't want to update is set as readonly.
Don't include the "key field in the grid columns, but make sure it is in the
datakeys of the grid.
Change the OldValuesParatm erFormat to "{0}" the default is "Original_{ 0}"

CREATE PROCEDURE [mok].[NewUpdateComman d]
(
@Description varchar(1000),
@Param2 datetime,
@ID int
)

Grid should ONLY have Description and Param2 as columns and paramaters. (Do
not specify ID as paramater, it is in the datakeys)
ASP.NET 2.0 add the Datakey value automatically as a extra paramater.

Use SQL Profiler and you will find
exec NewUpdateComman d @Description='s dsdf', @Param2 = '2005/09/04', @ID='3'
when you click update on the row you are editing...

BTW. SQL profiler is mans best friend (after dog) to see what really happens
when you do grid editing....

HTH

Gerhard

PS. Stay away from Generated SQL code from the designer... it is not
optimized.....u se it only to see how it works once... after that... write
your own sprocs...

"Mok" <ma********@gma il.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
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].[NewUpdateComman d]
(
@Description varchar(1000),
@Param2 datetime,
@Original_ID int,
@Original_Descr iption 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_Descr iption OR @Original_Descr iption 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).

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2440
by: lu | last post by:
I have a page with datagrid (for viewing, delete,edit,update) and text fields with button to add info to database. When user comes to page the datagrid selects info from database, binds in columns, and adds a delete buttoncolumn and editcommandcolumn. Below that, I have several text boxes with a button with an onclick event to add new information to the database table the grid pulls from. A)Select Info binds fine.
7
2257
by: TT (Tom Tempelaere) | last post by:
Hi there, I'm struggling to get the right event. I want to be notified of a change in a cell in a DataGrid made by a user. I need following information about the change: * the row index * the column index * the newly entered value I need this information to update another DataGrid. I have checked the
3
2221
by: D. Shane Fowlkes | last post by:
I have a Datagrid which in theory, should allow you to edit and update the records. I've stripped my test page down so that it's only attempting to update one field - "description". Yet when I make the changes and click Update, it reverts back to the old value. I'm stumped! I've also checked the db table and it is infact not updating. I'm attempting to follow and example right out of ASP.NET Unleashed but not having much luck. Any...
1
1726
by: BW | last post by:
Hello, I am attempting to to take edited values from a datagrid control and update the underlying database. Problem is that some of the edited values don quite seem to make it to my update sub. My code is shown below. The values that are not appearing are Cell(4) - sCompletion and Cell(5) - sNote. The other values show up just fine. What am I missing here?
1
2024
by: mursyidatun ismail | last post by:
Dear all, database use: Ms Access. platform: .Net i'm trying to update a record/records in a table called t_doctors by clicking da edit link provided in the database. when i ran through da browsers and click update it gave me this error: Specified argument was out of the range of valid values. Parameter name:
0
1756
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid for editing and deleting data. When a user clicks on the "Edit" button in the datagrid, Edit() method is called and the appropriate row is changed into textboxes. And if user clicks on the "Update" button, the Update() method is fired. But if...
0
2913
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string strDataValueField; string strDataTextField; public CreateEditItemTemplateDDL(string DDLName,string DataValueField,string
13
2460
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever works best) to display a dropdown menu of fields populated from table tblInvoiceData. This control also includes a textbox which the user can input a value. These two columns are side by side and not in a vertical layout. The user then clicks on...
6
2946
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1) Constraint pk_table1 Primary Key,
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.