Solved

Problem with DropDownList

Posted on 2007-11-26
10
234 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
  • 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:joex911
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
 

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:joex911
ID: 20352123
stellyuk we meant the HTML part
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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:
joex911 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now