ObjectDataSource and update methods

Last post 06-02-2008 12:03 PM by nwahlberg. 7 replies.

Sort Posts:

  • ObjectDataSource and update methods

    06-01-2008, 12:34 PM
    • Member
      4 point Member
    • nwahlberg
    • Member since 03-04-2008, 7:47 AM
    • Brooklyn, CT
    • Posts 22

    Hi, I am using FormView control to display a form for inserting and updating user records. I have not problem populating the form with the correct user record etc. but for some reason I can't get the data in the form to pass to my UpdateUserDetails method in the UserDB class. I am setting my object by using DataObjectTypeName. The class looks like this:

     

    using System;
    using System.Collections.Generic;
    using System.Text;


    namespace FPEN_Objects
    {
    public class User {

    // declare the class members
    private Guid userID;
    private string username;
    private string password;
    private string emailAddress;
    private string salutation;
    private string title;
    private string firstName;
    private string lastName;
    private string companyName;
    private string street;
    private string street2;
    private string city;
    private string state;
    private int zip;
    private string country;
    private string phoneNumber;
    private string faxNumber;
    private int allowedLogins;
    private DateTime expirationDate;
    private DateTime lastLoginDate;
    private bool recieveUpdateEmails;
    private string comments;
    private bool isProspect;
    private bool isActive;
    private bool isDeleted;
    private bool isRequest;
    private bool hasAgreed;
    private DateTime dateAgreed;

    private string roles;

    // default constructor
    public User() {
    }

    // constructor for login/session purposes
    public User(Guid userID, string userName, string password, string roles)
    {
    this.userID = userID;
    this.username = userName;
    this.password = password;
    this.roles = roles;
    }

    public Guid UserID
    {
    get { return userID; }
    set { userID = value; }
    }

    public string Username
    {
    get { return username; }
    set { username = value; }
    }

    public string Password
    {
    get { return password; }
    set { password = value; }
    }

    public string EmailAddress
    {
    get { return emailAddress; }
    set { emailAddress = value; }
    }

    public string Salutation
    {
    get { return salutation; }
    set { salutation = value; }
    }

    public string Title
    {
    get { return title; }
    set { title = value; }
    }

    public string FirstName
    {
    get { return firstName; }
    set { firstName = value; }
    }

    public string LastName
    {
    get { return lastName; }
    set { lastName = value; }
    }

    public string CompanyName
    {
    get { return companyName; }
    set { companyName = value; }
    }

    public string Street
    {
    get { return street; }
    set { street = value; }
    }

    public string Street2
    {
    get { return street2; }
    set { street2 = value; }
    }

    public string City
    {
    get { return city; }
    set { city = value; }
    }

    public string State
    {
    get { return state; }
    set { state = value; }
    }

    public int Zip
    {
    get { return zip; }
    set { zip = value; }
    }

    public string Country
    {
    get { return country; }
    set { country = value; }
    }

    public string PhoneNumber
    {
    get { return phoneNumber; }
    set { phoneNumber = value; }
    }

    public string FaxNumber
    {
    get { return faxNumber; }
    set { faxNumber = value; }
    }

    public int AllowedLogins
    {
    get { return allowedLogins; }
    set { allowedLogins = value; }
    }

    public DateTime ExpirationDate
    {
    get { return expirationDate; }
    set { expirationDate = value; }
    }

    public DateTime LastLoginDate
    {
    get { return lastLoginDate; }
    set { lastLoginDate = value; }
    }

    public bool RecieveUpdateEmails
    {
    get { return recieveUpdateEmails; }
    set { recieveUpdateEmails = value; }
    }

    public string Comments
    {
    get { return comments; }
    set { comments = value; }
    }

    public bool IsProspect
    {
    get { return isProspect; }
    set { isProspect = value; }
    }

    public bool IsActive
    {
    get { return isActive; }
    set { isActive = value; }
    }

    public bool IsDeleted
    {
    get { return isDeleted; }
    set { isDeleted = value; }
    }

    public bool IsRequest
    {
    get { return isRequest; }
    set { isRequest = value; }
    }

    public bool HasAgreed
    {
    get { return hasAgreed; }
    set { hasAgreed = value; }
    }

    public DateTime DateAgreed
    {
    get { return dateAgreed; }
    set { dateAgreed = value; }
    }

    public string Roles
    {
    get { return roles; }
    set { roles = value; }
    }

    }
    }

    My FormView tag looks like this:

     

    <asp:FormView ID="FormView1" runat="server" DataSourceID="userDetails" 
            DefaultMode="Edit">
            <ItemTemplate>
                <asp:Button ID="Button3" runat="server" Text="Edit User" CommandName="Edit" />
            </ItemTemplate>
            <InsertItemTemplate>
                insert user.
            </InsertItemTemplate>
            <EditItemTemplate>
                <table cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td valign="top">
                            <table class="formTbl" cellpadding="0" cellspacing="0" border="0">
                               
                                <tr>
                                    <td class="label">First Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="firstName" runat="server" Text='<%# Eval("FirstName") %>' />                                    
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Last Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="lastName" runat="server" Text='<%# Eval("LastName") %>' />
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td valign="top">
                            <asp:Button ID="updateUser" runat="server" Text="Update" CommandName="Update" />
                            <asp:Button ID="cancelUpdate" runat="server" Text="Cancel" onclick="cancelUpdate_Click" />
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
        </asp:FormView>
      

    Also, I have noticed that the fields are not available on my codebhind pages unless I move them out of the formview control. Is that expected behavior?


    Thanks! 

  • Re: ObjectDataSource and update methods

    06-01-2008, 2:15 PM
    • All-Star
      60,652 point All-Star
    • anas
    • Member since 09-21-2006, 8:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,847
    • Moderator

    Hello ,

    Can you please Edit Your Post and  format the code using the Source code button ()

    one more Note : I noticed that you didn't set the DataKeyNames property for your FormView ,you must set that property to your Object Identifier Name ,

    Like this :

    <asp:FormView DataKeyNames="UserId" ..... 

    Regards,

    Anas Ghanem | Blog

  • Re: ObjectDataSource and update methods

    06-01-2008, 2:26 PM
    • Member
      4 point Member
    • nwahlberg
    • Member since 03-04-2008, 7:47 AM
    • Brooklyn, CT
    • Posts 22

    Ok, so do I need to list every single field in that as a comma delimeted list?

    Thanks for your help! 

  • Re: ObjectDataSource and update methods

    06-01-2008, 2:43 PM
    Answer
    • All-Star
      60,652 point All-Star
    • anas
    • Member since 09-21-2006, 8:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,847
    • Moderator

    nwahlberg:
    Ok, so do I need to list every single field in that as a comma delimeted list?

    No , you just need to list the Field that maps to the Primary key for the table ( and i think its the UserId Field .. right ?).. so you just need to add the userid as i mentioned ,

     

    For now , i have some notes :

    you used Eval to Bind the TextBoxes to the Underlying Fields in your object , Note that Eval is used to implement a one-way binding , which means that any modifications on the Text boxes will not passed back to the ObjectDataSource, So please for Edit and Insert Templates , you should replace Eval with Bind ..

    nwahlberg:

    Also, I have noticed that the fields are not available on my codebhind pages unless I move them out of the formview control. Is that expected behavior? 

    Yes , that's a normal behavior ,when the controls placed inside a Template controls  you can't access them directly ,instead you need to use the formView  Findcontrol function to get a reference to them.

    Idea note that Findcontrol search the current displayed FormView Template , for example , you can't use it to find a control that is in EditTemplate while the formViw in insertTemplate ...

     

    Now , after applying the mentioned modifications , see if it worked , if not add a break point to ItemUpdated event handler for your formview and check the value of e.Exception , it must contains a details about the exception . try to paste that exception here..


     

    Regards,

    Anas Ghanem | Blog

  • Re: ObjectDataSource and update methods

    06-01-2008, 3:04 PM
    • Member
      4 point Member
    • nwahlberg
    • Member since 03-04-2008, 7:47 AM
    • Brooklyn, CT
    • Posts 22

     Great, this seems to work great! I am a little confused as to the DataKeyNames needing just the primary key...how does that work? Also, I didn't realize the difference on Bind vs. Eval...also great to know.

    Thanks again for the information!

  • Re: ObjectDataSource and update methods

    06-01-2008, 3:47 PM
    • All-Star
      60,652 point All-Star
    • anas
    • Member since 09-21-2006, 8:31 AM
    • Palestinian Territory, Occupied
    • Posts 6,847
    • Moderator

    nwahlberg:
    I am a little confused as to the DataKeyNames needing just the primary key...how does that work?

    The DataKeyNames used as a Temporary Storage for the Fields that is no going to be bound to a control inside the Template , what i mean is that to remember the value of any field that is not dispalyed in the FormView , you need to add that field to datakeynames property , so that the FormView will be able to send its value back to the ObjectDataSource ...

    That's why you faced a problem in update , you didn't displayed the UserId in the FormView Insert and edit templates , and so the FormView will not able to Get the Origianl Value of the UserId ,

    so a null value was passed for the UserId parameter ....

    Ideathe DataKeyNames is not just for the PrimayKeys , you can use it to store the value for any other fields of your object, for example if you have a property in your object which is not displayed on the FormView ( hidded or doesn't bound to a control like TextBox or Label ... you must add it to the dataKeyNames property to avoid setting it as null when the object updated !

    Regards,

    Anas Ghanem | Blog

  • Re: ObjectDataSource and update methods

    06-01-2008, 7:50 PM
    • Member
      4 point Member
    • nwahlberg
    • Member since 03-04-2008, 7:47 AM
    • Brooklyn, CT
    • Posts 22

    anas:
    what i mean is that to remember the value of any field that is not dispalyed in the FormView , you need to add that field to datakeynames property , so that the FormView will be able to send its value back to the ObjectDataSource

    Ok, but I wasn't getting the value of ANY of the fields until I added the DataKeyNames property. So, even if you are not trying to access a hidden field this needs to be there, correct? I'm sorry if this is a stupid question, but the relationship between the fields in the FormView and the DataKeyNames is not clear to me yet. I'm sure it will be once I have spent more time with these concepts. Or, was I just doing something else wrong?

    Thanks! 

  • Re: ObjectDataSource and update methods

    06-02-2008, 12:03 PM
    • Member
      4 point Member
    • nwahlberg
    • Member since 03-04-2008, 7:47 AM
    • Brooklyn, CT
    • Posts 22

    at the risk of sourding stupid here, now I am running into a problem with my InsertMethod instead! Tongue Tied

    Here's ther error that I am seeing:

    ObjectDataSource 'userDetails' has no values to insert. Check that the 'values' dictionary contains values.

    Again, here is my FormView

     

       <asp:FormView ID="FormView1" runat="server" DataSourceID="userDetails" 
            DefaultMode="Edit"
            DataKeyNames="userID">
            <ItemTemplate>
                <asp:Button ID="Button3" runat="server" Text="Edit User" CommandName="Edit" />
            </ItemTemplate>        
            <InsertItemTemplate>
                <table cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td valign="top">
                            <asp:HiddenField ID="userID" Visible="false" runat="server" />
                            <table class="formTbl" cellpadding="0" cellspacing="0" border="0">
                                <tr>
                                    <td class="label">Recieve Email Updates</td>
                                    <td class="field">
                                        <asp:CheckBox ID="recieveEmailUpdates" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Salutation</td>
                                    <td class="field">
                                        <asp:DropDownList ID="salutation" runat="server">
                                           <asp:ListItem Value="Dr.">Dr.</asp:ListItem>
                                           <asp:ListItem Value="Mr.">Mr.</asp:ListItem>                                       
                                           <asp:ListItem Value="Mrs.">Mrs.</asp:ListItem>
                                           <asp:ListItem Value="Miss">Miss</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">First Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="firstName" Text="" runat="server" />                                    
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Last Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="lastName" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Email Address</td>
                                    <td class="field">
                                        <asp:TextBox ID="emailAddress" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Username</td>
                                    <td class="field">
                                        <asp:TextBox ID="username" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Password</td>
                                    <td class="field">
                                        <asp:TextBox ID="password" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Company Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="companyName" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Title</td>
                                    <td class="field">
                                        <asp:TextBox ID="title" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Address</td>
                                    <td class="field">
                                        <asp:TextBox ID="street" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Address 2</td>
                                    <td class="field">
                                        <asp:TextBox ID="street2" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">City</td>
                                    <td class="field">
                                        <asp:TextBox ID="city" Text="" runat="server"/>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">State</td>
                                    <td class="field">
                                        <asp:TextBox ID="state" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Zip Code</td>
                                    <td class="field">
                                        <asp:TextBox ID="zip" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Country</td>
                                    <td class="field">
                                        <asp:TextBox ID="country" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Phone Number</td>
                                    <td class="field">
                                        <asp:TextBox ID="phoneNumber" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Fax Number</td>
                                    <td class="field">
                                        <asp:TextBox ID="faxNumber" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Comments</td>
                                    <td class="field">
                                        <asp:TextBox Rows="5" ID="comments" Text="" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Expiration Date</td>
                                    <td class="field">
                                        <asp:TextBox Rows="5" ID="expirationDate" Text="" runat="server" /> <asp:Image runat="server" ToolTip="Pick Date" ImageUrl="~/images/btn_cal.gif" id="expirationDate_b" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td valign="top">
                            <asp:Button ID="insertUser" runat="server" Text="Insert" CommandName="Insert" />
                            <asp:Button ID="cancelUpdate" runat="server" Text="Cancel" onclick="cancelUpdate_Click" />
                        </td>
                    </tr>
                </table>
                
                <script type="text/javascript" language="javascript">
                    Calendar.setup(
                        {
                            inputField     :    '<%=FormView1.FindControl("expirationDate").ClientID%>',
                            ifFormat       :    '%m/%d/%Y %I:%M:%S %p',
                            button         :    '<%=FormView1.FindControl("expirationDate_b").ClientID%>',
                            align          :    'Br',
                            singleClick    :    true,
                            showsTime      :    true
                        }
                    );
                </script>
            </InsertItemTemplate>
            <EditItemTemplate>
                <table cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td valign="top">
                            <asp:HiddenField ID="userID" Visible="false" Value='<%# Bind("UserID") %>' runat="server" />
                            <table class="formTbl" cellpadding="0" cellspacing="0" border="0">
                                <tr>
                                    <td class="label">Salutation</td>
                                    <td class="field">
                                        <asp:DropDownList ID="salutation" runat="server" SelectedValue='<%# Bind("Salutation") %>'>
                                           <asp:ListItem Value="Dr.">Dr.</asp:ListItem>
                                           <asp:ListItem Value="Mr.">Mr.</asp:ListItem>                                       
                                           <asp:ListItem Value="Mrs.">Mrs.</asp:ListItem>
                                           <asp:ListItem Value="Miss">Miss</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">First Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="firstName" runat="server" Text='<%# Bind("FirstName") %>' />                                    
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Last Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="lastName" runat="server" Text='<%# Bind("LastName") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Email Address</td>
                                    <td class="field">
                                        <asp:TextBox ID="emailAddress" runat="server" Text='<%# Bind("EmailAddress") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Username</td>
                                    <td class="field">
                                        <asp:TextBox ID="username" runat="server" Text='<%# Bind("Username") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Password</td>
                                    <td class="field">
                                        <asp:TextBox ID="password" runat="server" Text='<%# Bind("Password") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Company Name</td>
                                    <td class="field">
                                        <asp:TextBox ID="companyName" runat="server" Text='<%# Bind("CompanyName") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Title</td>
                                    <td class="field">
                                        <asp:TextBox ID="title" runat="server" Text='<%# Bind("Title") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Address</td>
                                    <td class="field">
                                        <asp:TextBox ID="street" runat="server" Text='<%# Bind("Street") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Address 2</td>
                                    <td class="field">
                                        <asp:TextBox ID="street2" runat="server" Text='<%# Bind("Street2") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">City</td>
                                    <td class="field">
                                        <asp:TextBox ID="city" runat="server" Text='<%# Bind("City") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">State</td>
                                    <td class="field">
                                        <asp:TextBox ID="state" runat="server" Text='<%# Bind("State") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Zip Code</td>
                                    <td class="field">
                                        <asp:TextBox ID="zip" runat="server" Text='<%# Bind("Zip") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Country</td>
                                    <td class="field">
                                        <asp:TextBox ID="country" runat="server" Text='<%# Bind("Country") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Phone Number</td>
                                    <td class="field">
                                        <asp:TextBox ID="phoneNumber" runat="server" Text='<%# Bind("PhoneNumber") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Fax Number</td>
                                    <td class="field">
                                        <asp:TextBox ID="faxNumber" runat="server" Text='<%# Bind("FaxNumber") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Comments</td>
                                    <td class="field">
                                        <asp:TextBox Rows="5" ID="comments" runat="server" Text='<%# Bind("Comments") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="label">Expiration Date</td>
                                    <td class="field">
                                        <asp:TextBox Rows="5" ID="expirationDate" runat="server" Text='<%# Bind("ExpirationDate") %>' /> <asp:Image runat="server" ToolTip="Pick Date" ImageUrl="~/images/btn_cal.gif" id="expirationDate_b" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td valign="top">
                            <asp:Button ID="updateUser" runat="server" Text="Update" CommandName="Update" />
                            <asp:Button ID="cancelUpdate" runat="server" Text="Cancel" onclick="cancelUpdate_Click" />
                        </td>
                    </tr>
                </table>
                
                <script type="text/javascript" language="javascript">
                    Calendar.setup(
                        {
                            inputField     :    '<%=FormView1.FindControl("expirationDate").ClientID%>',
                            ifFormat       :    '%m/%d/%Y %I:%M:%S %p',
                            button         :    '<%=FormView1.FindControl("expirationDate_b").ClientID%>',
                            align          :    'Br',
                            singleClick    :    true,
                            showsTime      :    true
                        }
                    );
                </script>
            </EditItemTemplate>
        </asp:FormView>
     Thanks again!
Page 1 of 1 (8 items)