473,756 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGrid - Edit, Update, Delete

Hello there,

I have DataGrid with some records and I would like to edit it right in
the datagrid using the built in commands.
I can't figure out a way to pass to the SQL query the record ID which
I'm editing.
SELECT:
SelectCommand=" SELECT ProviderID, AddressID, Address, Address2, City,
State, County, Zip FROM ProviderAddress WHERE (ProviderID =
@ProviderID)"
<SelectParamete rs>
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</SelectParameter s>
the above works fine.

DELETE:
DeleteCommand=" DELETE FROM [ProviderAddress] WHERE ([AddressID] =
@AddressID) AND ([ProviderID] = @ProviderID)"
<DeleteParamete rs>
<asp:Paramete r Name="AddressID " Type="Int32" />
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</DeleteParameter s>
Doesn't do anything. Posts the page back to itself but no record is
effected.
When I add DefaultValue="x xx" to the parameter then it works so I know
that the AddressID parameter is incorrect.

UPDATE:
UpdateCommand=" UPDATE [ProviderAddress] SET [Address] = @Address,
[Address2] = @Address2, [City] = @City, [State] = @State, [County] =
@County, [Zip] = @Zip WHERE ([AddressID] = @AddressID AND [ProviderID]
= @ProviderID)">
<UpdateParamete rs>
<asp:Paramete r Name="Address" />
<asp:Paramete r Name="Address2" />
<asp:Paramete r Name="City" />
<asp:Paramete r Name="State" />
<asp:Paramete r Name="County" />
<asp:Paramete r Name="Zip" />
<asp:Paramete r Name="AddressID " />
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</UpdateParameter s>
Doesn't work at all and returns SQL error: Must declare the variable
'@AddressID'.
Again, if I define the default value for AddressID then works fine.

QUESTION: How can I define the @Values (@AddressID) for the edited
record? I have the AddressID value displayed in the DataGrid to I
should be able to access it.

Thanks,
draku

Feb 7 '06 #1
4 9697
On your gridview you need an onrowcommand event handler (ie.
onrowcommand="G ridView1_RowCom mand" for declaritve assignment). This will
fire when a command button (such as "Delete") is clicked. Then on you
"Delete" button you need to specify the commandargument attribute (ie.
commandargument ='<%# Eval("SomeRecId ") %>') and commandname (ie.
commandname="De lete"). This will be the value passed to the eventhandler.
In you code behind add a funcation for the event handler:

protected void GridView1_RowCo mmand(object sender, GridViewCommand EventArgs e)
{
switch (e.CommandName)
{
case "Delete":
this.GridView1. DeleteRow(Conve rt.ToInt32(e.Co mmandArgument)) ;
break;
}
}

That should take care of your Delete row, I usually upate using a FormView,
so I'll have to dig up some code for that (unless someelse posts). It should
be possible to put the Update Routine (or call to it) in the
GridView1_RowCo mmand function and look for a different
commandname/commandargument .

Hope this helps.

-Phil

"dr****@gmail.c om" wrote:
Hello there,

I have DataGrid with some records and I would like to edit it right in
the datagrid using the built in commands.
I can't figure out a way to pass to the SQL query the record ID which
I'm editing.
SELECT:
SelectCommand=" SELECT ProviderID, AddressID, Address, Address2, City,
State, County, Zip FROM ProviderAddress WHERE (ProviderID =
@ProviderID)"
<SelectParamete rs>
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</SelectParameter s>
the above works fine.

DELETE:
DeleteCommand=" DELETE FROM [ProviderAddress] WHERE ([AddressID] =
@AddressID) AND ([ProviderID] = @ProviderID)"
<DeleteParamete rs>
<asp:Paramete r Name="AddressID " Type="Int32" />
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</DeleteParameter s>
Doesn't do anything. Posts the page back to itself but no record is
effected.
When I add DefaultValue="x xx" to the parameter then it works so I know
that the AddressID parameter is incorrect.

UPDATE:
UpdateCommand=" UPDATE [ProviderAddress] SET [Address] = @Address,
[Address2] = @Address2, [City] = @City, [State] = @State, [County] =
@County, [Zip] = @Zip WHERE ([AddressID] = @AddressID AND [ProviderID]
= @ProviderID)">
<UpdateParamete rs>
<asp:Paramete r Name="Address" />
<asp:Paramete r Name="Address2" />
<asp:Paramete r Name="City" />
<asp:Paramete r Name="State" />
<asp:Paramete r Name="County" />
<asp:Paramete r Name="Zip" />
<asp:Paramete r Name="AddressID " />
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</UpdateParameter s>
Doesn't work at all and returns SQL error: Must declare the variable
'@AddressID'.
Again, if I define the default value for AddressID then works fine.

QUESTION: How can I define the @Values (@AddressID) for the edited
record? I have the AddressID value displayed in the DataGrid to I
should be able to access it.

Thanks,
draku

Feb 7 '06 #2
In your GridView you need to specify a couple of attributes on the Delete
button:
commandname="De lete"
commandargument ='<%# Eval("SomeRecId ")%>'

Also you need to declare an event handler for row commands on you GridView:
onrowcommand="G ridView1_RowCom mand"

Then create the handler in your codebehind:
protected void GridView1_RowCo mmand(object sender, GridViewCommand EventArgs e)
{
switch (e.CommandName)
{
case "Delete":
this.GridView1. DeleteRow(Conve rt.ToInt32(e.Co mmandArgument)) ;
break;
}
}

