C# 数据绑定:“System.Data.DataRowView”不包含名为“Rating”的属性
这是我的Html标记C# 数据绑定:“System.Data.DataRowView”不包含名为“Rating”的属性,c#,asp.net,C#,Asp.net,这是我的Html标记 <form runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White
<form runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" DataKeyNames="FilePath" >
<Columns>
<asp:BoundField DataField="FileName" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<object type="application/x-shockwave-flash" data='dewplayer-vol.swf?mp3=<%#Eval("FilePath") %>'
width="240" height="20" id="dewplayer">
<param name="wmode" value="transparent" />
<param name="movie" value='dewplayer-vol.swf?mp3=<%#Eval("FilePath") %>' />
</object>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField><asp:TemplateField HeaderText="Song Ratings">
<ItemTemplate>
<asp:Rating ID="Rating1" OnChanged="OnRatingChanged" runat="server"
StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star"
FilledStarCssClass="FilledStar" CurrentRating='<%# Eval("Rating") %>'>
</asp:Rating>
</ItemTemplate>
</asp:TemplateField></Columns></asp:GridView></form>
//将用户评级插入并保存到SQL Server数据库表
protected void OnRatingChanged(object sender, RatingEventArgs e)
{
int rowIndex = ((sender as Rating).NamingContainer as GridViewRow).RowIndex;
int songId = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO SongRating VALUES(@SongId, @Rating)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@SongId", songId);
cmd.Parameters.AddWithValue("@Rating", e.Value);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
private void BindGridviewData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select * from SongTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
错误发生在哪里?我是asp.net的新手谢谢你hi@abhi你在选择列表中没有提到列评级,请执行以下操作 选择SongId,文件名,ISNULLAVGRating,0作为SongTable左侧的分级,在SongRating.SongId=SongTable.SongId组中按SongId,文件名加入SongRating
希望这能对你有所帮助。根据你的例外情况,上面说 当web中存在指定的数据项时,会发生上述异常 控件在返回的DataRowView的架构中不存在。在里面 也就是说,使用数据绑定指定的列 从数据库返回的数据中不存在控件 因此,在BindGridviewData方法中,可能会从此处更改Select查询 对此
SELECT SongId, FileName, ISNULL((SELECT AVG(Rating)
FROM SongRating WHERE SongId = SongTable.SongId), 0) as Rating FROM SongTable
对不起,我不明白。请指定查询。
select * from SongTable
SELECT SongId, FileName, ISNULL((SELECT AVG(Rating)
FROM SongRating WHERE SongId = SongTable.SongId), 0) as Rating FROM SongTable