DropDownList调用数据库的问题。
编辑、修改已经都没问题了。
但我现在发现一个问题,当我选择“添加一行”addrow按钮时,提示下面错误。
“/WebApplicationCSharp”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 268:
行 269:
行 270: DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
行 271: }
行 272:
 源文件: c:\wk\webapplicationcsharp\sales.aspx.cs    行: 270 看看我的ASPX代码:<asp:datagrid id="SALES"                          .........>
.......
<EditItemTemplate>
<asp:DropDownList id="CU_ID" runat="server" AutoPostBack="True" SelectedIndex='<%# DataBinder.Eval(Container.DataItem, "CU_ID")) %>'></asp:DropDownList></EditItemTemplate>
......
</asp:datagrid>看看我的CS代码。 public void addrow(object s,EventArgs e)
{    
DataTable dt;
DataRow dr;
dt=(DataTable)Session["MY_SALES"]; 
dr=dt.NewRow();
dt.Rows.Add(dr);
//更新内存的Dataset
Session["MY_SALES"]=dt;
Int32 Index;
Index=dt.DefaultView.Count;
this.SALES.DataSource=dt;
this.SALES.DataKeyField = "S_ID";
this.SALES.DataBind();
this.SALES.CurrentPageIndex=SALES.PageCount - 1;
this.SALES.DataBind(); //为添加的新行打开编辑模式
LBTN_Add.Visible = false;
this.SALES.EditItemIndex = SALES.Items.Count - 1;
this.SALES.DataBind();
} public void BindGridToSource()
{


OracleConnection conn=new OracleConnection("Data Source=WK;user id=wk;password=yourpassword");
string mySelectQuery = "SELECT * FROM V_SALES order by S_ID";
OracleDataAdapter myCommand = new OracleDataAdapter(mySelectQuery,conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "sales");
this.SALES.DataKeyField="S_ID";
DataTable  myDt= ds.Tables["sales"];
DataView dv = ds.Tables["sales"].DefaultView;;
Session["MY_SALES"]=myDt;
SALES.DataSource = myDt;
this.SALES.DataBind();
}


public void SALES_ItemDataBound(object sender, DataGridItemEventArgs e)
{ OracleConnection conn=new OracleConnection("Data Source=WK;user id=wk;password=yourpassword");
string mySelectQuery = "SELECT * FROM CUSTOMER";
OracleDataAdapter myCommand = new OracleDataAdapter(mySelectQuery,conn);
DataSet dr = new DataSet();
myCommand.Fill(dr, "customer");
if(e.Item.ItemType==ListItemType.EditItem)
{
           DropDownList DDLCU_ID=(DropDownList)e.Item.FindControl("CU_ID");
DDLCU_ID.DataSource = dr.Tables["customer"].DefaultView;
DDLCU_ID.DataTextField = "CU_NAME_CODE";
DDLCU_ID.DataValueField = "CU_ID";
DDLCU_ID.DataBind();
DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
}



}

解决方案 »

  1.   

    ListItemType.EditItem是在触发"edit"事件才有效的吧,databond就用ListItemType.Item
      

  2.   

    TO:sugarsupper(北欧神话) 不行,提示下面这个错误:行 235: {
    行 236:           DropDownList DDLCU_ID=(DropDownList)e.Item.FindControl("CU_ID");
    行 237: DDLCU_ID.DataSource = dr.Tables["customer"].DefaultView;
    行 238: DDLCU_ID.DataTextField = "CU_NAME_CODE";
    行 239: DDLCU_ID.DataValueField = "CU_ID";
      

  3.   

    你看你再DataGrid的databound事件里面写的
    DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
    但,你的DropDownList也是绑定生成的,你的DataBinder.Eval(e.Item.DataItem,"CU_ID"),知道数据源到底是那个么?
    所以第一要查看的是:DataBinder.Eval(e.Item.DataItem,"CU_ID")是不是你想要的呀!
      

  4.   

    不好意思,我的ASPX代码应该去掉SelectedIndex部份,就像下面这样:<asp:datagrid id="SALES"                          .........>
    .......
    <EditItemTemplate>
    <asp:DropDownList id="CU_ID" runat="server" AutoPostBack="True"></asp:DropDownList></EditItemTemplate>
    ......
    </asp:datagrid>
    我的意思是这样:这个代码编辑、修改已经都没问题了。
    但我添加新行并且打开编辑模式时,就发生错误(激活public void addrow)。如果我删除这行:DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
    那么添加新行就变成没有问题了,但是编辑、修改里的 SelectedIndex无法得到默认值。
      

  5.   

    你首先检查DataBinder.Eval(e.Item.DataItem,"CU_ID")值是什么?如果是你预期的结果,那我的猜测才不成立!分布调试呀,大哥!
      

  6.   

    看你的错误提示,我怀疑是DataBinder.Eval(e.Item.DataItem,"CU_ID")这里没有取出的值不对,你可以单独测试一下这句到底出来什么。
      

  7.   

    解决了,谢谢各位。我改成这样就OK了。谢谢。
    string keyid = this.SALES.DataKeys[e.Item.ItemIndex].ToString();
    if(keyid != null && keyid != "")
    {

    DDLCU_ID.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"CU_ID"))).Selected=true;
    }