DropDownList control - Object reference not set to an instance of an object

Discussion in 'ASP .Net Web Controls' started by egi, Aug 2, 2004.

  1. egi

    egi Guest

    Hi!
    Please help me to solve this problem. I have a dropdownlist on a page, which
    is populated trough database (databind).
    Page is working well till form submit. When i submit a page and try to get
    value of dropdown, i get this error:


    ******************* ERROR ********************
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.

    Source Error:


    Line 103: lblError.Visible = True
    Line 104: btnSubmit.Visible = False
    Line 105: lblError.Text = request_department.SelectedItem.Value
    Line 106:
    Line 107: 'lblError.Text = request_informer_email.Text.ToString &
    "<br><br>" & _
    ***************************************************




    ***************************************************
    Code Behind:
    ***************************************************

    Public Class AddRequest
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    End Sub
    Protected WithEvents tblRequest As System.Web.UI.WebControls.Table
    Protected WithEvents tblInformer As System.Web.UI.WebControls.Table
    Protected WithEvents request_department As
    System.Web.UI.WebControls.DropDownList
    Protected WithEvents request_name As System.Web.UI.WebControls.TextBox
    Protected WithEvents request_text As System.Web.UI.WebControls.TextBox
    Protected WithEvents request_informer As System.Web.UI.WebControls.TextBox
    Protected WithEvents request_informer_email As
    System.Web.UI.WebControls.TextBox
    Protected WithEvents request_informer_phone As
    System.Web.UI.WebControls.TextBox
    Protected WithEvents request_informer_notify As
    System.Web.UI.WebControls.CheckBox
    Protected WithEvents lblError As System.Web.UI.WebControls.Label
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub
    #End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'hide error label and main table
    lblError.Visible = False
    tblRequest.Visible = False
    'check if page is not post back
    If Not Page.IsPostBack Then
    'show form on the page
    ShowForm()
    End If

    End Sub


    Private Sub ShowForm()

    'show table
    tblRequest.Visible = True
    'declare SQL string
    Dim sSQL As String
    'initialize database class
    Dim fConnectDB As connectDB = New connectDB
    'declare datatable, datarow and dataview
    Dim dtTable As DataTable
    Dim drData As DataRow
    Dim dvData As DataView

    Try
    'SQL string
    sSQL = "EXEC sp_show_departments"
    'connect to database and execute stored procedure sp_show_departments
    fConnectDB.FillDataAdapter(sSQL, "Departments", "HelpDesk")
    'get the dataset from database and add new row to it (default selection for
    DropDown)
    dtTable = fConnectDB.ReturnDataSet.Tables(0)
    drData = dtTable.NewRow()
    drData("department_name") = "--- Select department ---"
    drData("id") = "0"
    dtTable.Rows.Add(drData)
    'sort data on field department_name
    dvData = dtTable.DefaultView
    dvData.Sort = "department_name"
    'databind Dropdown list
    request_department.DataSource = dvData.Table
    request_department.DataTextField = "department_name"
    request_department.DataValueField = "id"
    request_department.DataBind()
    'set default selection
    request_department.SelectedValue = "0"
    Catch ex As Exception
    'catch posibble errors
    tblRequest.Visible = False
    lblError.Visible = True
    lblError.Text = ex.ToString
    End Try

    End Sub


    Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnSubmit.Click

    lblError.Visible = True
    btnSubmit.Visible = False

    lblError.Text = request_department.SelectedItem.Value '********** ERROR IS
    HERE !!! *************


    '*********** THIS IS WORKING OK !!! ****************
    'lblError.Text = request_informer_email.Text.ToString & "<br><br>" & _
    ' request_informer.Text.ToString & "<br><br>" & _
    ' request_informer_phone.Text.ToString & "<br><br>" & _
    ' request_name.Text.ToString & "<br><br>" & _
    ' request_text.Text.ToString & "<br><br>"

    End Sub


    End Class



    ***************************************************
    Page:
    ***************************************************

    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="AddRequest.ascx.vb" Inherits="HelpDesk.AddRequest"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:table id="tblRequest" EnableViewState="False" CellPadding="4"
    CellSpacing="2" runat="server"
    Width="100%" BorderStyle="None">
    <asp:TableRow>
    <asp:TableCell>
    <b>Name:</b>
    <asp:RequiredFieldValidator id="rfvName" ControlToValidate="request_name"
    EnableViewState="False" runat="server"
    ErrorMessage="Enter request name!"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ControlToValidate="request_name"
    ValidationExpression=".{10,80}" Text="Min 10 and Max 80 characters!"
    runat="server" ID="regexName"></asp:RegularExpressionValidator>
    <br />
    <asp:TextBox Runat="server" ID="request_name" MaxLength="80"
    Width="100%"></asp:TextBox>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell>
    <b>Description:</b>
    <asp:RequiredFieldValidator id="rfvText" ControlToValidate="request_text"
    EnableViewState="False" runat="server"
    ErrorMessage="Enter request description!"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ControlToValidate="request_text"
    ValidationExpression=".{10,1000}" Text="Min 10 and Max 1000 characters!"
    runat="server" ID="regexDescription"></asp:RegularExpressionValidator>
    <br />
    <asp:TextBox Runat="server" ID="request_text" MaxLength="1000"
    Width="100%" Rows="10" TextMode="MultiLine"></asp:TextBox>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell>
    <b>Informer data:</b><br />
    <asp:Table ID="tblInformer" Runat="server" CellPadding="0"
    CellSpacing="2" EnableViewState="False"
    BorderStyle="None" Width="100%">
    <asp:TableRow>
    <asp:TableCell>Full name:
    <asp:RequiredFieldValidator id="rfvInformer"
    ControlToValidate="request_informer" EnableViewState="False" runat="server"
    ErrorMessage="Enter your full name!"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ControlToValidate="request_informer"
    ValidationExpression=".{3,30}" Text="Min 3 and Max 30 characters!"
    runat="server" ID="regexInformer"></asp:RegularExpressionValidator>
    <br />
    <asp:TextBox ID="request_informer" MaxLength="30" Width="200px"
    Runat="server"></asp:TextBox>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell>Email:<br />
    <asp:TextBox ID="request_informer_email" MaxLength="50" Width="200px"
    Runat="server"></asp:TextBox>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell>Phone:<br />
    <asp:TextBox ID="request_informer_phone" MaxLength="15" Width="110px"
    Runat="server"></asp:TextBox>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell>Department:
    <asp:RequiredFieldValidator id="rfvDepartment"
    ControlToValidate="request_department" EnableViewState="False"
    runat="server" InitialValue="0" ErrorMessage="Select
    department!"></asp:RequiredFieldValidator><br />




    <asp:DropDownList ID="request_department"
    Runat="server"></asp:DropDownList> ******** DROP DOWN *********




    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell ColumnSpan="2">
    <br />
    <asp:CheckBox ID="request_informer_notify" Runat="server" Text="Notify
    me on changes"></asp:CheckBox>
    </asp:TableCell>
    </asp:TableRow>
    </asp:Table>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
    <asp:TableCell>
    <asp:button id="btnSubmit" text="Submit" onClick="btnSubmit_Click"
    runat="server"></asp:button>
    </asp:TableCell>
    </asp:TableRow>
    </asp:table>
    <P><asp:label id="lblError" runat="server"
    Visible="False">Error</asp:label></P>
     
    egi, Aug 2, 2004
    #1
    1. Advertising

  2. egi

    egi Guest

    I found out, that when i put asp:dropdownlist out ot the asp:table it works
    fine ...
    Whats wrong with that ??




    "egi" <> wrote in message
    news:...
    > Hi!
    > Please help me to solve this problem. I have a dropdownlist on a page,

    which
    > is populated trough database (databind).
    > Page is working well till form submit. When i submit a page and try to get
    > value of dropdown, i get this error:
    >
    >
    > ******************* ERROR ********************
    > Object reference not set to an instance of an object.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    >
    > Source Error:
    >
    >
    > Line 103: lblError.Visible = True
    > Line 104: btnSubmit.Visible = False
    > Line 105: lblError.Text = request_department.SelectedItem.Value
    > Line 106:
    > Line 107: 'lblError.Text = request_informer_email.Text.ToString &
    > "<br><br>" & _
    > ***************************************************
    >
    >
    >
    >
    > ***************************************************
    > Code Behind:
    > ***************************************************
    >
    > Public Class AddRequest
    > Inherits System.Web.UI.UserControl
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub

    InitializeComponent()
    > End Sub
    > Protected WithEvents tblRequest As System.Web.UI.WebControls.Table
    > Protected WithEvents tblInformer As System.Web.UI.WebControls.Table
    > Protected WithEvents request_department As
    > System.Web.UI.WebControls.DropDownList
    > Protected WithEvents request_name As System.Web.UI.WebControls.TextBox
    > Protected WithEvents request_text As System.Web.UI.WebControls.TextBox
    > Protected WithEvents request_informer As System.Web.UI.WebControls.TextBox
    > Protected WithEvents request_informer_email As
    > System.Web.UI.WebControls.TextBox
    > Protected WithEvents request_informer_phone As
    > System.Web.UI.WebControls.TextBox
    > Protected WithEvents request_informer_notify As
    > System.Web.UI.WebControls.CheckBox
    > Protected WithEvents lblError As System.Web.UI.WebControls.Label
    > Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    > 'NOTE: The following placeholder declaration is required by the Web Form
    > Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    > #End Region
    >
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > 'hide error label and main table
    > lblError.Visible = False
    > tblRequest.Visible = False
    > 'check if page is not post back
    > If Not Page.IsPostBack Then
    > 'show form on the page
    > ShowForm()
    > End If
    >
    > End Sub
    >
    >
    > Private Sub ShowForm()
    >
    > 'show table
    > tblRequest.Visible = True
    > 'declare SQL string
    > Dim sSQL As String
    > 'initialize database class
    > Dim fConnectDB As connectDB = New connectDB
    > 'declare datatable, datarow and dataview
    > Dim dtTable As DataTable
    > Dim drData As DataRow
    > Dim dvData As DataView
    >
    > Try
    > 'SQL string
    > sSQL = "EXEC sp_show_departments"
    > 'connect to database and execute stored procedure sp_show_departments
    > fConnectDB.FillDataAdapter(sSQL, "Departments", "HelpDesk")
    > 'get the dataset from database and add new row to it (default selection

    for
    > DropDown)
    > dtTable = fConnectDB.ReturnDataSet.Tables(0)
    > drData = dtTable.NewRow()
    > drData("department_name") = "--- Select department ---"
    > drData("id") = "0"
    > dtTable.Rows.Add(drData)
    > 'sort data on field department_name
    > dvData = dtTable.DefaultView
    > dvData.Sort = "department_name"
    > 'databind Dropdown list
    > request_department.DataSource = dvData.Table
    > request_department.DataTextField = "department_name"
    > request_department.DataValueField = "id"
    > request_department.DataBind()
    > 'set default selection
    > request_department.SelectedValue = "0"
    > Catch ex As Exception
    > 'catch posibble errors
    > tblRequest.Visible = False
    > lblError.Visible = True
    > lblError.Text = ex.ToString
    > End Try
    >
    > End Sub
    >
    >
    > Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles btnSubmit.Click
    >
    > lblError.Visible = True
    > btnSubmit.Visible = False
    >
    > lblError.Text = request_department.SelectedItem.Value '********** ERROR IS
    > HERE !!! *************
    >
    >
    > '*********** THIS IS WORKING OK !!! ****************
    > 'lblError.Text = request_informer_email.Text.ToString & "<br><br>" & _
    > ' request_informer.Text.ToString & "<br><br>" & _
    > ' request_informer_phone.Text.ToString & "<br><br>" & _
    > ' request_name.Text.ToString & "<br><br>" & _
    > ' request_text.Text.ToString & "<br><br>"
    >
    > End Sub
    >
    >
    > End Class
    >
    >
    >
    > ***************************************************
    > Page:
    > ***************************************************
    >
    > <%@ Control Language="vb" AutoEventWireup="false"
    > Codebehind="AddRequest.ascx.vb" Inherits="HelpDesk.AddRequest"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    > <asp:table id="tblRequest" EnableViewState="False" CellPadding="4"
    > CellSpacing="2" runat="server"
    > Width="100%" BorderStyle="None">
    > <asp:TableRow>
    > <asp:TableCell>
    > <b>Name:</b>
    > <asp:RequiredFieldValidator id="rfvName"

    ControlToValidate="request_name"
    > EnableViewState="False" runat="server"
    > ErrorMessage="Enter request name!"></asp:RequiredFieldValidator>
    > <asp:RegularExpressionValidator ControlToValidate="request_name"
    > ValidationExpression=".{10,80}" Text="Min 10 and Max 80 characters!"
    > runat="server" ID="regexName"></asp:RegularExpressionValidator>
    > <br />
    > <asp:TextBox Runat="server" ID="request_name" MaxLength="80"
    > Width="100%"></asp:TextBox>
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell>
    > <b>Description:</b>
    > <asp:RequiredFieldValidator id="rfvText"

    ControlToValidate="request_text"
    > EnableViewState="False" runat="server"
    > ErrorMessage="Enter request

    description!"></asp:RequiredFieldValidator>
    > <asp:RegularExpressionValidator ControlToValidate="request_text"
    > ValidationExpression=".{10,1000}" Text="Min 10 and Max 1000 characters!"
    > runat="server" ID="regexDescription"></asp:RegularExpressionValidator>
    > <br />
    > <asp:TextBox Runat="server" ID="request_text" MaxLength="1000"
    > Width="100%" Rows="10" TextMode="MultiLine"></asp:TextBox>
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell>
    > <b>Informer data:</b><br />
    > <asp:Table ID="tblInformer" Runat="server" CellPadding="0"
    > CellSpacing="2" EnableViewState="False"
    > BorderStyle="None" Width="100%">
    > <asp:TableRow>
    > <asp:TableCell>Full name:
    > <asp:RequiredFieldValidator id="rfvInformer"
    > ControlToValidate="request_informer" EnableViewState="False"

    runat="server"
    > ErrorMessage="Enter your full name!"></asp:RequiredFieldValidator>
    > <asp:RegularExpressionValidator ControlToValidate="request_informer"
    > ValidationExpression=".{3,30}" Text="Min 3 and Max 30 characters!"
    > runat="server"

    ID="regexInformer"></asp:RegularExpressionValidator>
    > <br />
    > <asp:TextBox ID="request_informer" MaxLength="30" Width="200px"
    > Runat="server"></asp:TextBox>
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell>Email:<br />
    > <asp:TextBox ID="request_informer_email" MaxLength="50"

    Width="200px"
    > Runat="server"></asp:TextBox>
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell>Phone:<br />
    > <asp:TextBox ID="request_informer_phone" MaxLength="15"

    Width="110px"
    > Runat="server"></asp:TextBox>
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell>Department:
    > <asp:RequiredFieldValidator id="rfvDepartment"
    > ControlToValidate="request_department" EnableViewState="False"
    > runat="server" InitialValue="0" ErrorMessage="Select
    > department!"></asp:RequiredFieldValidator><br />
    >
    >
    >
    >
    > <asp:DropDownList ID="request_department"
    > Runat="server"></asp:DropDownList> ******** DROP DOWN *********
    >
    >
    >
    >
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell ColumnSpan="2">
    > <br />
    > <asp:CheckBox ID="request_informer_notify" Runat="server"

    Text="Notify
    > me on changes"></asp:CheckBox>
    > </asp:TableCell>
    > </asp:TableRow>
    > </asp:Table>
    > </asp:TableCell>
    > </asp:TableRow>
    > <asp:TableRow>
    > <asp:TableCell>
    > <asp:button id="btnSubmit" text="Submit" onClick="btnSubmit_Click"
    > runat="server"></asp:button>
    > </asp:TableCell>
    > </asp:TableRow>
    > </asp:table>
    > <P><asp:label id="lblError" runat="server"
    > Visible="False">Error</asp:label></P>
    >
    >
     
    egi, Aug 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    482
    Mark Rae [MVP]
    Sep 20, 2007
  2. David Bowey
    Replies:
    1
    Views:
    249
    David Bowey
    Mar 16, 2005
  3. gishani ratnayake via .NET 247

    User Control Error : Object reference not set to an instance of an object.

    gishani ratnayake via .NET 247, Mar 11, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    174
  4. David Bowey
    Replies:
    1
    Views:
    335
    David Bowey
    Mar 16, 2005
  5. vladp
    Replies:
    0
    Views:
    323
    vladp
    Aug 18, 2005
Loading...

Share This Page