0
我的任務有些問題。當我在這裏編輯數據時,我嘗試爲GridView中的dropdownlist設置值。但是當我將作業發送給我的朋友時,他告訴我他可以無誤地運行它(他使用Visual Studio 2010)。我使用Visual Studio 2008和2010,但我無法運行我的代碼。請幫幫我。設置下拉列表的值
SqlConnection cn=new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cn"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
LoadProduct();
}
}
public DataSet LoadCategory()
{
SqlDataAdapter da = new SqlDataAdapter("select * from categories", cn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
private void LoadProduct()
{
//throw new NotImplementedException();
SqlDataAdapter da=new SqlDataAdapter("select * from products,categories where products.categoryID=categories.categoryID",cn);
DataTable db=new DataTable();
da.Fill(db);
GridView1.DataSource=db;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadProduct();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Literal ltr=GridView1.Rows[e.NewEditIndex].FindControl("Literal1") as Literal;
string categoryname = ltr.Text;
GridViewRow row=GridView1.Rows[e.NewEditIndex];
LoadProduct();
DropDownList ddl = row.Cells[3].Controls[1] as DropDownList;
ddl.DataSource = LoadCategory().Tables[0];// when I run there is a error. "Object reference not set to an instance of an object"
ddl.DataTextField = "categoryName";
ddl.DataValueField = "categoryID";
ddl.DataBind();
ddl.Items.FindByText(categoryname).Selected = true;
}
爲什麼你不能運行呢?請給我們例外。 – Amy
作業?這是功課嗎? – jwiscarson