'System.Data.DataRowView' does not contain a property with the name 'ID'.

M

Monty

[ASP.Net 2005]

Hi, I have a grid like so:
<asp:GridView ID="grdItemActivity" runat="server" ><Columns>

<asp:BoundField DataField="TransactionType" HeaderText="Type"
ReadOnly="True" />

<asp:BoundField DataField="DocumentNumber" HeaderText="Doc. No."
ReadOnly="True" />

<asp:BoundField DataField="Location" HeaderText="Location" ReadOnly="True"
/>

<asp:BoundField DataField="Quantity" HeaderText="Qty" ReadOnly="True" />

<asp:BoundField DataField="QuantityOnOrder" HeaderText="Committed"
ReadOnly="True" />

<asp:BoundField DataField="AmountPerUnit" HeaderText="Cost/Unit"
ReadOnly="True" />

<asp:BoundField DataField="DateAdded" HeaderText="Added" ReadOnly="True" />

<asp:BoundField DataField="DateModified" HeaderText="Modified"
ReadOnly="True" />

</Columns></asp:GridView>

Which I bind like so:

Dim oDS As System.Data.DataSet = Item.GetItemActivity(miItemID, Param1,
Param2)
grdItemActivity.DataSource = oDS '[have also tried: oDS.Tables(0) ]
grdItemActivity.DataBind()

On the DataBind(), I receive the error message "System.Data.DataRowView'
does not contain a property with the name 'ID'." That is correct, the
results don't have a field called "ID", but I'm not referencing any field
called ID in my GridView. WTH? I also tried removing ALL columns and setting
autogeneratecolumns = true, but I still get the same exact error. What am I
missing? TIA.
 
W

Walter Wang [MSFT]

Hi Monty,

Please check if your GridView's declaration has specified property
"DataKeyNames", for example:

<asp:GridView ID="GridView1" DataKeyNames="ID" runat="server" ...


The fields specified in DataKeyNames will also be retrieved from data
source.

If this is not the case, I might need more code or a complete reproducible
project to further test this. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Monty

When I first created the grid I'd copied it from another grid and it did
have the value "DataKeyNames=ID", so I tried changing it to the name of the
correct key field (DataKeyNames="ItemID") and it gave me the same error
message, looking for the field "ID" (not "ItemID"). So I deleted the grid
and started from scratch (with nothing specified for the DataKeyNames
property) and still got the same error. So I removed all the columns and set
AutoGenerateColumns=True, like so:

<asp:GridView ID="grdItemActivity" runat="server" AutoGenerateColumns=true
/>

In this case it would run without error, but there was no grid created. So I
interrogated the DataSet object in the immediate pane at the time of
databinding to make sure it had values, and it does:

?oDS.Tables(0).Rows.Count
25
?oDS.Tables(0).Rows(3).Item(0)
3 {Integer}
?oDS.Tables(0).Rows(3).Item(2)
8 {Integer}
?oDS.Tables(0).Rows(3).Item(5)
20D {Decimal}

I also tried setting the datasource of the GridView to the specific Data
Table, but no luck:
grdItemActivity.DataSource = oDS.Tables(0)

The code for the binding looks like so:

Dim oDS As System.Data.DataSet = Item.GetItemActivity(miItemID, P1, P2)
grdItemActivity.DataSource = oDS '[also tried: oDS.Tables(0) ]
grdItemActivity.DataBind()

I could try using a DataSourceObject, but since this is just a read-only
list of items I was trying to avoid the overhead involved with it.

Thanks Walter.
 
M

Monty

Also note that if I do it as a repeater control rather than a gridview, it
works fine. It just seems to be with a GridView...

This works (though I'd rather have a gridview):

<asp:Repeater ID="Repeater1" runat="server"
Visible="true">
<HeaderTemplate>
<table>
<tr>
<th>Type</th>
<th>Doc. No.</th>
<th>Location</th>
<th>Qty</th>
<th>Created</th>
<th>Modified</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("TransactionType") %></td>
<td><%#Eval("DocumentNumber") %></td>
<td><%#Eval("Location") %></td>
<td align="right"><%#Eval("Quantity",
"{0:c}") %></td>
<td><%#Eval("DateAdded") %></td>
<td><%#Eval("DateModified") %></td>
</tr>
</ItemTemplate>
<footertemplate>
</table>
</footertemplate>
</asp:Repeater>
 
W

Walter Wang [MSFT]

Hi Monty,

Unless you're substituting the built-in GridView control with a custom
GridView control in web.config via configuration such as:


<pages>
<tagMapping>
<add tagType="System.Web.UI.WebControls.GridView"
mappedTagType="myns.EmptyGridView,App_Code"/>
</tagMapping>
</pages>
</system.web>


I must say this is very strange since your code looks very clean and
simple.

Can you create a new WebForm, add a GridView, then bind it to a manually
created DataTable to see if it works:

