• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

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

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

0
hijiki7777
Asked:
hijiki7777
  • 2
1 Solution
 
guru_samiCommented:
One option could be using Type= Object for UserId in UpdateParameters and before you add it to cm.Parameters Convert it to Guid.
0
 
hijiki7777Author 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

0
 
hijiki7777Author 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.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now