Results 1 to 3 of 3

Thread: asp webform getting Object reference not set to an instance of an object. on a DDL

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    7

    asp webform getting Object reference not set to an instance of an object. on a DDL

    Hi has anyone else come across a similar issue as described below

    I have a web form with several DDL and text boxes some of them have post backs some dont

    the DDLs have 3 or 4 Values like Yes , No or Often, Regularly, Never

    a user is able to select the values and save his selection results on a sql server and a record number is generated on the save.

    he is also able to retrieve his results via the same record number Via a subroutine called RecordSrchId all the text boxes get populated most of the DDLs get populated and results are selected but some of the DDLs dont get selected.

    The DDLs for example will have Yes, No results as values in the list of items. The retrieved data from the sql is presented in a datatable and the value is cascaded down via the correct ID of the items for example "mydatatable.Tables(0).Rows(0)(2).ToString" where my datatable is the datatable and (2) is the item number i get a value from mydatatable as i have tested this with a Label control so its not returning null and the DDl have the correct list of values also in the correct case formats but for some reason it will give me an error below

    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 1148:
    Line 1149: crane_requirement_Frequency_40T0.Items.FindByText(mydatatable.Tables(0).Rows(0)(32).ToString).Select ed = True Line 1150: Line 1151:

    Source File: C:\inetpub\wwwroot\aspx\factfind\Default.aspx.vb Line: 1149

    Stack Trace:



    [NullReferenceException: Object reference not set to an instance of an object.]
    FactFindMenu.recordidsrch() in C:\inetpub\wwwroot\aspx\factfind\Default.aspx.vb:1149
    FactFindMenu.Srch_Record_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\aspx\factfind\Default.aspx.vb:1090
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

    the subroutine to retrive the date is below





    Private Sub recordidsrch()

    Dim searchFactfnd As New SearchFactFnd ' custom class used to retrive data From sql server

    Dim mydatatable As New Data.DataSet ' Dataset/table used

    mydatatable = searchFactfnd.FactFNDTableID(Rcrd_num_Txt.Text) ' populates the dataset with the function FactFNDTableID via a record Id gained from a textbox and function returns a dataset/table

    contact_name.Text = mydatatable.Tables(0).Rows(0)(1).ToString

    crane_hireCranesFrequency.Items.FindByText(mydatatable.Tables(0).Rows(0)(30).ToString)

    crane_spendPA.Text = mydatatable.Tables(0).Rows(0)(31).ToString

    crane_requirement_Frequency_40T.Items.FindByText(mydatatable.Tables(0).Rows(0)(32).ToString).Selecte d =True ' Line give error if i include .Selected =True

    Label4.Text = mydatatable.Tables(0).Rows(0)(32) ' gives me a result that can be found in the DDL

    end sub





    The HTML code dor the DDL is

    <aspropDownList ID="crane_requirement_Frequency_40T" runat="server" CssClass="largeinput" AutoPostBack="True">

    <asp:ListItem>Crane Requirements Frequency</asp:ListItem>

    <asp:ListItem>Regularly</asp:ListItem>

    <asp:ListItem>Often</asp:ListItem>

    <asp:ListItem>Seldom</asp:ListItem>

    <asp:ListItem>Never</asp:ListItem>

    </aspropDownList>



    I have tried several variations of the below

    Items.FindByText(mydatatable.Tables(0).Rows(0)(32).ToString).Selected =True

    Items.FindByvalue(mydatatable.Tables(0).Rows(0)(32).ToString).Selected =True

    ddl.selectedvalue

    ddl.selecteditem

    if not ispostback then

    'populate

    end if

    ddl.clearcontents

    tried rewriting the html ddl to below and try variations above did not work

    <aspropDownList ID="crane_requirement_Frequency_40T" runat="server" CssClass="largeinputd">

    <asp:ListItem Text="Crane Requirements Frequency"Value="" />

    <asp:ListItem Text="Regularly" Value="Regularly" />

    <asp:ListItem Text="Often" Value="Often" />

    <asp:ListItem Text="Seldom" Value="Seldom" />

    </aspropDownList>

    

    seriously stuck now any help whould be appreciated i know the values fro the data set are correct and not null and the values exist in the DDL could post back be causing this

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    7

    Re: asp webform getting Object reference not set to an instance of an object. on a DD

    Issue fixed the db server had the datatype as nchar(10) the xtra white spaces created the error
    chnaged to nvarchar instead

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: asp webform getting Object reference not set to an instance of an object. on a DD

    I'm willing to bet that this is because you're doing a case sensitive comparison.

    Edit: Nevermind, glad you got it sorted out.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width