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;
}
}
编辑、修改已经都没问题了。
但我现在发现一个问题,当我选择“添加一行”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;
}
}
解决方案 »
- 如何显示html?
- 求助,asp.net中将office文件转成pdf,并加上水印,现在水印加不上
- 急,50分在线等,解决马上结帖,数据库表中只有一个字段,怎么用GridView实现修改更新操作
- 100 分 C# 2005 ASP.NET 为什么我看不到自己写得类
- .net发邮件问题(急急急急)
- 求上传图片到数据库的保存方式问题,很简单,谢谢!
- JQuery函数 如何在服务器端判断条件启用
- TextBox的MultiLine为True,如何取得每一行的文本内容,在线等待
- 有没有如何实现blog的文章?或者自助建站方面的.谢谢.给点意见也可以了.
- 【小白求助】想自己建个网站,只会asp.net应该怎么做呢
- 请高手过来看,这样配制Access数据库连接方式难道有错吗?
- 大家来想这样写数据库的操作是否有问题!
行 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";
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")是不是你想要的呀!
.......
<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无法得到默认值。
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;
}