views:

340

answers:

1

I have a Detailsview object that is loaded with user data when a User is clicked from a gridview. The problem is, I am using a sqldatasource and I'd rather use my pre-exisiting user class which has all the built in functionality for this. I haven't been able to get this done correctly. I can't get the detailsview populated with user data without the sqldatasource, and when I try inserting or updating with my objects, it runs through the insert/update code, then fails afterwards because I don't have a InsertCommand, etc for my SQLDataSource....bottom line...can somebody help me get this working without needing the SQLDataSource?

Here's my code.

<asp:GridView runat="server" ID="gvUsers" DataKeyNames="UserID" BackColor="#eeeeee" Width="85%"
                        HorizontalAlign="Center"
                        Font-Bold="True" Font-Names="Verdana"
                        Font-Size="10pt" AutoGenerateColumns="False"
                        OnRowDataBound="GridView1_RowDataBound"
                        OnRowDeleting="GridView1_RowDeleting" >
                <HeaderStyle BackColor="Black" ForeColor="White"
                       Font-Bold="True" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="yellow" ForeColor="blue" />
                <AlternatingRowStyle BackColor="#ffffff" />
                       <Columns>
                             <asp:TemplateField>
                           <ItemTemplate>
                               <asp:LinkButton ID="LinkButton2"
                                 CommandArgument='<%# Eval("UserID") %>'
                                 CommandName="Select" runat="server">
                                 Select</asp:LinkButton>
                             </ItemTemplate>     
                             </asp:TemplateField>
                             <asp:BoundField DataField="UserID" Visible="false" />
                            <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                            <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                            <asp:TemplateField HeaderText="Delete?">
                             <ItemTemplate>
                               <asp:LinkButton ID="LinkButton1"
                                 CommandArgument='<%# Eval("UserID") %>'
                                 CommandName="Delete" runat="server">
                                 Delete</asp:LinkButton>
                             </ItemTemplate>
                           </asp:TemplateField>
                        </Columns>
                  </asp:GridView><br /><br />
                  <asp:DetailsView runat="server" ID="dvUser" DataSourceID="SqlDataSource3" AutoGenerateRows="False" Width="85%"
                        HorizontalAlign="Center" DataKeyNames="UserID">
                      <Fields>
                        <asp:BoundField DataField="UserID" Visible="false" />
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                        <asp:BoundField DataField="UserName" HeaderText="User Name" />
                        <asp:BoundField DataField="Password" HeaderText="Password" />
                        <asp:BoundField DataField="Birthdate" HeaderText="Birthdate" />
                        <asp:BoundField DataField="Address" HeaderText="Address" />
                        <asp:BoundField DataField="Apt" HeaderText="Apt" />
                        <asp:BoundField DataField="City" HeaderText="City" />
                        <asp:BoundField DataField="Province" HeaderText="Province" />
                        <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
                        <asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum" />
                        <asp:BoundField DataField="Email" HeaderText="Email" />
                        <asp:BoundField DataField="ynAdminUser" HeaderText="ynAdminUser" />
                        <asp:CommandField ShowDeleteButton="False" ShowEditButton="True" ShowInsertButton="True" />
                    </Fields>
                </asp:DetailsView>
                    <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:ConnectionString%>" ID="SqlDataSource3"
                        runat="server" SelectCommand="sp_GetUser" SelectCommandType="StoredProcedure" OnInserting="OnInserting" >
                        <SelectParameters>
                            <asp:ControlParameter ControlID="gvUsers" Name="UserID" PropertyName="SelectedValue" Type="Int32" />
                        </SelectParameters>

</asp:SqlDataSource>

Not sure the CodeBehind is necessary, I just want to use that to call my data object code for updating, insert etc

+1  A: 

Why not use a ObjectDataSource. This control works much like an SqlDataSource but instead of specifying an SQL query or Stored Procedure you specify the methods of your custom business object to perform your data access.

<asp:ObjectDataSource ID="ObjectDataSource" Drunat="server" DeleteMethod="Delete" InsertMethod="Insert" SelectMethod="Select" UpdateMethod="Update" TypeName="YourType">

Here's an example illustrating how to use an ObjectDataSource with a DetailsView.

Phaedrus
thanks, this looks like the answer for me, however my User class "Load" method selected user data and loads it to member variables...does this mean I have to create another method to return the results or is there a way I can use the member variables that are loaded to display the data I have loaded in my cUser.Load method?
Mark
No, you should be able to use your existing Load() method. Specify your Load() Method in the SelectMethod property of the ObjectDataSource. In your dvUser DetailsView set the values of the BoundField DataField properties to the corresponding properties of your custom object.
Phaedrus
my load method requires UserId passed in...I don't see any parameter options for the selectmethod...
Mark
Arguments required by your Load() method can be defined as a ControlParameter within your SelectParameters of your ObjectDataSource to pass UserID just as you did with the SqlDataSource control.
Phaedrus