DataTable dt = new DataTable();
dt.Columns.Add("Code");
dt.Rows.Add("code1");
GridView1.DataSource = dt;
GridView1.DataBind();


I'm also wondering if it can be reproduced on my side. Would you please
send me a copy of the code? Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Monty

Well, I created a new project, added a new grid, and copied over the
procedure that retrieves the data from the database and... it worked
perfectly. Same procedure, same database, everthing. So, I created a new,
simple blank page ~in my existing project~ and tried again and got the same
error. If I don't specify any columns and set autogeneratecolumns=true then
I do not receive an error, but there is no grid created. If I specify even a
single column (like below), I get this error message:

DataBinding: 'System.Data.DataRowView' does not contain a property with the
name 'ID'.

That's correct, there is no "ID" column, but who is looking for one?

<asp:GridView ID="GridView1" runat="server" DataKeyNames="ItemID" >
<Columns>
<asp:BoundField DataField=DateModified />
</Columns>
</asp:GridView>

Anyway, I've been able to jerry-rig a grid by using a repeater control, and
I think I'm just going to have to run with that for a while... unless you
have any last thoughts on it? Thanks.
 
M

Monty

OK, I think I figured it out. In my Theme/Skin for the project, I've defined
a gridview (template) with css style tags, etc. One of the attributes set on
that gridview in my skin is 'DataKeyNames="ID"'. I guess the property in the
Skin is not being overridden by the property set on the actual GridView.
Shouldn't it be?
 
W

Walter Wang [MSFT]

Hi Monty,

Thanks very much for your information. I haven't thought about the skin
files.

According to the MSDN library:

#ASP.NET Themes and Skins Overview
http://msdn2.microsoft.com/en-us/library/ykzx33wh(VS.80).aspx
<quote>
If a control setting is defined in both the control and the theme, the
control settings from the theme override any page settings on the control.
This strategy enables the theme to create a consistent look across pages,
even if controls on the pages already have individual property settings.
</quote>

This means the setting in a skin file will override setting on the control.

Thanks again for sharing your experience here. This will sure benefit the
community.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

becky bloomwood

Hi, I am also encountering the same problem too. I too look at the skin.css but it nevers mention anything. I am still getting that error.In the grid view, I named the Datakeysname as: termsID(exact as my database col) as I need to make use of this such that when I insert a new records into the database,it will automatically increment. And i also needs it for delete and updating of records since my termsID will not be displayed in the grid view.May i know how to resolve it? It gt to do with my stored procedure and database.cs? I am using C# btw,thks!
[ASP.Net 2005]

Hi, I have a grid like so:
<asp:GridView ID="grdItemActivity" runat="server" ><Columns>

<asp:BoundField DataField="TransactionType" HeaderText="Type"
ReadOnly="True" />

<asp:BoundField DataField="DocumentNumber" HeaderText="Doc. No."
ReadOnly="True" />

<asp:BoundField DataField="Location" HeaderText="Location" ReadOnly="True"
/>

<asp:BoundField DataField="Quantity" HeaderText="Qty" ReadOnly="True" />

<asp:BoundField DataField="QuantityOnOrder" HeaderText="Committed"
ReadOnly="True" />

<asp:BoundField DataField="AmountPerUnit" HeaderText="Cost/Unit"
ReadOnly="True" />

<asp:BoundField DataField="DateAdded" HeaderText="Added" ReadOnly="True" />

<asp:BoundField DataField="DateModified" HeaderText="Modified"
ReadOnly="True" />

</Columns></asp:GridView>

Which I bind like so:

Dim oDS As System.Data.DataSet = Item.GetItemActivity(miItemID, Param1,
Param2)
grdItemActivity.DataSource = oDS '[have also tried: oDS.Tables(0) ]
grdItemActivity.DataBind()

On the DataBind(), I receive the error message "System.Data.DataRowView'
does not contain a property with the name 'ID'." That is correct, the
results don't have a field called "ID", but I'm not referencing any field
called ID in my GridView. WTH? I also tried removing ALL columns and setting
autogeneratecolumns = true, but I still get the same exact error. What am I
missing? TIA.
On Tuesday, August 21, 2007 4:41 AM wawan wrote:
Hi Monty,

Please check if your GridView's declaration has specified property
"DataKeyNames", for example:

<asp:GridView ID="GridView1" DataKeyNames="ID" runat="server" ...


The fields specified in DataKeyNames will also be retrieved from data
source.

If this is not the case, I might need more code or a complete reproducible
project to further test this. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
On Tuesday, August 21, 2007 10:59 AM Monty wrote:
When I first created the grid I'd copied it from another grid and it did
have the value "DataKeyNames=ID", so I tried changing it to the name of the
correct key field (DataKeyNames="ItemID") and it gave me the same error
message, looking for the field "ID" (not "ItemID"). So I deleted the grid
and started from scratch (with nothing specified for the DataKeyNames
property) and still got the same error. So I removed all the columns and set
AutoGenerateColumns=True, like so:

