Link to home
Start Free TrialLog in
Avatar of stellyuk
stellyuk

asked on

Problem with DropDownList

I have this code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PC();
        }
    }

    private void PC()
    {
        int SID = Int32.Parse(Request.QueryString["SID"].ToString());

        if (SID > 0)
        {
            DataTable tbl = PubQrys.SelectedSctr(SID);

            if (tbl.Rows.Count == 1)
            {
                SctrStat Status = PubQrys.GetSctrStat(SID);

                DropDownList StatDdl = (DropDownList)SctrFrmVw.FindControl("StatDdl");

                StatDdl.Items.Add(new ListItem("Please Select", "0"));
                StatDdl.Items.Add(new ListItem("With Client", "1"));
                StatDdl.Items.Add(new ListItem("Stored", "2"));
                StatDdl.Items.Add(new ListItem("Written Off", "3"));
                StatDdl.SelectedValue = Status.sStatus.ToString();

                SctrFrmVw.PagerSettings.Visible = false;
                SctrFrmVw.DataSource = tbl;
                SctrFrmVw.DataBind();
            }
        }
        else
        {
            SctrFrmVw.DataSource = PubQrys.GetAllSctrs();
            SctrFrmVw.DataBind();
        }
    }

I keep getting this error:
Object reference not set to an instance of an object

on this line:
StatDdl.Items.Add(new ListItem("Please Select", "0"));

I think its a problem with this line:
DropDownList StatDdl = (DropDownList)SctrFrmVw.FindControl("StatDdl");

As even when I try and reference a Label with the following code:
Label StatLbl = (Label)SctrFrmVw.FindControl("StatLbl");
StatLbl.Text = "BOB";

I get the same error on the StatLbl.Text = "BOB"; line

Any ideas?

Stelly
Avatar of divinewind80
divinewind80

I would suggest that you are attempting to refer to the dropdownlist before it has been rendered.  The PageLoad event happens before any controls that are contained on the page are created.  Try switching your section of code adding the items to a OnLoad event for the control itself.  

Let me know if you have questions.
Avatar of Munawar Hussain
Seems you are unable to find the controls ..
is it possible to post code for aspx?
thanks
Hi, post your aspx to see how are your controls declared.

Btw when Page_Load is called the controls have already been rendered to the page so using that event shouldn't be a problem.
Avatar of stellyuk

ASKER

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_SctrDets : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PC();
        }
    }

    private void PC()
    {
        int SID = Int32.Parse(Request.QueryString["SID"].ToString());

        if (SID > 0)
        {
            DataTable tbl = PubQrys.SelectedSctr(SID);

            if (tbl.Rows.Count == 1)
            {
                SctrStat Status = PubQrys.GetSctrStat(SID);

                DropDownList StatDdl = (DropDownList)SctrFrmVw.FindControl("StatDdl");

                StatDdl.Items.Add(new ListItem("Please Select", "0"));
                StatDdl.Items.Add(new ListItem("With Client", "1"));
                StatDdl.Items.Add(new ListItem("Stored", "2"));
                StatDdl.Items.Add(new ListItem("Written Off", "3"));
                StatDdl.SelectedValue = Status.sStatus.ToString();

                SctrFrmVw.PagerSettings.Visible = false;
                SctrFrmVw.DataSource = tbl;
                SctrFrmVw.DataBind();
            }
        }
        else
        {
            SctrFrmVw.DataSource = PubQrys.GetAllSctrs();
            SctrFrmVw.DataBind();
        }
    }

    protected void InfoBtn_Click(object sender, EventArgs e)
    {
        string value = Request.QueryString["SID"].ToString();

        string xScript = "<script language='javascript'>window.open('SctrInfo.aspx?SID=" + value + "',name='_blank','scrollbars=1,width=720,HEIGHT=450');</script>";
        Page.RegisterStartupScript("Scooter Information", xScript);
    }
    protected void UpdateTxtBx_Click(object sender, EventArgs e)
    {
        int value = Int32.Parse(Request.QueryString["SID"].ToString());

        TextBox RegNoTxtBx = (TextBox)SctrFrmVw.FindControl("RegNoTxtBx");
        TextBox MakeTxtBx = (TextBox)SctrFrmVw.FindControl("MakeTxtBx");
        TextBox MdlTxtBx = (TextBox)SctrFrmVw.FindControl("MdlTxtBx");
        TextBox DateRegTxtBx = (TextBox)SctrFrmVw.FindControl("DateRegTxtBx");
        TextBox OrgCostTxtBx = (TextBox)SctrFrmVw.FindControl("OrgCostTxtBx");
        TextBox InsTxtBx = (TextBox)SctrFrmVw.FindControl("InsTxtBx");
        TextBox TaxTxtBx = (TextBox)SctrFrmVw.FindControl("TaxTxtBx");
        TextBox MotTxtBx = (TextBox)SctrFrmVw.FindControl("MotTxtBx");
        TextBox RacTxtBx = (TextBox)SctrFrmVw.FindControl("RacTxtBx");
        TextBox StatTxtBx = (TextBox)SctrFrmVw.FindControl("StatTxtBx");

        Label StatLbl = (Label)SctrFrmVw.FindControl("StatLbl");

        StatLbl.Text = RegNoTxtBx.Text + " " + InsTxtBx.Text;

        if (value > 0)
        {

        }
    }
}

