Frage

I created a user control in ASP.net C# with a Dropdownlist in it (called LookupGrid below). Now in the main form , I want to set the data source for the Combobox, but I am getting NullExceptions when on the line that sets the datasource for the combobox

Code in the main web form :

LookupGrid droplist = new LookupGrid();
droplist.ID = field.Name;
RecordSet lookupvalues = inRecs.ServiceClient.WebServiceClient.GetInstance().GetLookup("CurrentNRS_Admit", Convert.ToInt64(Session["UserID"].ToString()), field);
droplist.SetData(lookupvalues.ToDataSet());

In the user control :

protected void Page_Load(object sender, EventArgs e)
{

}
public void SetData(DataSet dTLookupValues)
{
    DropDownList1.DataSource = dTLookupValues.Tables[0];
}

I am getting error:

Object reference not set to an instance of an object.

On this line:

DropDownList1.DataSource = dTLookupValues.Tables[0];
War es hilfreich?

Lösung

create property and set the data table from the public method. in page load you can bind the data

public DataTable MyData { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList1.DataSource =MyData  ;
    DropDownList1.DataBind();
}

public void SetData(DataSet dTLookupValues)
{
    MyData  = dTLookupValues.Tables[0];
}

in your main page

   protected void Page_Load(object sender, EventArgs e)
    {
        var control = (LookupGrid )LoadControl("~/LookupGrid.ascx");
        control.SetData(lookupvalues.ToDataSet());
        Panel1.Controls.Add(control); //add control to your page or panel
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top