<asp:GridView ID="grdItemActivity" runat="server" AutoGenerateColumns=true
/>

In this case it would run without error, but there was no grid created. So I
interrogated the DataSet object in the immediate pane at the time of
databinding to make sure it had values, and it does:

?oDS.Tables(0).Rows.Count
25
?oDS.Tables(0).Rows(3).Item(0)
3 {Integer}
?oDS.Tables(0).Rows(3).Item(2)
8 {Integer}
?oDS.Tables(0).Rows(3).Item(5)
20D {Decimal}

I also tried setting the datasource of the GridView to the specific Data
Table, but no luck:
grdItemActivity.DataSource = oDS.Tables(0)

The code for the binding looks like so:

Dim oDS As System.Data.DataSet = Item.GetItemActivity(miItemID, P1, P2)
grdItemActivity.DataSource = oDS '[also tried: oDS.Tables(0) ]
grdItemActivity.DataBind()

I could try using a DataSourceObject, but since this is just a read-only
list of items I was trying to avoid the overhead involved with it.

Thanks Walter.


news:[email protected]...
 
Joined
Jul 26, 2011
Messages
1
Reaction score
0
solve my problem

hi i get an error like this
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'dept_name'.

my code is


protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=softpro-srv;database=wintest;uid=testdb;password=T3stdb");

SqlDataAdapter da = new SqlDataAdapter("select dept_name from dept3 where dept_name='Hr'union all select dept_name from dept3 where dept_name='accounts'", con);

DataSet ds = new DataSet();
da.Fill(ds);


GridView1.DataSource = ds;

GridView1.DataBind();

}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView Gv = (GridView)e.Row.FindControl("GridView2");
SqlConnection con = new SqlConnection("server=softpro-srv;database=wintest;uid=testdb;password=T3stdb");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string data = Convert.ToString(((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString());
SqlDataAdapter da1 = new SqlDataAdapter("select emp3.emp_name from emp3 join dept3 on emp3.dept_id=dept3.dept_id where dept3.dept_name='" +data+ "'", con);
DataSet ds2 = new DataSet();
da1.Fill(ds2);
Gv.DataSource = ds2;
Gv.DataBind();

}



}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Departmentwise Employee Datails.aspx.cs" Inherits="Departmentwise_Employee_Datails" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script language="javascript" type="text/javascript">
function switchViews(obj, row)

{
var div = document.getElementById(obj);

var img = document.getElementById('img' + obj);
if (div.style.display == "none") {

div.style.display = "inline";
if (row == 'alt')

{


img.src = 'Plus.jpg';

}
else {


img.src = 'minus.jpg';

}
img.alt = 'Close';

}

else

{
div.style.display = "none";if (row == 'alt')

{
img.src = 'minus.jpg';

}

else {
img.src = 'Plus.jpg';

}
img.alt = 'Expand';

}

}

</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
style="z-index: 1; left: 225px; top: 144px; position: absolute; width: 310px; height: 59px;"
onrowdatabound="GridView1_RowDataBound" ShowHeader="false"
ShowFooter="false" ShowHeaderWhenEmpty="True" GridLines="Horizontal"
CellPadding="5" CellSpacing="2"
onselectedindexchanged="GridView1_SelectedIndexChanged"
DataKeyNames="dept_name" >
<Columns>
<asp:TemplateField>

<ItemTemplate>

<a href="javascript:switchViews('div<%# Eval("dept_name") %>','one' )">

<img id="imgdiv<%# Eval("dept_name") %>" alt="open" src="Plus.jpg" height="15" width="15" />

</a>

</ItemTemplate>

</asp:TemplateField>
<asp:BoundField DataField="dept_name" HeaderText="dept_name" />

<asp:TemplateField HeaderText="dept_name">

<ItemTemplate>
<asp:Label ID="lbldeptid" runat="server" Text="<%# Bind('dept_name') %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>

<ItemTemplate>

<div id ="div<%# Eval("dept_name") %>" style="display:none;position:relative;">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" ShowHeader="false" ShowFooter="false" BorderStyle="None" GridLines="None" DataKeyNames="dept_name" >
<Columns>
<asp:BoundField DataField="emp_name" />
</Columns>
</asp:GridView>
</div>


</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbltotalnoofemployees" runat="server" ForeColor="Brown" Text="Total Number of employees:" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="false" ShowHeader="false" ShowFooter="false" BorderStyle="None" GridLines="None" DataSourceID="SqlDataSource3">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblcount" runat="server" Text="<%# Bind('count') %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:wintestConnectionString2 %>"
SelectCommand="select count(*) as count from emp3 join dept3 on emp3.dept_id=dept3.dept_id where dept3.dept_name=@deptname">
<SelectParameters>
<asp:SessionParameter Name="deptname" SessionField="deptname" Type="string"/>
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top