Update Command not working with Gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ihaveaquery
    New Member
    • Feb 2007
    • 6

    Update Command not working with Gridview

    Hi,
    Delete is working fine for me, but Update- Nothing is happening. Please find the code snippet below and help me out...
    Code:
    <asp:GridView ID="empGrid" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                DataSourceID="empTestDataSource" >
                <Columns>
                    <asp:ButtonField    CommandName="Select"    ButtonType=Link         DataTextField="empId" />              
                    <asp:BoundField     DataField="empName"     HeaderText="empName"    SortExpression="empName" />
                    <asp:BoundField     DataField="empAge"      HeaderText="empAge"     SortExpression="empAge" />
                    <asp:CommandField   ShowEditButton=true     ButtonType=Button />
                    <asp:CommandField   ShowDeleteButton=true   ButtonType=Button />
                </Columns>
            </asp:GridView>
            <asp:AccessDataSource ID="empTestDataSource" runat="server" DataFile="~/Database/Employee.mdb"
                SelectCommand="SELECT [empName], [empAge], [empId] FROM [emp]"
                UpdateCommand="UPDATE emp SET empName=@empName WHERE empId=@empId"
                DeleteCommand="DELETE FROM emp WHERE empId=@empId" OldValuesParameterFormatString="{0}">
                <UpdateParameters>
                    <asp:Parameter Name="empName" Type=string />
                    <asp:Parameter Name="empId" Type=Int32 />
                </UpdateParameters>
            </asp:AccessDataSource>
    Any help would be appreciated....
    thnks in advance...
    Last edited by kenobewan; Mar 1 '07, 12:32 AM. Reason: Add code tags
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    How are you executing the update statement?

    Comment

    • ihaveaquery
      New Member
      • Feb 2007
      • 6

      #3
      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...

      Comment

      • ihaveaquery
        New Member
        • Feb 2007
        • 6

        #4
        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 !!!

        Code:
        <table width="500px"><tr><td>
        <div style="margin:5px; margin-top:5px">
        	<div class="smallfont" style="margin-bottom:2px">Code:</div>
        	<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;
                    DataSourceID=&quot;empTestDataSource&quot; &gt;
                    &lt;Columns&gt;
                        &lt;asp:ButtonField    CommandName=&quot;Select&quot;    ButtonType=Link         DataTextField=&quot;empId&quot; /&gt;              
                        &lt;asp:BoundField     DataField=&quot;empName&quot;     HeaderText=&quot;empName&quot;    SortExpression=&quot;empName&quot; /&gt;
                        &lt;asp:BoundField     DataField=&quot;empAge&quot;      HeaderText=&quot;empAge&quot;     SortExpression=&quot;empAge&quot; /&gt;
                        &lt;asp:CommandField   ShowEditButton=true     ButtonType=Button /&gt;
                        &lt;asp:CommandField   ShowDeleteButton=true   ButtonType=Button /&gt;
                    &lt;/Columns&gt;
                &lt;/asp:GridView&gt;
                &lt;asp:AccessDataSource ID=&quot;empTestDataSource&quot; runat=&quot;server&quot; DataFile=&quot;~/Database/Employee.mdb&quot;
                    SelectCommand=&quot;SELECT [empName], [empAge], [empId] FROM [emp]&quot;
                    UpdateCommand=&quot;UPDATE emp SET empName=@empName WHERE empId=@empId&quot;
                    DeleteCommand=&quot;DELETE FROM emp WHERE empId=@empId&quot; OldValuesParameterFormatString=&quot;{0}&quot;&gt;
                    &lt;UpdateParameters&gt;
                        &lt;asp:Parameter Name=&quot;empName&quot; Type=string /&gt;
                        &lt;asp:Parameter Name=&quot;empId&quot; Type=Int32 /&gt;
                    &lt;/UpdateParameters&gt;
                &lt;/asp:AccessDataSource&gt;</div></pre>
        </div>
        </td></tr></table><br />

        Comment

        • kenobewan
          Recognized Expert Specialist
          • Dec 2006
          • 4871

          #5
          Are you refreshing the dataqsource? Otherwise gridview is going ti display same values.

          Comment

          • ihaveaquery
            New Member
            • Feb 2007
            • 6

            #6
            Originally posted by kenobewan
            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...

            Comment

            • kenobewan
              Recognized Expert Specialist
              • Dec 2006
              • 4871

              #7
              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

              Comment

              • marloutor
                New Member
                • Mar 2007
                • 1

                #8
                I had the same problem. I found that in Visual Web Developer I had to specify the "DataKeyNam es" 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 "DataKeyNam es" box.

                Hope that helps,
                marloutor

                Comment

                • calico
                  New Member
                  • Apr 2007
                  • 1

                  #9
                  Thanks! This was very helpful.

                  Originally posted by marloutor
                  I had the same problem. I found that in Visual Web Developer I had to specify the "DataKeyNam es" 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 "DataKeyNam es" box.

                  Hope that helps,
                  marloutor

                  Comment

                  • MrAMagoo
                    New Member
                    • Sep 2014
                    • 1

                    #10
                    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.

                    Code:
                            Protected Sub MyGridView_RowUpdating _
                            (sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
                            Handles MyGridView.RowUpdating
                    
                            ' Inspect the parameters being sent to the database for an ASP NET GridView UPDATE.
                    
                            Dim I As Integer
                    
                            I = 0
                    
                            For Each MVO As System.Collections.DictionaryEntry In e.OldValues
                                If MVO.Value Is DBNull.Value OrElse MVO.Value Is Nothing Then
                                    Debug.Print(I.ToString + ": " + MVO.Key + " Value:  ")
                                Else
                                    Debug.Print(I.ToString + ": " + MVO.Key + " Value:  " + MVO.Value.ToString)
                                End If
                    
                                I += 1
                            Next MVO
                    
                            I = 0
                            For Each MVN As System.Collections.DictionaryEntry In e.NewValues
                                If MVN.Value Is DBNull.Value OrElse MVN.Value Is Nothing Then
                                    Debug.Print(I.ToString + ": " + MVN.Key + " Value:  ")
                                Else
                                    Debug.Print(I.ToString + ": " + MVN.Key + " Value:  " + MVN.Value.ToString)
                                End If
                                I += 1
                            Next MVN
                    
                    
                        End Sub

                    Comment

                    Working...