472,110 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Update Command not working with Gridview

ihaveaquery
Hi,
Delete is working fine for me, but Update- Nothing is happening. Please find the code snippet below and help me out...
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="empGrid" runat="server" AllowSorting="True" AutoGenerateColumns="False"
  2.             DataSourceID="empTestDataSource" >
  3.             <Columns>
  4.                 <asp:ButtonField    CommandName="Select"    ButtonType=Link         DataTextField="empId" />              
  5.                 <asp:BoundField     DataField="empName"     HeaderText="empName"    SortExpression="empName" />
  6.                 <asp:BoundField     DataField="empAge"      HeaderText="empAge"     SortExpression="empAge" />
  7.                 <asp:CommandField   ShowEditButton=true     ButtonType=Button />
  8.                 <asp:CommandField   ShowDeleteButton=true   ButtonType=Button />
  9.             </Columns>
  10.         </asp:GridView>
  11.         <asp:AccessDataSource ID="empTestDataSource" runat="server" DataFile="~/Database/Employee.mdb"
  12.             SelectCommand="SELECT [empName], [empAge], [empId] FROM [emp]"
  13.             UpdateCommand="UPDATE emp SET empName=@empName WHERE empId=@empId"
  14.             DeleteCommand="DELETE FROM emp WHERE empId=@empId" OldValuesParameterFormatString="{0}">
  15.             <UpdateParameters>
  16.                 <asp:Parameter Name="empName" Type=string />
  17.                 <asp:Parameter Name="empId" Type=Int32 />
  18.             </UpdateParameters>
  19.         </asp:AccessDataSource>
Any help would be appreciated....
thnks in advance...
Feb 28 '07 #1
9 25442
kenobewan
4,871 Expert 4TB
How are you executing the update statement?
Mar 1 '07 #2
On clicking the delete button, i get the row deleted from the DB. But, Update is not working as it is suppsoed to work. I am not getting any error, the page returns with the original values.
Am i missing something in the code? Pls help...
Mar 1 '07 #3
Somebody pls help....
I am using a simple Gridview to display the emp id (as a link for selecting), emp name, emp Age. There are edit and delete buttons in each row. Using the delete button, the record can be deleted. But the Edit-Update/Cancel functinality is not working. I am not getting any errors. The old values are not updated with the new values. This is the scenario. The code is once more put for reference...PLS HELP !!!

Expand|Select|Wrap|Line Numbers
  1. <table width="500px"><tr><td>
  2. <div style="margin:5px; margin-top:5px">
  3.     <div class="smallfont" style="margin-bottom:2px">Code:</div>
  4.     <pre class="alt2 smallcode" style="margin:0px; padding:6px; border:1px inset; width: 500px; overflow:scroll"><div dir="ltr" style="text-align:left;">&lt;asp:GridView ID=&quot;empGrid&quot; runat=&quot;server&quot; AllowSorting=&quot;True&quot; AutoGenerateColumns=&quot;False&quot;
  5.             DataSourceID=&quot;empTestDataSource&quot; &gt;
  6.             &lt;Columns&gt;
  7.                 &lt;asp:ButtonField    CommandName=&quot;Select&quot;    ButtonType=Link         DataTextField=&quot;empId&quot; /&gt;              
  8.                 &lt;asp:BoundField     DataField=&quot;empName&quot;     HeaderText=&quot;empName&quot;    SortExpression=&quot;empName&quot; /&gt;
  9.                 &lt;asp:BoundField     DataField=&quot;empAge&quot;      HeaderText=&quot;empAge&quot;     SortExpression=&quot;empAge&quot; /&gt;
  10.                 &lt;asp:CommandField   ShowEditButton=true     ButtonType=Button /&gt;
  11.                 &lt;asp:CommandField   ShowDeleteButton=true   ButtonType=Button /&gt;
  12.             &lt;/Columns&gt;
  13.         &lt;/asp:GridView&gt;
  14.         &lt;asp:AccessDataSource ID=&quot;empTestDataSource&quot; runat=&quot;server&quot; DataFile=&quot;~/Database/Employee.mdb&quot;
  15.             SelectCommand=&quot;SELECT [empName], [empAge], [empId] FROM [emp]&quot;
  16.             UpdateCommand=&quot;UPDATE emp SET empName=@empName WHERE empId=@empId&quot;
  17.             DeleteCommand=&quot;DELETE FROM emp WHERE empId=@empId&quot; OldValuesParameterFormatString=&quot;{0}&quot;&gt;
  18.             &lt;UpdateParameters&gt;
  19.                 &lt;asp:Parameter Name=&quot;empName&quot; Type=string /&gt;
  20.                 &lt;asp:Parameter Name=&quot;empId&quot; Type=Int32 /&gt;
  21.             &lt;/UpdateParameters&gt;
  22.         &lt;/asp:AccessDataSource&gt;</div></pre>
  23. </div>
  24. </td></tr></table><br />
