Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ddl Question

Status
Not open for further replies.

quinnipiac0807

Programmer
Oct 25, 2004
38
US
private bool LoadDepartment()
{
SqlConnection Conn = new SqlConnection(Global.ConnectionString);
Conn.Open();
//Populate Department DDL
string sql = "SELECT ITDivisionID, ITDivShortName + ' - ' + itdivision as ITDivision " +
"FROM psd_ITDivision " +
"ORDER BY ITDivShortName ";
SqlCommand Cmd = new SqlCommand(sql, Conn);
SqlDataReader DR = Cmd.ExecuteReader();

ddlDepartment.DataValueField = "ITDivisionID";
ddlDepartment.DataTextField = "ITDivision";
ddlDepartment.DataSource = DR;
ddlDepartment.DataBind();

ddlDepartment.Items.Insert(0, "Please Select Department.");
ddlDepartment.Items[0].Value = "";

DR.Close();
Cmd.Dispose();
Conn.Close();
return true;
}

For some reason on this line (ddlDepartment.DataValueField = "ITDivisionID";) I get this error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 207: SqlDataReader DR = Cmd.ExecuteReader();
Line 208:
Line 209: ddlDepartment.DataValueField = "ITDivisionID";
Line 210: ddlDepartment.DataTextField = "ITDivision";
Line 211: ddlDepartment.DataSource = DR;


Source File: c:\inetpub\ Line: 209

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
Usis.briefCASE.itd.itdITDivision.LoadDepartment() in c:\inetpub\ Usis.briefCASE.itd.itdITDivision.Page_Load(Object sender, EventArgs e) in c:\inetpub\ System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731

Any Suggestions?
 
where do you create the ddlDepartment dropdown?? do you create it at all? (and i mean instantiate, not just declare)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
First off...

ddlDepartment.Items.Insert("Please Select Department.",0);

First will set DataTextField, second will set DataValueField

Next, is your DDL within a DataGrid? If so, do...
DropDownList myDDL = datagrid.items[0].FindControl["ddlDepartment"]
myDDL.DataValueField = "ITDivisionID"
....

If not, be sure youve set the aspx(html) code correctly...

<asp:DropDownList id=ddlDepartment runat=server DataTextField=ITDivision DataValueField=ITDivisionID />

post a reply with what you found out please.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top