Thus, when a command button is click on your GridView (Delete, Update
etc...) it will fire the GridView1_RowCo mmand routine, passsing in your
commandname and commandargument (holding your recid).

I'll have to dig up some Update code, as I usually use a FormView for my
updates. You should be able to follow the same principle and add an Update
routine and catch it in the RowCommand routine as you do the delete.

Hope this helps.

-Phil

"dr****@gmail.c om" wrote:
Hello there,

I have DataGrid with some records and I would like to edit it right in
the datagrid using the built in commands.
I can't figure out a way to pass to the SQL query the record ID which
I'm editing.
SELECT:
SelectCommand=" SELECT ProviderID, AddressID, Address, Address2, City,
State, County, Zip FROM ProviderAddress WHERE (ProviderID =
@ProviderID)"
<SelectParamete rs>
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</SelectParameter s>
the above works fine.

DELETE:
DeleteCommand=" DELETE FROM [ProviderAddress] WHERE ([AddressID] =
@AddressID) AND ([ProviderID] = @ProviderID)"
<DeleteParamete rs>
<asp:Paramete r Name="AddressID " Type="Int32" />
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</DeleteParameter s>
Doesn't do anything. Posts the page back to itself but no record is
effected.
When I add DefaultValue="x xx" to the parameter then it works so I know
that the AddressID parameter is incorrect.

UPDATE:
UpdateCommand=" UPDATE [ProviderAddress] SET [Address] = @Address,
[Address2] = @Address2, [City] = @City, [State] = @State, [County] =
@County, [Zip] = @Zip WHERE ([AddressID] = @AddressID AND [ProviderID]
= @ProviderID)">
<UpdateParamete rs>
<asp:Paramete r Name="Address" />
<asp:Paramete r Name="Address2" />
<asp:Paramete r Name="City" />
<asp:Paramete r Name="State" />
<asp:Paramete r Name="County" />
<asp:Paramete r Name="Zip" />
<asp:Paramete r Name="AddressID " />
<asp:SessionPar ameter Name="ProviderI D" SessionField="P Id" Type="Int32"
/>
</UpdateParameter s>
Doesn't work at all and returns SQL error: Must declare the variable
'@AddressID'.
Again, if I define the default value for AddressID then works fine.

QUESTION: How can I define the @Values (@AddressID) for the edited
record? I have the AddressID value displayed in the DataGrid to I
should be able to access it.

Thanks,
draku

Feb 7 '06 #3
Thanks for the replay Phil,
I believe that there is an easier method and I don't need to take it
into code behind. I just need to figure out a way to define the @values
for the datagrid parameters.
But if I won't find it soon I'l do it in code behind.

draku

Feb 7 '06 #4
It was a BUG in VS2005 Beta 2 version. My code is perfectly fine.

draku

Feb 9 '06 #5

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

Similar topics

0
2439
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.
0
1656
by: shamila | last post by:
</asp:label><asp:datagrid id="DataGrid3" runat="server" cssclass="DataGrid" showfooter="True" onupdatecommand="DataGrid3_Update"ondeletecommand="DataGrid3_Delete" oneditcommand="DataGrid3_Edit" oncancelcommand="DataGrid3_Cancel" caption="Pricing Schedules" autogeneratecolumns="False" OnItemDataBound="ItemDataBoundEventHandler"> <alternatingitemstyle cssclass="DataGrid_AlternatingItemStyle" /> <columns> <asp:templatecolumn...
1
4338
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls (watch the layout, some have child controls):
0
473
by: Steve | last post by:
I have a datagrid that is created at run time DataGrid dgG = new DataGrid(); BoundColumn bcB; dgG.CellPadding = 5; dgG.CellSpacing = 0; dgG.GridLines = GridLines.Both; dgG.CssClass = "SectionTableLines"; dgG.DataKeyField = "PlanWorkOrderID";
1
3780
by: Harry Devine | last post by:
I have a DataGrid that is configured to use the Edit/Update/Cancel concept correctly. My grid shows values from 5 database fields. I only need to update that last 4 fields. The last field is a Yes/No value in Access. Using the OleDbCommand, if I do not consider the Yes/No field, the ExecuteNonQuery command, using my UPDATE SQL statement, updates the record correctly. However, if I put the Yes/No field into the mix, ExecuteNonQuery...
4
6947
by: rkbnair | last post by:
Is there any way to display an image button instead of the traditional Edit/Update/Cancel push button in a datagrid (with bound data)?
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...
9
2727
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
2
1985
by: Iain | last post by:
Hi All Using Delphi 2006 developer - C# Project I have the following 2 event handlers for the datagrid - see botton of page Both events will fire off correctly but i have a problem collecting the correct data in teh Update event - I have noted the problem in the event handler
1
1593
by: Andrew Wan | last post by:
Is it possible to make some of the DataGrid functionality to client-side? Such as the Edit, Update, Cancel, Delete functionality? Everytime I click on a row's Edit button, I wait 2 seconds before I see the Edit controls (eg. textboxes, drop-down combos, checkboxes). I understand that the DataGrid's Edit, Cancel, Update, Delete functions are server-side because it uses ASP.NET postback handler functions.
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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
9819
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
9689
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
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
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

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.