the whole ascx.cs
stellyuk we meant the HTML part
it should be
 
StatDdl.Items.Insert(0, "Please Select")
StatDdl.Items.Insert("Please Select", 0)
here ya go:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SctrDets.ascx.cs" Inherits="UserControls_SctrDets" %>
&nbsp;<asp:FormView ID="SctrFrmVw" runat="server" AllowPaging="True">
    <ItemTemplate>
        <table>
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Reg Number</td>
                <td style="width: 100px">
                    <asp:TextBox ID="RegNoTxtBx" Text='<%# Bind("RegNo") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Make</td>
                <td style="width: 100px">
                    <asp:TextBox ID="MakeTxtBx" Text='<%# Bind("Make") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Model</td>
                <td style="width: 100px">
                    <asp:TextBox ID="MdlTxtBx" Text='<%# Bind("Model") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    DateRegistered</td>
                <td style="width: 100px">
                    <asp:TextBox ID="DateRegTxtBx" Text='<%# Bind("DateRegistered") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Orginal Cost</td>
                <td style="width: 100px">
                    <asp:TextBox ID="OrgCostTxtBx" Text='<%# Bind("OrginalCost") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Insurance Due</td>
                <td style="width: 100px">
                    <asp:TextBox ID="InsTxtBx" Text='<%# Bind("InsDue") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Tax Due</td>
                <td style="width: 100px">
                    <asp:TextBox ID="TaxTxtBx" Text='<%# Bind("TaxDue") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    MOT Due</td>
                <td style="width: 100px">
                    <asp:TextBox ID="MotTxtBx" Text='<%# Bind("MOTDue") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px; height: 26px">
                    RAC Due</td>
                <td style="width: 100px; height: 26px">
                    <asp:TextBox ID="RacTxtBx" Text='<%# Bind("RACDue") %>' runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Status</td>
                <td style="width: 100px">
                    <asp:DropDownList ID="StatDdl" runat="server">
                    </asp:DropDownList></td>
            </tr>
            <tr>
                <td style="width: 100px; height: 21px">
                    Information</td>
                <td style="width: 100px; height: 21px">
                    <asp:Button ID="InfoBtn" runat="server" Text="    " OnClick="InfoBtn_Click" /></td>
            </tr>
            <tr>
                <td align="center" colspan="2" style="height: 26px">
                    <asp:Button ID="UpdateTxtBx" runat="server" Text="Update" OnClick="UpdateTxtBx_Click" /></td>
            </tr>
            <tr>
                <td align="center" colspan="2" style="height: 26px">
                    <asp:Label ID="StatLbl" runat="server"></asp:Label></td>
            </tr>
        </table>
    </ItemTemplate>
    <PagerSettings Mode="NumericFirstLast" />
</asp:FormView>
any help please?

Stelly
ASKER CERTIFIED SOLUTION
Avatar of Oliver Amaya
Oliver Amaya
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial