![](/img/trans.png)
[英]DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'
[英]DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Rating'
这是我的 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" 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>
这是我的 aspx.cs 文件
绑定 ASP.Net GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
GridView1.DataSource = GetData("SELECT SongId, FileName, ISNULL((SELECT AVG(Rating) FROM SongRating WHERE SongId = SongTable.SongId), 0) Rating FROM SongTable");
GridView1.DataBind();
}
}
private static DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
//将用户评分插入并保存到 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的新手谢谢
嗨@abhi 你没有在你的选择列表中提到“评级”列,做这样的事情
SELECT SongId, FileName, ISNULL(AVG(Rating),0) as Rating from SongTable left 通过 SongId, FileName 加入 SongRating on SongRating.SongId = SongTable.SongId 组
希望能帮到你..
根据您的例外情况,它说
当web控件中指定的数据项在返回的DataRowView的schema中不存在时,就会出现上述异常。 换句话说,您使用数据绑定控件指定的列不存在于从数据库返回的数据中
所以可能在BindGridviewData方法中更改您的选择查询
select * from SongTable
对此
SELECT SongId, FileName, ISNULL((SELECT AVG(Rating)
FROM SongRating WHERE SongId = SongTable.SongId), 0) as Rating FROM SongTable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.