update gridview issue

Last post 02-03-2008 12:41 PM by newbieAl. 7 replies.

Sort Posts:

  • update gridview issue

    02-02-2008, 11:06 AM
    • Member
      46 point Member
    • newbieAl
    • Member since 10-18-2007, 7:43 PM
    • Posts 303

      When I hard code the values into the updatecommand then the gridview gets updated, but if I just have the parameters the gridview does not get updated.  What could the issue be?

    This updates the grid:
    UpdateCommand="Update atable set [afield]='test', [bfield]='test' where [id]=@id"
    <UpdateParameters>
                 <asp:Parameter Name="afield" Type="string" />
                 <asp:Parameter Name="bfield" Type="string" />
    <asp:Parameter Name="id" Type="Int32"
                 </UpdateParameters>

    This doesn't:
    UpdateCommand="Update atable set [afield]=@afield, [bfield]=@bfield where [id]=@id"

    <UpdateParameters>
                 <asp:Parameter Name="afield" Type="string" />
                 <asp:Parameter Name="bfield" Type="string" />
    <asp:Parameter Name="id" Type="Int32"
                 </UpdateParameters>

  • Re: update gridview issue

    02-02-2008, 11:22 AM
    • All-Star
      22,266 point All-Star
    • vik20000in
    • Member since 12-30-2005, 6:02 AM
    • Kolkata
    • Posts 3,464
    • TrustedFriends-MVPs

    Instead of this  

    newbieAl:
    <UpdateParameters>
                 <asp:Parameter Name="afield" Type="string" />
                 <asp:Parameter Name="bfield" Type="string" />
    <asp:Parameter Name="id" Type="Int32"
                 </UpdateParameters>
     

    Try

    <UpdateParameters>
                 <asp:Parameter Name="@afield" Type="string" />
                 <asp:Parameter Name="@bfield" Type="string" />
    <asp:Parameter Name="@id" Type="Int32"
                 </UpdateParameters>

    Vikram
    www.vikramlakhotia.com


    Please mark the answer if it helped you
  • Re: update gridview issue

    02-02-2008, 8:27 PM
    • Member
      46 point Member
    • newbieAl
    • Member since 10-18-2007, 7:43 PM
    • Posts 303

     good idea...but that didn't work either.

  • Re: update gridview issue

    02-02-2008, 10:38 PM
    • All-Star
      86,524 point All-Star
    • limno
    • Member since 06-10-2005, 7:50 PM
    • Iowa, USA
    • Posts 4,924
    • Moderator
      TrustedFriends-MVPs

    Your GridView code please.

    Limno

    Format your SQL query with instant sql formatter:
    http://www.dpriver.com/pp/sqlformat.htm
  • Re: update gridview issue

    02-03-2008, 9:31 AM
    • Member
      46 point Member
    • newbieAl
    • Member since 10-18-2007, 7:43 PM
    • Posts 303

    Here is my Gridview code.  In addition to gridview I have 2 dropdownlists and the filtering logic is on the page_load event one the code behind (VB).
     

     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
               
    AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None"
               
    BorderWidth="1px" CellPadding="3" DataKeyNames="ID" DataSourceID="DATA"
               
    GridLines="Vertical"
           
    Font-Names="Verdana"
           
    Font-Size="8pt"
           
    HeaderStyle-BackColor="#aaaadd" >
           
    <PagerSettings Position="TopAndBottom"
             
    FirstPageText="First Page"
             
    LastPageText="Last Page"
             
    Mode="NumericFirstLast" >
         
    </PagerSettings>
     
               
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
               
    <Columns>
                   
    <asp:HyperLinkField DataNavigateUrlFields="ANOTHER_ID" DataNavigateUrlFormatString="test2.aspx?ANOTHER_ID={0}"
                       
    Text="View Record" />
                   
    <asp:CommandField ShowEditButton="True" />
                   
    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                       
    ReadOnly="True" SortExpression="ID" />
                   
    <asp:BoundField DataField="AFIELD" HeaderText="AFIELD" SortExpression="AFIELD" />
                   
    <asp:BoundField DataField="BFIELD" HeaderText="BFIELD" SortExpression="BFIELD" />
                   
    <asp:BoundField DataField="CFIELD" HeaderText="CFIELD" SortExpression="CFIELD" ReadOnly="True" />
                   
    <asp:BoundField DataField="DATE" HeaderText="DATE" SortExpression="DATE" ReadOnly="True" />                
               
    </Columns>
               
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
               
    <EmptyDataTemplate>
                    No Records Found
               
    </EmptyDataTemplate>
               
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
               
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
               
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
               
    <AlternatingRowStyle BackColor="Gainsboro" />
           
    </asp:GridView>
  • Re: update gridview issue

    02-03-2008, 12:07 PM
    • Member
      46 point Member
    • newbieAl
    • Member since 10-18-2007, 7:43 PM
    • Posts 303

    I have this logic in the page_load:

     

    If Not Page.IsPostBack Then

                GridView1.DataBind()

            End If

            If DropDownList1.SelectedIndex.Equals(0) And DropDownList2.SelectedIndex.Equals(0) Then

                DATA.SelectCommand = ("SELECT * FROM [ATABLE]")

                GridView1.DataBind()

            ElseIf Not DropDownList1.SelectedIndex.Equals(0) And DropDownList2.SelectedIndex.Equals(0) Then

                DATA.SelectCommand = ("SELECT * FROM [ATABLE] where AFIELD = '" & DropDownList1.SelectedValue.ToString & " ' ")

                GridView1.DataBind()

            ElseIf DropDownList1.SelectedIndex.Equals(0) And Not DropDownList2.SelectedIndex.Equals(0) Then

                DATA.SelectCommand = ("SELECT * FROM [ATABLE] where BFIELD = '" & DropDownList2.SelectedValue.ToString & " ' ")

                GridView1.DataBind()

            ElseIf Not DropDownList1.SelectedIndex.Equals(0) And Not DropDownList2.SelectedIndex.Equals(0) Then

                DATA.SelectCommand = ("SELECT * FROM [ATABLE] where AFIELD = '" & DropDownList1.SelectedValue.ToString & " ' and BFIELD = '" & DropDownList2.SelectedValue.ToString & " ' ")

                GridView1.DataBind()

            End If

    Once I removed this code, the update worked.  Where should I put this code so that it doesn't interfere with the update? 

  • Re: update gridview issue

    02-03-2008, 12:15 PM
    • Member
      46 point Member
    • newbieAl
    • Member since 10-18-2007, 7:43 PM
    • Posts 303

     if i take the databind out in page_load then the filtering doesn't work.  If I put the code in the selectindexchanged event then the gridview is not displayed when the page loads.  Also, when I got to edit a filtered record, the page just refreshes and the grid is gone...

  • Re: update gridview issue

    02-03-2008, 12:41 PM
    Answer
    • Member
      46 point Member
    • newbieAl
    • Member since 10-18-2007, 7:43 PM
    • Posts 303

     I've found the solution.  I removed the page_load, dropdownlist logic, and added it to the sqldatasource: select command in the aspx page.  It works now. 

Page 1 of 1 (8 items)