Trouble with GridView data keys

325 views
Skip to first unread message

Pizzini

unread,
Jan 20, 2009, 3:41:03 PM1/20/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I'm trying to put together what I thought was a simple gridview/
detailsview. But I'm running into this when I try to view the page.
(The rest of the page, loads fine if I take the grid, details, and
datasources.)

"Data keys must be specified on GridView 'gvAddresses' before the
selected data keys can be retrieved. Use the DataKeyNames property to
specify data keys."

I would try, and have tried, adding the DataKeyNames, but I can't seem
to figure out what it wants there. Once I add the property it lets the
page load, as long as I don't pass the customerid query string. I
tried ADDRESS_ID and CUSTOMER_ID but got this error:
"DataBinding: 'System.Data.DataRowView' does not contain a property
with the name 'ADDRESS_ID'."

ADDRESS_ID is the primary key, and CUSTOMER_ID is a foreign key.

When I test the SQL, it returns the expected rows.

Here's my code of the grid view, it's sql data source, the details
view, and it's sql data source. It was all autogenerated the same way
as the rest of the gridview/detailsviews on the page.

<asp:GridView ID="gvAddresses" runat="server"
AutoGenerateColumns="False" DataSourceID="sdsAddresses">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="TYPE" HeaderText="TYPE"
SortExpression="TYPE" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="sdsAddresses" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$
ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [TYPE] FROM [ADDRESSES] WHERE ([CUSTOMER_ID]
= ?)">
<SelectParameters>
<asp:QueryStringParameter Name="CUSTOMER_ID"
QueryStringField="customerid" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView3" runat="server"
AutoGenerateRows="False" DataSourceID="sdsAddressDetails"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="STREET" HeaderText="STREET"
SortExpression="STREET" />
<asp:BoundField DataField="CITY" HeaderText="CITY"
SortExpression="CITY" />
<asp:BoundField DataField="STATE" HeaderText="STATE"
SortExpression="STATE" />
<asp:BoundField DataField="ZIP" HeaderText="ZIP"
SortExpression="ZIP" />
</Fields>
</asp:DetailsView>

<asp:SqlDataSource ID="sdsAddressDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$
ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [STREET], [CITY], [STATE], [ZIP] FROM
[ADDRESSES] WHERE ([ADDRESS_ID] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="gvAddresses"
Name="ADDRESS_ID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

I think that's everything there is to know... Thank in advance.

Cerebrus

unread,
Jan 21, 2009, 1:00:31 AM1/21/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
DataKeyNames requires the name(s) of the Primary Key column.

Rromero_Bolivia

unread,
Jan 21, 2009, 9:53:08 AM1/21/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
you will need to add the dataKeyNames attribute
(DataKeyNames="ADDRESS_ID, CUSTOMER_ID") :

<asp:GridView ID="gvAddresses" runat="server"
AutoGenerateColumns="False" DataSourceID="sdsAddresses" ==>
DataKeyNames="ADDRESS_ID, CUSTOMER_ID" <== >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="TYPE" HeaderText="TYPE"
SortExpression="TYPE" />
</Columns>
</asp:GridView>


Also change your source control to obtain the two values (edit your
SelectCommand):
<asp:SqlDataSource ID="sdsAddresses" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$
ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [ADDRESS_ID],[CUSTOMER_ID], [TYPE] FROM
[ADDRESSES] WHERE ([CUSTOMER_ID]
= ?)">


then in your event you can retrieve this

protected void gvAddresses_SelectedIndexChanges(object sender,
EventArgs e)
{

DataKey data = gvAddresses.SeleectedDataKey;
string PrimaryKey = (string)data.Values["ADDRESS_ID"];
string ForeignKey = (string)data.Values["CUSTOMER_ID"];
}

I hope that works
_____________________
Reply all
Reply to author
Forward
0 new messages