By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

Help Me~~My DropDownList Can not get the"BigClassList.SelectedItem.Text" there is an error: System.NullReferenceException

P: n/a

I have many dropdownlist controls in my ascx (and use LoadControl in a aspx fiel) file,so i write a method "InitList(DropDownList list,string strsql,string TextField,string ValueField)"
but when i want to get the BigClassList.SelectedItem.Text and BigClassList.SelectedItem.Value in a button onclick method,btnSmallClassEdit_Click(....),there post an error " System.NullReferenceException: 未将对象引用设置到对象的实例。",

I Don't know the wrong is where in my code, pls help me thanks!!!
I guess the BigClassList.SelectedItem.Text and BigClassList.SelectedItem.Value may be null
///////a part of my codes here//////////////////////////////////////////////////////
///My connection defined in the parent page which is inherits from the class "Page";
// And i make sure the type, and ID matches to the HTML, and HTML declaration of BigClassList has a
// runat=server attribute and in a "<form runat=server>" tags .
////////////////////////////////////////////////////////////////////////////////////
public void Page_Load(Object sender,EventArgs e)

{

if(!IsPostBack)

{
BigClassDropDownListInit();
}
}
public void BigClassDropDownListInit()
{
InitList(BigClassList,"select ID,BigClassName from RootClass order by BigClassName","BigClassName","ID");
}
public void InitList(DropDownList list,string strsql,string TextField,string ValueField)
{
Conn.Open();
Cmd =new OleDbCommand(strsql,Conn);
OleDbDataReader Rd = Cmd.ExecuteReader();
list.DataSource=Rd;
list.DataTextField=TextField;
list.DataValueField=ValueField;
list.DataBind();
Rd.Close();
Conn.Close();
}
public void btnSmallClassAdd_Click(Object sender,EventArgs e)
{
Conn.Open();
OleDbCommand commTest=new OleDbCommand("select * from Type where TypeName='"+txtSmallClassAdd.Text+"'",Conn);
int i=commTest.ExecuteNonQuery();
Conn.Close();
if(i>0)
Response.Write("<script>alert('已有同名分类存在!') ;</script>");
else
{
string strInsert="insert into Type(TypeName,BigClassName,BigID) Values ('"+txtSmallClassAdd.Text+"','"+BigClassList.Selec tedItem.Text+"','"+BigClassList.SelectedItem.Value +"')";

Conn.Open();
OleDbCommand commAdd=new OleDbCommand(strInsert,Conn);
commAdd.ExecuteNonQuery();
Conn.Close();
Response.Write("<script>alert('二级分类添加成功!') ;</script>");
}
BigClassDropDownListInit();
SmallClassDropDownListInit();
}
////////////////about the ascx fiels code here//
<td width="80%" valign="middle" bgcolor="#FFFFFF"><font color="#000000">
 在:</font> <asp:dropdownlist ID="BigClassList" Width="100" runat="server" ></asp:dropdownlist> <font color="#000000">中增加二级栏目 </font> <asp:textbox ID="txtSmallClassAdd" Width="100" runat="server" ></asp:textbox> <asp:button ID="btnSmallClassAdd" OnClick="btnSmallClassAdd_Click" Text="添加" runat="server" ></asp:button> </td>
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.