views:

123

answers:

3

Below code i have to ammend for adding a dropdown in my asp website. I have already added could you please check what is wrong.

Function collectEmailBodyText()
        Try
            Dim counterEnd As Integer = subActivated_HowManyControlsInAPanel()
            Dim counter As Integer = 0
            Dim tempPanelLabel As Label
            Dim tempPanelInputBox As TextBox
            Dim tempPanelDropDownBox As DropDownList
            Dim tempCollector As String
            Dim panelUsed As String = ""
            '* Find out which panel is used to collect panel data:
            panelUsed = view0_panelUsed.ToString
            Response.Write("<!-- " + panelUsed + " -->")
            '
            tempCollector = "<p><b>" + lbl_viewTitle0.Text + "</b>"
            tempCollector = tempCollector + "<br>" + lbl_view0_firstName.Text + ": " + txt_firstName.Text
            tempCollector = tempCollector + "<br>" + lbl_view0_surname.Text + ": " + txtSurName.Text
            tempCollector = tempCollector + "<br>" + lbl_view0_ContactNum.Text + ": " + txt_contactNum.Text
            '
            tempCollector = tempCollector + "<p><b>" + lbl_viewTitle1.Text + "</b>"
            tempCollector = tempCollector + "<br>" + lbl_view1_firstName.Text + ": " + txt_view1_firstname.Text
            tempCollector = tempCollector + "<br>" + lbl_view1_surname.Text + ": " + txt_view1_surname.Text
            tempCollector = tempCollector + "<br>" + lbl_view1_userID.Text + " " + txt_view1_userID.Text
            tempCollector = tempCollector + "<br>" + lbl_view1_workUnit.Text + ": " + ddl_view1_workunit.SelectedItem.Text + " :: " + ddl_view1_workunit.SelectedValue.ToString()
            tempCollector = tempCollector + "<br>" + lbl_view0_typeOfRequest.Text + ": " + ddl_view0_typeOfRequest.SelectedItem.ToString
            tempCollector = tempCollector + "<br>" + lbl_view0_workUnitLevel.Text + ": " + ddl_view0_workUnitLevel.SelectedItem.ToString + "<br>"
            '
            '* Collect panel data:
            Do
                counter = counter + 1
                tempPanelLabel = New Label
                tempPanelInputBox = New TextBox
                tempPanelDropDownBox = New DropDownList
                tempPanelLabel = form1.FindControl("lbl_" + panelUsed + "_label" + counter.ToString())
                tempPanelInputBox = form1.FindControl("txt_" + panelUsed + "_input" + counter.ToString())
                tempPanelDropDownBox = DirectCast(form1.FindControl(("txt_" & panelUsed & "_ddinput") + counter.ToString()), DropDownList)

                tempCollector = tempCollector + "<br>" + tempPanelLabel.Text
                'tempCollector = tempCollector + ": " + tempPanelInputBox.Text
                tempCollector = tempCollector + ": " + tempPanelDropDownBox.SelectedValue


            Loop Until counter = counterEnd
            '
            If storeSelected() = 0 Then
                tempCollector = tempCollector + "<p><b>" + lbl_viewTitle2.Text + "</b>"
                tempCollector = tempCollector + "<br>" + lbl_view2_ManagersEmailAddress.Text + ": " + txt_view2_ManagersEmailAddress.Text
            End If
            '
            Return tempCollector
        Catch ex As Exception
            Return ex.ToString()
            Response.Write(ex.ToString())
        End Try

    End Function

Below is the extra line i added

tempPanelDropDownBox = DirectCast(form1.FindControl(("txt_" & panelUsed & "_ddinput") + counter.ToString()), DropDownList)

and i am getting the following error:

System.NullReferenceException: Object reference not set to an instance of an object. at WebApplication1._Default.collectEmailBodyText() in C:\v1.5_production_05June09\Default.aspx.vb:line 220

+1  A: 

It would be easier to know which object is null given line numbers, but at a guess one of the lines in the form

tempPanelInputBox = form1.FindControl("txt_" + panelUsed + "_input" + counter.ToString())

is failing, this would be because the name generated by the part "txt_" + panelUsed + "_input" + counter.ToString() doesn't match a control within the form.

So, look at the line 220 within the file, and check the control that it is looking for appears within the form.

After looking at it, I think the line

tempPanelDropDownBox = DirectCast(form1.FindControl(("txt_" & panelUsed & "_ddinput") + counter.ToString()), DropDownList)

should read

tempPanelDropDownBox = DirectCast(form1.FindControl(("txt_" & panelUsed & "_ddlinput") + counter.ToString()), DropDownList)

the difference being ddl in the name not dd (Based on assumption that you name drop downs ddl)

Tetraneutron
this is line 220tempCollector = tempCollector + ": " + tempPanelDropDownBox.textand i have selected a value in dropdown list in the form
no name is currect it is dd NOT ddl
in that case is it txt_ at the start? At the end of the day the string you generate to pass into the FindControl function doesn't match a controls name, can you give us where that particular drop down is created?
Tetraneutron
txt_panview3_ddinput1 this is the dropdown control ID
when i select a option called store in another dropdown it will be diverted to a view and which will show the dropdown in questionbefore there was a textbox i have put aside a dropdown near to that . Later i want to remove the textbox
And if you break point on that line, then quick watch into the forms controls, you see a control with this name?
Tetraneutron
i guess if the loop runs , the name of dropdown for different views does not exists and hence throwing a error !what about pulling out that dropdown reference line out of loop , i want this dropdown for only one view!
Ahh, if you only have one drop down, but this loop runs multiple times, then sure pull this line out of the loop, or something similar. Does that work? (I assume you;; have to replace the counter.ToString() with just 1
Tetraneutron
Thanks a ton its worked :-)
+1  A: 

In your code, if a control with the name you are concatenating doesn't exist (or if your concatenation isn't quite right, for example) then you could get this error.

Perform the findcontrol function first and check it's not null before trying to obtain a value from it.

For debugging purposes, it may well be worth checking that "panelUsed" is what you're expecting. If it's an empty string because the variable hasn't been set correctly, this could cause your error.

Sohnee
+1  A: 

try using Debugger! Go step by step and you'll find your error.

100r