box =new CheckBoxList();
string strcon2=System.Configuration.ConfigurationSettings.AppSettings["strCon"];
SqlConnection cn=new SqlConnection(strcon2);
DataSet ds=new DataSet(); if(this.paper_topics.SelectedValue=="C")
{
string query1="select * from Class where C_str='C'";
SqlCommand cmd=new SqlCommand(query1,cn);
cmd.Connection.Open();
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Session["leibie"]=dr["C_ID"].ToString(); }
cmd.Connection.Close(); string query="select * from Class where C_fatherID='1'";
SqlDataAdapter da=new SqlDataAdapter(query,cn);
da.Fill(ds,"linshi");
box.DataSource=ds.Tables["linshi"];
box.DataTextField=ds.Tables["linshi"].Columns["C_name"].ToString();
box.DataValueField=ds.Tables["linshi"].Columns["C_ID"].ToString();
box.DataBind();
Session["path"]="C";
}
if(this.paper_topics.SelectedValue=="D")
{
string query1="select * from Class where C_str='D'";
SqlCommand cmd=new SqlCommand(query1,cn);
cmd.Connection.Open();
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Session["leibie"]=dr["C_ID"].ToString();
}
cmd.Connection.Close(); string query="select * from Class where C_fatherID='2'";
SqlDataAdapter da=new SqlDataAdapter(query,cn);
da.Fill(ds,"linshi");
box.DataSource=ds;
box.DataTextField=ds.Tables["linshi"].Columns["C_name"].ToString();
box.DataValueField=ds.Tables["linshi"].Columns["C_ID"].ToString();
box.DataBind();
Session["path"]="D";
}
box.RepeatDirection=RepeatDirection.Vertical;
box.ForeColor=Color.Orange;
box.ID="box1";
this.Panel1.Controls.Add(box);
我想初始化几个选项,我是这么写的
this.box.Items.FindByText(dr6["P_childclass1"].ToString()).Selected=true;
dr6["P_childclass1"].ToString()).这个为数据库里读出的数据,是正确的,但是这样写运行的时候总是说
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 74: if(dr6["P_childclass2"].ToString()=="")
行 75: {
行 76: box.Items.FindByText(dr6["P_childclass1"].ToString()).Selected=true;
行 77: }
行 78: else
请问是怎么回事啊
string strcon2=System.Configuration.ConfigurationSettings.AppSettings["strCon"];
SqlConnection cn=new SqlConnection(strcon2);
DataSet ds=new DataSet(); if(this.paper_topics.SelectedValue=="C")
{
string query1="select * from Class where C_str='C'";
SqlCommand cmd=new SqlCommand(query1,cn);
cmd.Connection.Open();
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Session["leibie"]=dr["C_ID"].ToString(); }
cmd.Connection.Close(); string query="select * from Class where C_fatherID='1'";
SqlDataAdapter da=new SqlDataAdapter(query,cn);
da.Fill(ds,"linshi");
box.DataSource=ds.Tables["linshi"];
box.DataTextField=ds.Tables["linshi"].Columns["C_name"].ToString();
box.DataValueField=ds.Tables["linshi"].Columns["C_ID"].ToString();
box.DataBind();
Session["path"]="C";
}
if(this.paper_topics.SelectedValue=="D")
{
string query1="select * from Class where C_str='D'";
SqlCommand cmd=new SqlCommand(query1,cn);
cmd.Connection.Open();
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Session["leibie"]=dr["C_ID"].ToString();
}
cmd.Connection.Close(); string query="select * from Class where C_fatherID='2'";
SqlDataAdapter da=new SqlDataAdapter(query,cn);
da.Fill(ds,"linshi");
box.DataSource=ds;
box.DataTextField=ds.Tables["linshi"].Columns["C_name"].ToString();
box.DataValueField=ds.Tables["linshi"].Columns["C_ID"].ToString();
box.DataBind();
Session["path"]="D";
}
box.RepeatDirection=RepeatDirection.Vertical;
box.ForeColor=Color.Orange;
box.ID="box1";
this.Panel1.Controls.Add(box);
我想初始化几个选项,我是这么写的
this.box.Items.FindByText(dr6["P_childclass1"].ToString()).Selected=true;
dr6["P_childclass1"].ToString()).这个为数据库里读出的数据,是正确的,但是这样写运行的时候总是说
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 74: if(dr6["P_childclass2"].ToString()=="")
行 75: {
行 76: box.Items.FindByText(dr6["P_childclass1"].ToString()).Selected=true;
行 77: }
行 78: else
请问是怎么回事啊
解决方案 »
- Response 方法为什么不输出附件,却直接在页面上输出来了
- 请教一个.net的一般处理程序的问题
- 获取客户端.net版本
- ASP.NETGridView的RowDeleting事件
- 为什么app_code中的类在本地可以,但传到服务器上就出错找不到类.急急~~~~~~~~
- 有谁知道asp.net开发的网页能通过机顶盒在电视上浏览吗?
- 请问在ASP.NET中如何把光标定位到一个textbox上?
- 在DataList控件如何实现文本提示功能?
- json格式的转换
- 高分通缉微软的dotnet官方培训课程(MOC)的资料
- 怎么判断多个复选框是否被选中呢++++100送
- ------------------如何用IFRAME传数据来判断用户输入的MAIL是否重复---------------------
报错吗? 那就是选项没找到
改成下面这样一跟踪调试就明白了
ListItem tmp = box.Items.FindByText(dr6["P_childclass1"].ToString());
if(tmp!=null)
tmp.Selected = true;