DataBinding:“System.Data.DataRowView”不包含名為“vcompanyname”的屬性。
百度的大多數答案的意思是查詢生成的ds中沒有vcompanyname字段,但我的查詢語句確實是有的,有還人說的session的問題,但沒具體解釋原因或者解決方案。
問題具體如下:
表1 公司表,從公司表的聯系人,鏈接到聯系人對應的表2 聯系人信息表。vcompanyname均不是2表主鍵。
<asp:HyperLinkField HeaderText="聯系人" DataNavigateUrlFields="vcompanyname" DataNavigateUrlFormatString="~/companycontrast/List.aspx?id={0}"
Text="聯系人" />
表2的gridview是動軟生成的,這部分代碼在生成表1時用過,是可用的,但在表2時出問題了。
Maticsoft.BLL.companycontrast bll = new Maticsoft.BLL.companycontrast();
public string strid = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
gridView.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
gridView.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());
btnDelete.Attributes.Add("onclick", "return confirm(\"你確認要刪除嗎?\")");
if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
{
strid = Request.Params["id"];
}
BindData(strid.Trim());
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
BindData(strid.Trim());
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string idlist = GetSelIDlist();
if (idlist.Trim().Length == 0)
return;
bll.DeleteList(idlist);
BindData(strid.Trim());
}
#region gridView
public void BindData(string vcompanyname)
{
#region
//if (!Context.User.Identity.IsAuthenticated)
//{
// return;
//}
//AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
//if (user.HasPermissionID(PermId_Modify))
//{
// gridView.Columns[6].Visible = true;
//}
//if (user.HasPermissionID(PermId_Delete))
//{
// gridView.Columns[7].Visible = true;
//}
#endregion
DataSet ds = new DataSet();
/* StringBuilder strWhere = new StringBuilder();
if (txtKeyword.Text.Trim() != "")
{
#warning 代碼生成警告:請修改 keywordField 為需要匹配查詢的真實字段名稱
strWhere.AppendFormat("keywordField like '%{0}%'", txtKeyword.Text.Trim());
} */
string strWhere = "vcompanyname = '" + vcompanyname + "'";
ds = bll.GetList(strWhere.ToString());
gridView.DataSource = ds;
gridView.DataBind();
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
BindData(strid.Trim());
}
protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
}
}
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("style", "background:#FFF");
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
linkbtnDel.Attributes.Add("onclick", "return confirm(\"你確認要刪除嗎\")");
//object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
//if ((obj1 != null) && ((obj1.ToString() != "")))
//{
// e.Row.Cells[1].Text = obj1.ToString();
//}
}
}
protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//#warning 代碼生成警告:請檢查確認真實主鍵的名稱和類型是否正確
//int ID = (int)gridView.DataKeys[e.RowIndex].Value;
//bll.Delete(ID);
//gridView.OnBind();
}
private string GetSelIDlist()
{
string idlist = "";
bool BxsChkd = false;
for (int i = 0; i < gridView.Rows.Count; i++)
{
CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
if (ChkBxItem != null && ChkBxItem.Checked)
{
BxsChkd = true;
//#warning 代碼生成警告:請檢查確認Cells的列索引是否正確
if (gridView.DataKeys[i].Value != null)
{
idlist += gridView.DataKeys[i].Value.ToString() + ",";
}
}
}
if (BxsChkd)
{
idlist = idlist.Substring(0, idlist.LastIndexOf(","));
}
return idlist;
}
#endregion
}
代碼運行到 gridView.DataBind();時報錯。
單步跟蹤到這裡時:
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
無法查看返回數據集ds中數據,但所涉及所有字段數據庫中均存在,測試查詢內容在數據庫中也存在。
我自己調試解決了。
我的還是字段的問題
綁定的關鍵字段,在查詢條件中必須有,並且字符形式保持一致。
我的關鍵字是vcompanyname,所以select語句中必須有vcompanyname .(原來vcompanyname不是我需要的查詢項)。
注意字段屬性中datafield和sortexpression的區別與聯系。