Updating a gridview with a joined table select statement

Posted on 2008-11-10
Last Modified: 2013-11-26
Using Visual Studio 2005
I have a gridview that I populate with a select statement that joins two tables.
Select events.*, eventtype.label
From events, eventtype
Where events.eventtypeID = eventtype.eventtypeID
The update will update the events table.
I cannot get the update to work with the joined field.  Is there a property I can set to make this work or will I need to figure another way??
Question by:PEAKTT
    LVL 2

    Expert Comment

    I think instead of using SQLDataSource, I suggest you to use ObjectDataSource where it gives you flexible of Select, Insert, updating as you need.

    Do some expore on ObjectDataSource
    LVL 20

    Expert Comment


    But SqlDataSource does it as well (

    Can you please post the SQL statement you use for the SqlDataSource.UpdateCommand property?
    LVL 4

    Expert Comment

    <asp:SqlDataSource ID="display" runat="server" ConnectionString="<%$ ConnectionStrings:myprojDB %>"
    SelectCommand="SELECT customer.[UserId],[aspnet_Membership].[Email], aspnet_Users.[UserName], [fname] FROM [customer] JOIN [aspnet_Users] ON [customer].[UserId] = [aspnet_Users].[UserId]  JOIN [aspnet_Membership] ON [aspnet_Users].[UserId] = [aspnet_Membership].[UserId] WHERE (customer.[UserId] = @UserId)"
    UpdateCommand="UPDATE [customer] SET [fname] = @fname WHERE [UserId] = @UserId">
                                                    <asp:Parameter Name="fname" Type="String" />
                                                    <asp:ControlParameter ControlID="grdCustomers" Name="UserId" PropertyName="SelectedValue" Type="Object" />

    This works for me!!! I not so clear about your question. See if this helps or le me know what u r looking for exactly

    Author Comment

    This is my SQL statement code:

    I am using the select command to fill the gridview rows.  Each row will have an Edit button.
    I then use the update command to edit the rows.

    These statements are what I am using.  As is the update does not work.  If I take out the field from the joined table, it works just fine.  I have the gridview set with enable editing and the datakeynames set to the key field.

    Any Ideas??
    SelectCommand="SELECT SubpeonaExhibitFields.SubpeonaExhibitFieldsID, SubpeonaExhibitFields.SubpoenaID, SubpeonaExhibitFields.ProvideriD, SubpeonaExhibitFields.ExhibitID, SubpeonaExhibitFields.ExhibitFieldsID, SubpeonaExhibitFields.ExhibitFieldValue, SubpeonaExhibitFields.ModifiedDate, SubpeonaExhibitFields.ModifiedBy, ExhibitFields.ExhibitLabel FROM SubpeonaExhibitFields LEFT OUTER JOIN ExhibitFields ON SubpeonaExhibitFields.ExhibitFieldsID = ExhibitFields.ExhibitFieldsID WHERE (SubpeonaExhibitFields.SubpeonaExhibitFieldsID = @SubpeonaExhibitFieldsID) ORDER BY SubpeonaExhibitFields.ExhibitFieldsID" 
                        UpdateCommand="UPDATE SubpeonaExhibitFields SET SubpoenaID = @SubpoenaID, ProvideriD = @ProvideriD, ExhibitID = @ExhibitID, ExhibitFieldsID = @ExhibitFieldsID, ExhibitFieldValue = @ExhibitFieldValue, ModifiedDate = GETDATE(), ModifiedBy = @ModifiedBy WHERE (SubpeonaExhibitFieldsID = @SubpeonaExhibitFieldsID)" 
                        DeleteCommand="DELETE FROM [SubpeonaExhibitFields] WHERE [SubpeonaExhibitFieldsID] = @SubpeonaExhibitFieldsID" 
                        InsertCommand="INSERT INTO [SubpeonaExhibitFields] ([SubpoenaID], [ProvideriD], [ExhibitID], [ExhibitFieldsID], [ExhibitFieldValue], [ModifiedDate], [ModifiedBy]) VALUES (@SubpoenaID, @ProvideriD, @ExhibitID, @ExhibitFieldsID, @ExhibitFieldValue,getdate(), @ModifiedBy)">

    Open in new window


    Accepted Solution

    I found that if you add all the key fields to the DataKeyNames property you can have them hidden when displaying the gridview yet use them for editting.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now