Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Button to update row in gridview

Posted on 2012-03-21
8
Medium Priority
?
402 Views
Last Modified: 2012-03-21
I am new to asp.net and I am hoping you can help with a question on gridview controls.   I have a gridview control that shows a list of transactions.   I have added two buttons (Approve/Rescind) to the gridview that I would like to update a field on the record.   There is a SQL data source that is attached to the gridview control and I have created two separate SQL data sources with the appropriate update statements for the updating the field.  I have assigned the buttons with CommandName attributes of "Approve" and "Rescind" respectively.   A couple of questions...

1.  There is a transaction ID in the select statement used by the gridview.   It is the primary key for the table and I would need to use this for my update statement.  How do I get the transaction id associated with the buttons on a given row?  

2.  How would I initiate the update (assume this is done in the C# code)?

Thank you!
John
0
Comment
Question by:jzlamal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747032
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37747123
How do I get the transaction id associated with the buttons on a given row?  

Add a CommandArgument attribute to the button to hold the transaction ID.

<asp:Button ID="btnTest" Text="Approve" runat="server" CommandName="Approve" CommandArgument='<%# Bind("transactionid") %>' OnCommand="btnTest_Command" />

Handle the update on the button click, retrieve the transaction id from the CommandArgument.
protected void btnTest_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Approve")
            {
                string transactionId = (string)e.CommandArgument;
                //Update the database
            }
        }

Open in new window

0
 

Author Comment

by:jzlamal
ID: 37747153
I tried your suggestion and I received the following...

System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:Button' is of type 'System.Web.UI.WebControls.Button'

Is the Button field you provided appropriate for adding inside a gridview control or do I need to use something else?

Thanks!
john
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Accepted Solution

by:
Tom Beck earned 2000 total points
ID: 37747162
The asp:Button would need to be inside an asp:TemplateField.

         <asp:TemplateField>
            <ItemTemplate>                
                <asp:Button ID="btnTest" Text="Approve" runat="server" CommandName="Approve" CommandArgument='<%# Bind("transactionid") %>' OnCommand="btnTest_Command" />
            </ItemTemplate>
        </asp:TemplateField>
0
 

Author Comment

by:jzlamal
ID: 37747288
Ok.  So the transaction ID is being passed when I click on the button.   However, it is not being passed along to the update statement.   In the btnTest_Command, I am calling the Update method of the SQL Datasource using "UpdApprove.Update()".   I have a SQL Data source defined as:

<asp:SqlDataSource ID="UpdApprove" runat="server"
        ConnectionString="<%$ ConnectionStrings:PMMARTConnectionString %>"
        UpdateCommand="UPDATE [TT_TRANS] SET [ApprovedFlag] = 'Y', [ApprDate] = getdate() WHERE [TranID] = @TranID">
        <UpdateParameters>
            <asp:Parameter Name="TranID" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

Do I need to do anything else to pass the transactionID from the btnTest_Command to the SQLDataSource?   Even though the button is getting the correct transaction id, it doesn't seem to be passing it along to the update statement.  Thanks!
0
 

Author Closing Comment

by:jzlamal
ID: 37747422
Once I added a line to update the UpdateParameter, everything worked.   Thank you so much!
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37747484
You are revealing new circumstances that were not part of your original question. The method I presented assumed that you were manually updating your database. If you are using all the built-in mechanisms for populating/updating a Gridview then disregard my suggestion. There are multiple ways to do this. You could specify an OnRowUpdating event in your GridView markup.

<asp:GridView ID="GridView1" OnRowUpdating="GridView1_RowUpdating"....

You could add DataKeyNames to get the transaction ids.

...DataKeyNames="transactionid" >

Then in the <columns> block you could include a command field.

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

When a user clicks the Edit link, in code behind, switch to Update mode.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex; // turn to edit mode
            BindGridView(); // Rebind GridView to show the data in edit mode
        }

Open in new window


This should change your BoundFields to textboxes and the Edit/Delete link to Update/Cancel.

User makes changes and clicks Update link

Then in the RowUpdating event.
protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
             string transactionId = (string)GridView1.DataKeys[0].Value;
             //Use the DataKey as the Update statement parameter
        }

Open in new window


But, like I said, there are many possibilities.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37747501
Our posts crossed. Thanks for the points. Glad to help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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