Solved

Problem with DropDownList

Posted on 2007-11-26
10
236 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

774 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