Solved

Problem with DropDownList

Posted on 2007-11-26
10
238 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:stellyuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 9

Expert Comment

by:divinewind80
ID: 20351757
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.
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 20351775
Seems you are unable to find the controls ..
is it possible to post code for aspx?
thanks
0
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20351878
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stellyuk
ID: 20352022
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
0
 
LVL 10

Expert Comment

by:Oliver Amaya
ID: 20352123
stellyuk we meant the HTML part
0
 
LVL 7

Expert Comment

by:nisarkhan
ID: 20352158
it should be
 
StatDdl.Items.Insert(0, "Please Select")
0
 
LVL 7

Expert Comment

by:nisarkhan
ID: 20352162
StatDdl.Items.Insert("Please Select", 0)
0
 

Author Comment

by:stellyuk
ID: 20352410
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>
0
 

Author Comment

by:stellyuk
ID: 20353556
any help please?

Stelly
0
 
LVL 10

Accepted Solution

by:
Oliver Amaya earned 500 total points
ID: 20354898
You need to bind your FormView first, before trying to look for the dropdownlist:
if (tbl.Rows.Count == 1)
{
  SctrStat Status = PubQrys.GetSctrStat(SID);
 
  SctrFrmVw.PagerSettings.Visible = false;
  SctrFrmVw.DataSource = tbl;
  SctrFrmVw.DataBind();
 
  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();
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question