hijiki7777
asked on
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?
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();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
However the hard work was done and points are deserved. Using object does work.
ASKER
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.
Open in new window