Solved

Problem with DropDownList

Posted on 2007-11-26
10
239 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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