Mar 2 '07 #4
kenobewan
4,871 Expert 4TB
Are you refreshing the dataqsource? Otherwise gridview is going ti display same values.
Mar 3 '07 #5
Are you refreshing the dataqsource? Otherwise gridview is going ti display same values.
But the value is not getting updated in the database even.
And how do we refresh a datasource? Pls help...
Mar 6 '07 #6
kenobewan
4,871 Expert 4TB
I was thinking that if your gridview is rebound then the updated data would be displayed. You indicate that it is the former so that your update command isn't executing. Here is an article that may help:
ASP.NET GridView - Add a new record
Mar 7 '07 #7
I had the same problem. I found that in Visual Web Developer I had to specify the "DataKeyNames" property of the GridView as the primary key for my database table.

So the primary key for my database table was "id". So I clicked on the GridView and in the Properties tab, I typed "id" (no quotes) in the "DataKeyNames" box.

Hope that helps,
marloutor
Mar 22 '07 #8
calico
1
Thanks! This was very helpful.

I had the same problem. I found that in Visual Web Developer I had to specify the "DataKeyNames" property of the GridView as the primary key for my database table.

So the primary key for my database table was "id". So I clicked on the GridView and in the Properties tab, I typed "id" (no quotes) in the "DataKeyNames" box.

Hope that helps,
marloutor
Apr 9 '07 #9
I also had a problem with a GridView that was not updating. Inspection of the e.Newvalues Dictionary in the GridView's RowUpdating event showed that the old values for the record were being sent to the database UPDATE query. DataKeyNames was not my problem; I had it set correctly. The WHERE clause of my SELECT query referenced a control parameter against a TextBox on my form. I had inadvertently set EnableViewState for this textbox to "False". Because of this, the GridView was rebinding itself before the UPDATE occurred. Setting EnableViewState on the TextBox to "True" fixed the problem.

Expand|Select|Wrap|Line Numbers
  1.         Protected Sub MyGridView_RowUpdating _
  2.         (sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
  3.         Handles MyGridView.RowUpdating
  4.  
  5.         ' Inspect the parameters being sent to the database for an ASP NET GridView UPDATE.
  6.  
  7.         Dim I As Integer
  8.  
  9.         I = 0
  10.  
  11.         For Each MVO As System.Collections.DictionaryEntry In e.OldValues
  12.             If MVO.Value Is DBNull.Value OrElse MVO.Value Is Nothing Then
  13.                 Debug.Print(I.ToString + ": " + MVO.Key + " Value:  ")
  14.             Else
  15.                 Debug.Print(I.ToString + ": " + MVO.Key + " Value:  " + MVO.Value.ToString)
  16.             End If
  17.  
  18.             I += 1
  19.         Next MVO
  20.  
  21.         I = 0
  22.         For Each MVN As System.Collections.DictionaryEntry In e.NewValues
  23.             If MVN.Value Is DBNull.Value OrElse MVN.Value Is Nothing Then
  24.                 Debug.Print(I.ToString + ": " + MVN.Key + " Value:  ")
  25.             Else
  26.                 Debug.Print(I.ToString + ": " + MVN.Key + " Value:  " + MVN.Value.ToString)
  27.             End If
  28.             I += 1
  29.         Next MVN
  30.  
  31.  
  32.     End Sub
  33.  
Sep 4 '14 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by NETDeveloper | last post: by
reply views Thread by Nick | last post: by
reply views Thread by =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post: by
1 post views Thread by Selvakumar | last post: by
reply views Thread by Bobby Edward | last post: by
reply views Thread by =?Utf-8?B?ZWdzZGFy?= | last post: by
reply views Thread by leo001 | last post: by

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.