[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

update not working on gridview

Posted on 2008-02-01
19
Medium Priority
?
953 Views
Last Modified: 2010-04-21
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>
0
Comment
Question by:newbieal
18 Comments
 
LVL 9

Expert Comment

by:divinewind80
ID: 20797857
Are you declaring the values for your parameters in the code behind?  If not, it would appear they remain blank... updating nothing in your table.  

Another option, of course, is to use a control parameter or another type...
0
 
LVL 4

Author Comment

by:newbieal
ID: 20798068
No, I'm doing nothing in the code behing reg the parameters/values.  Do you have an example?
0
 
LVL 14

Expert Comment

by:shahprabal
ID: 20798929
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 4

Author Comment

by:newbieal
ID: 20800498
thanks but that link's info doesn't help me in my case, as I have everything set up the way it mentions.   What exactly did you mean by defining the parameter in the code behind?  Example?
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 20805227
In your gridview do you have bound fields for afield, bfield and id? If not the gridview won't know where to get the values from.
0
 
LVL 4

Author Comment

by:newbieal
ID: 20806903
Yes.  In fact when I hard code values the gridview gets updated. This works:


UpdateCommand="Update atable set [afield]='test', [bfield]='test' where [id]=@id"

But not when I put the parameter values there.  
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 20808142
Can you post the code you are using for your gridview?
0
 
LVL 4

Author Comment

by:newbieal
ID: 20808720
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 MRPS" />
                <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>

Open in new window

0
 
LVL 15

Expert Comment

by:NazoUK
ID: 20808936
I recreated your example using an accessdatasource and it worked fine for me. What kind of datasource object are you using?
Also you don't need to explicitly declare the update parameters, it will automatically figure out what they are, not sure if that's causing the problem but worth a shot.
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 20808939
Also do you have anything in your code-behind (such as in page_load) that could be interfering with the update operation?
0
 
LVL 4

Author Comment

by:newbieal
ID: 20809093
I've removed the update parameters and it didn't make a difference - it still didn't work.

I'm using sqldata source.

Yes, page load has some logic reg. dropdownlist selection (ie. if a then not b, etc.) and after each if statements I have a gridview1.databind().
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 20809144
There's the problem. Calling databind in your pageload is causing the changes to be lost.
The gridview will automatically bind to a sqldatasource if it's datasourceid is set (which it is), there shouldn't be any need to manually call databind unless you're manually changing things.
If the data in the gridview somehow depends on the values in the dropdown lists (changing the datasource or setting select parameters, this kind of thing) then move the logic to a selectedindexchanged event for them and call the databind in there.
I suspect calling databind in the pageload is causing the update values to be lost thus nothing happens.

I've confirmed this in my own test, calling databind in page_load causes the update to fail.
0
 
LVL 4

Author Comment

by:newbieal
ID: 20809195
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?
0
 
LVL 4

Author Comment

by:newbieal
ID: 20809204
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...
0
 
LVL 15

Accepted Solution

by:
NazoUK earned 2000 total points
ID: 20809232
I think what I'd be tempted to do is to rewrite your select command in the datasource to use the values of the dropdown list directly. Assuming index 0 of your drop down lists is an empty string (replace @Param1/2 ='' with whatever the default value is if not)

SELECT * FROM [Atable] WHERE (Afield=@param1 or @Param1='') AND (Bfield=@Param2 OR @Param2='')

This will match everything if your parameter is blank, or the selected value if something is chosen.

Then in your sqldatasource:

<selectParameters>
     <asp:controlparameter name="param1" controlid="DropDownList1" PropertyName="SelectedValue" type="String" />
     <asp:controlparameter name="param2" controlid="DropDownList2" PropertyName="SelectedValue" type="String" />
</selectParameters>

If you set the autopostback="true" properties on your dropdownlist objects the data will automatically rebind on postback, otherwise add a button on there or whatever. Tehy key thing is asp.net will filter the gridview without you having to do anything manually and it shouldn't interfere with your updates.

You can then get rid of the decision logic in the page_load altogether.
0
 
LVL 4

Author Comment

by:newbieal
ID: 20809260
NazoUK,

You are awesome!! I've made the changes you suggested and it works like a charm.  Thank you so much!!
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 20809428
You're very welcome, glad it worked :)
0
 
LVL 4

Author Closing Comment

by:newbieal
ID: 31427165
Perfect solution provided!!!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question