RE: Gridview not updating the database
- From: Siva M <shiva_sm@xxxxxxxxxxxxxxxxx>
- Date: Thu, 14 Jun 2007 22:30:00 -0700
It looks like you are not setting DataKeyNames property for the GridView.
This property represents the primary key (or comma separated if composite PK)
for the table bound to the grid and GridView needs this info for automatic
update or delete.
Also from MSDN: "You must set the DataKeyNames property for the automatic
updating and deleting features of the GridView control to work. The values of
these key fields are passed to the data source control in order to match the
row to update or delete."
"Barbara Alderton" wrote:
I setup a simple gridview as a utility just to do some updates, nothing fancy.
just wanted easy UI to make updates. When I select ‘Edit’, I get the fields
I want to edit. I edit them and click ‘Update’, the page returns to its
original state (prior to clicking Edit) and no updates occur in the DB. What
am I missing? I included the html code below.
--
Thank-you,
Barbara Alderton
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="DBNameSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CONV_ADMINConnectionString %>"
SelectCommand="SELECT DISTINCT [DBName] FROM
[CNV_CODE_MAPPING]"></asp:SqlDataSource>
<asp:SqlDataSource ID="TableSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CONV_ADMINConnectionString %>"
SelectCommand="SELECT DISTINCT [TableName] FROM
[CNV_CODE_MAPPING] WHERE ([DBName] = @DBName)">
<SelectParameters>
<asp:ControlParameter ControlID="ddDataBases" Name="DBName"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ForeignKeySource" runat="server"
ConnectionString="<%$ ConnectionStrings:CONV_ADMINConnectionString %>"
SelectCommand="SELECT DISTINCT [ForeignKey] FROM
[CNV_CODE_MAPPING] WHERE (([DBName] = @DBName) AND ([TableName] =
@TableName))">
<SelectParameters>
<asp:ControlParameter ControlID="ddDataBases" Name="DBName"
PropertyName="SelectedValue"
Type="String" />
<asp:ControlParameter ControlID="ddTables" Name="TableName"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<asp:SqlDataSource ID="DataGridSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CONV_ADMINConnectionString %>"
SelectCommand="SELECT [Mapping_UID], [DBName], [TableName],
[ForeignKey], [UIDOld], [ValueOld], [DescOld], [CreateDate], [DescNew],
[ValueNew], [UIDNew], [LastChangeDate] FROM [CNV_CODE_MAPPING] WHERE
(([DBName] = @DBName) AND ([TableName] = @TableName) AND ([ForeignKey] =
@ForeignKey))" OldValuesParameterFormatString="original_{0}"
DeleteCommand="DELETE FROM [CNV_CODE_MAPPING] WHERE [Mapping_UID] =
@original_Mapping_UID" InsertCommand="INSERT INTO [CNV_CODE_MAPPING]
([DBName], [TableName], [ForeignKey], [UIDOld], [ValueOld], [DescOld],
[CreateDate], [DescNew], [ValueNew], [UIDNew], [LastChangeDate]) VALUES
(@DBName, @TableName, @ForeignKey, @UIDOld, @ValueOld, @DescOld, @CreateDate,
@DescNew, @ValueNew, @UIDNew, @LastChangeDate)" UpdateCommand="UPDATE
[CNV_CODE_MAPPING] SET [DBName] = @DBName, [TableName] = @TableName,
[ForeignKey] = @ForeignKey, [UIDOld] = @UIDOld, [ValueOld] = @ValueOld,
[DescOld] = @DescOld, [CreateDate] = @CreateDate, [DescNew] = @DescNew,
[ValueNew] = @ValueNew, [UIDNew] = @UIDNew, [LastChangeDate] =
@LastChangeDate WHERE [Mapping_UID] = @original_Mapping_UID">
<SelectParameters>
<asp:ControlParameter ControlID="ddDataBases" Name="DBName"
PropertyName="SelectedValue"
Type="String" />
<asp:ControlParameter ControlID="ddTables" Name="TableName"
PropertyName="SelectedValue"
Type="String" />
<asp:ControlParameter ControlID="ddForeignKeys"
Name="ForeignKey" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_Mapping_UID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="DBName" Type="String" />
<asp:Parameter Name="TableName" Type="String" />
<asp:Parameter Name="ForeignKey" Type="String" />
<asp:Parameter Name="UIDOld" Type="Int32" />
<asp:Parameter Name="ValueOld" Type="String" />
<asp:Parameter Name="DescOld" Type="String" />
<asp:Parameter Name="CreateDate" Type="DateTime" />
<asp:Parameter Name="DescNew" Type="String" />
<asp:Parameter Name="ValueNew" Type="String" />
<asp:Parameter Name="UIDNew" Type="Int32" />
<asp:Parameter Name="LastChangeDate" Type="DateTime" />
<asp:Parameter Name="original_Mapping_UID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="DBName" Type="String" />
<asp:Parameter Name="TableName" Type="String" />
<asp:Parameter Name="ForeignKey" Type="String" />
<asp:Parameter Name="UIDOld" Type="Int32" />
<asp:Parameter Name="ValueOld" Type="String" />
<asp:Parameter Name="DescOld" Type="String" />
<asp:Parameter Name="CreateDate" Type="DateTime" />
<asp:Parameter Name="DescNew" Type="String" />
<asp:Parameter Name="ValueNew" Type="String" />
<asp:Parameter Name="UIDNew" Type="Int32" />
<asp:Parameter Name="LastChangeDate" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
<table style="width: 800px">
<tr>
<td style="width: 800px">
<table style="width: 790px">
<tr>
<td style="width: 150px">
</td>
<td style="text-align: center; width: 490px">
<span style="font-family: Arial"><strong>Code Mapping
Tool</strong></span></td>
<td style="width: 150px">
</td>
</tr>
<tr>
<td style="height: 21px">
</td>
<td style="height: 21px">
</td>
<td style="height: 21px">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="width: 800px">
<table style="width: 400px">
<tr>
<td style="width: 167px">
<asp:Label ID="Label1" runat="server"
Font-Bold="True" Font-Names="Arial" Font-Size="11pt"
Text="DataBase Name:"
Width="120px"></asp:Label></td>
<td style="width: 200px">
<asp:DropDownList ID="ddDataBases"
runat="server" AutoPostBack="True" DataSourceID="DBNameSource"
DataTextField="DBName"
DataValueField="DBName">
</asp:DropDownList></td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td style="width: 167px; height: 20px;">
<asp:Label ID="lblTblTag" runat="server"
Font-Bold="True" Font-Names="Arial" Font-Size="11pt"
Text="Table Name:"></asp:Label></td>
<td style="width: 200px; height: 20px;">
<asp:DropDownList ID="ddTables"
runat="server" AutoPostBack="True" DataSourceID="TableSource"
DataTextField="TableName"
DataValueField="TableName">
</asp:DropDownList></td>
<td style="width: 100px; height: 20px;">
</td>
</tr>
<tr>
<td style="width: 167px">
<asp:Label ID="Label2" runat="server"
Font-Bold="True" Font-Names="Arial" Font-Size="11pt"
Text="Foreign Key:"></asp:Label></td>
<td style="width: 200px">
<asp:DropDownList ID="ddForeignKeys"
runat="server" AutoPostBack="True" DataSourceID="ForeignKeySource"
DataTextField="ForeignKey"
DataValueField="ForeignKey">
</asp:DropDownList></td>
<td style="width: 100px">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="width: 800px">
<table style="width: 790px">
<tr>
<td style="width: 50px">
</td>
<td style="width: 740px">
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid"
BorderWidth="3px" CellPadding="4"
CellSpacing="2" DataSourceID="DataGridSource"
ForeColor="Black">
<FooterStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField
DataField="Mapping_UID" HeaderText="Mapping_UID" InsertVisible="False"
ReadOnly="True"
SortExpression="Mapping_UID" />
<asp:BoundField
DataField="TableName" HeaderText="Table Name" ReadOnly="True"
SortExpression="TableName" >
<FooterStyle Width="50px" />
</asp:BoundField>
<asp:BoundField
DataField="ForeignKey" HeaderText="Foreign Key" ReadOnly="True"
SortExpression="ForeignKey" />
<asp:BoundField DataField="UIDOld"
HeaderText="Old UID" ReadOnly="True" SortExpression="UIDOld" />
<asp:BoundField DataField="ValueOld"
HeaderText="Old Value" ReadOnly="True" SortExpression="ValueOld" />
<asp:BoundField DataField="DescOld"
HeaderText="Old Desc" ReadOnly="True" SortExpression="DescOld" />
<asp:BoundField
DataField="CreateDate" DataFormatString="{0:d}" HeaderText="Create Date"
HtmlEncode="False"
ReadOnly="True" SortExpression="CreateDate" />
<asp:BoundField DataField="UIDNew"
HeaderText="UIDNew" SortExpression="UIDNew" />
<asp:BoundField DataField="ValueNew"
HeaderText="New Value" SortExpression="ValueNew" />
<asp:BoundField DataField="DescNew"
HeaderText="New Desc" SortExpression="DescNew" />
<asp:BoundField
DataField="LastChangeDate" HeaderText="Last Change Date"
SortExpression="LastChangeDate" />
<asp:CommandField
ShowEditButton="True" />
</Columns>
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099"
Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC"
ForeColor="Black" HorizontalAlign="Left" />
<HeaderStyle BackColor="Black"
Font-Bold="True" Font-Names="Arial" Font-Size="11pt"
ForeColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="width: 50px">
</td>
<td style="width: 740px">
</td>
</tr>
<tr>
<td style="width: 50px">
</td>
<td style="width: 740px">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
- References:
- Gridview not updating the database
- From: Barbara Alderton
- Gridview not updating the database
- Prev by Date: Re: Help needed on ComputeHash()
- Next by Date: Re: Make images only available to intranet
- Previous by thread: Gridview not updating the database
- Next by thread: RE: Gridview not updating the database
- Index(es):