We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Cannot use guid type field for primary key in Gridview for Update

hijiki7777
hijiki7777 asked
on
Medium Priority
621 Views
Last Modified: 2012-05-06
I have a Guld field as a primary key in my datasource for my Gridview.
I cannot use this field when I want to do an update.
I have read round on this.
If I remove the DataObjectType name and remove the userId from the parameter list, then the update method is executed and the userId is correct, but the other parameters are null and the system falls over.
What is the solution to this?
<asp:GridView runat="server" ID="gvUsers" AllowPaging="True" DataSourceID="ods_Users"
            AutoGenerateColumns="False" DataKeyNames="UserId"
            >
                <Columns>
                    <asp:TemplateField HeaderText="User Name">
                        <ItemTemplate>
                            <asp:Label ID="LabelUserName" runat="server" Text='<%# Eval("UserName") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxUserName" runat="server" Text='<%# Eval("UserName") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Forename">
                        <ItemTemplate>
                            <asp:Label ID="LabelForename" runat="server" Text='<%# Eval("Forename") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxForename" runat="server" Text='<%# Eval("Forename") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Surname">
                        <ItemTemplate>
                            <asp:Label ID="LabelSurname" runat="server" Text='<%# Eval("Surname") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Eval("Surname") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Hours Per Day">
                        <ItemTemplate>
                            <asp:Label ID="LabelHoursPerDay" runat="server" Text='<%# Eval("HoursPerDay") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxHoursPerDay" runat="server" Text='<%# Eval("HoursPerDay") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                        ShowSelectButton="True" />
               </Columns>
            </asp:GridView>
            <asp:ObjectDataSource runat="server" ID="ods_Users" 
                 DeleteMethod="Delete" 
                 SelectMethod="Select" 
                TypeName="ResourceManager.UserData" UpdateMethod="Update" 
                DataObjectTypeName="System.Guid" OldValuesParameterFormatString="original_{0}" 
            >
                <UpdateParameters>
                    <asp:Parameter DbType="Guid" Name="userId" />
                    <asp:Parameter Name="userName" Type="String" />
                    <asp:Parameter Name="forename" Type="String" />
                    <asp:Parameter Name="surname" Type="String" />
                    <asp:Parameter Name="hoursPerDay" Type="Single" />
                </UpdateParameters>
            </asp:ObjectDataSource>
 
---------------------------------------------------------------------------------------------------------------
 
        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public void Update(Guid userId, string userName, string forename, string surname, float hoursPerDay)
        {
            using (SqlConnection cn = new SqlConnection(DataConnection))
            using (SqlCommand cm = new SqlCommand())
            {
                cn.Open();
 
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "dbo.rmUpdateUser";
                cm.Connection = cn;
                cm.Parameters.AddWithValue("@UserId", userId);
                cm.Parameters.AddWithValue("@UserName", userName);
                cm.Parameters.AddWithValue("@Forename", forename);
                cm.Parameters.AddWithValue("@Surname", surname);
                cm.Parameters.AddWithValue("@HoursPerDay", hoursPerDay);
 
                cm.ExecuteNonQuery();
 
                cn.Close();
            }
        }

Open in new window

Comment
Watch Question

Top Expert 2013
Commented:
One option could be using Type= Object for UserId in UpdateParameters and before you add it to cm.Parameters Convert it to Guid.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I got the message; Could not find a property named 'UserId' on the type specified by the DataObjectTypeName property in ObjectDataSource 'ods_Users'.

If I delete the DataObjectTypeName property, I then find that the UserId is correct, but the other parameters are null.
I think a rather clumsy work around is to introduce an alternative key that is not a Guid.
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h3>Edit Users</h3>
<table>
    <tr>
        <td>
            <asp:GridView runat="server" ID="gvUsers" AllowPaging="True" DataSourceID="ods_Users"
            AutoGenerateColumns="False" DataKeyNames="UserId"
            >
                <Columns>
                    <asp:TemplateField HeaderText="User Name">
                        <ItemTemplate>
                            <asp:Label ID="LabelUserName" runat="server" Text='<%# Eval("UserName") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxUserName" runat="server" Text='<%# Eval("UserName") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Forename">
                        <ItemTemplate>
                            <asp:Label ID="LabelForename" runat="server" Text='<%# Eval("Forename") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxForename" runat="server" Text='<%# Eval("Forename") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Surname">
                        <ItemTemplate>
                            <asp:Label ID="LabelSurname" runat="server" Text='<%# Eval("Surname") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxSurname" runat="server" Text='<%# Eval("Surname") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Hours Per Day">
                        <ItemTemplate>
                            <asp:Label ID="LabelHoursPerDay" runat="server" Text='<%# Eval("HoursPerDay") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBoxHoursPerDay" runat="server" Text='<%# Eval("HoursPerDay") %>' />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                        ShowSelectButton="True" />
               </Columns>
            </asp:GridView>
            <asp:ObjectDataSource runat="server" ID="ods_Users" 
                 SelectMethod="Select" 
                TypeName="ResourceManager.UserData" UpdateMethod="Update" DataObjectTypeName="System.Object" 
            >
                <UpdateParameters>
                    <asp:Parameter Name="userId" Type="Object"/>
                    <asp:Parameter Name="userName" Type="String" />
                    <asp:Parameter Name="forename" Type="String" />
                    <asp:Parameter Name="surname" Type="String" />
                    <asp:Parameter Name="hoursPerDay" Type="Single" />
                </UpdateParameters>
            </asp:ObjectDataSource>
        </td>
    </tr>
    <tr>
        <asp:Button runat="server" ID="btnAddUser"  Text="Add User" 
            SkinID="adminButtonSkin" onclick="btnAddUser_Click"/>
    </tr>
</table>

Open in new window

Author

Commented:
The answer was correct, but missed that for Textboxes that are input/output I should be using Bind and not Eval.
However the hard work was done and points are deserved. Using object does work.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.