Solved

Object reference not set to an instance of an object.

Posted on 2016-08-03
8
38 Views
Last Modified: 2016-08-04
Cant figure out why this error is happening...  It happens in DetailsView1_DataBound  after the SaveButton_Click event shown below.

error
           <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="sqlds_SelectedRec" AutoGenerateRows="False"
                     DefaultMode="Edit" DataKeyNames="TID" BorderStyle="None" Width="500px" CellPadding="3" GridLines="None" CellSpacing="2" 
                     InsertRowStyle-Wrap="False" RowStyle-Wrap="False">
        <FieldHeaderStyle Wrap="False" HorizontalAlign="Left" />
        <Fields>
            <asp:TemplateField HeaderText="Department" HeaderStyle-CssClass="bold">
                <EditItemTemplate>
                     <asp:DropDownList ID="ddDept" runat="server" AutoPostBack="True" DataSourceID="sqlds_DDDept" DataTextField="DESCRIPTION"
                                       DataValueField="ENTRYID" AppendDataBoundItems="True" OnSelectedIndexChanged="ddDept_SelectedIndexChanged" OnDataBound="ddDept_OnDataBound">
                        <asp:ListItem></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvDept" runat="server" ErrorMessage="REQUIRED" CssClass="RequiredField" ControlToValidate="ddDept"></asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Activity Number" HeaderStyle-CssClass="bold">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddActivity" runat="server" AutoPostBack="False" DataSourceID="sqlds_DDActNum" DataTextField="DESCRIPTION"
                                      DataValueField="PROJECTID"  AppendDataBoundItems="True">
                         <asp:ListItem Value="" Text="Selection required"></asp:ListItem>
                         <asp:ListItem Value="99" Text="Not applicable"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvActivity" runat="server" ErrorMessage="REQUIRED" CssClass="RequiredField" ControlToValidate="ddActivity" Display="Dynamic"></asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>

Open in new window


  Protected Sub DetailsView1_DataBound(sender As Object, e As EventArgs) Handles DetailsView1.DataBound
        
        Using stCon As New SqlConnection(ConfigurationManager.ConnectionStrings("DirectoryConnectionString").ConnectionString)
            stCon.Open()
            
            Dim ddDept As DropDownList = DirectCast(DetailsView1.FindControl("ddDept"), DropDownList)
            sLabID = CType(Session("intAcctID"), String)
            Dim selCommand4 As New SqlCommand("SELECT ACCT_UNIT FROM [CorpCard_new].[dbo].[Trans] WHERE TID=@TID", stCon)
            selCommand4.Parameters.Add("@TID", SqlDbType.Int).Value = TID
            Dim sAcctUnit As String = CType(selCommand4.ExecuteScalar, String)
            If sAcctUnit = "0" Then
                ddDept.SelectedValue = sLabID
            Else
                ddDept.SelectedValue = sAcctUnit
            End If
            sDeptID = ddDept.SelectedValue        

Open in new window


  Protected Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
        
        If sender.CommandName="Update" And Page.IsValid Then
        
            Using stCon As New SqlConnection(ConfigurationManager.ConnectionStrings("CorpCardConnectionString").ConnectionString)
                stCon.Open()
            
                Dim item As GridDataItem = DirectCast(RadGrid1.SelectedItems(0), GridDataItem)
	            TID = item("TID").Text

                Dim ddDept As DropDownList = DirectCast(DetailsView1.FindControl("ddDept"), DropDownList)
                Dim sACCT_UNIT As String = ddDept.SelectedValue.ToString()
                Dim ddActNum As DropDownList = DirectCast(DetailsView1.FindControl("ddActivity"), DropDownList)
                Dim sAcctNum As String = ddActNum.SelectedValue.ToString()
                Dim ddExpType As DropDownList = DirectCast(DetailsView1.FindControl("ddExpType"), DropDownList)
                sExpType = ddExpType.SelectedValue.ToString()
                Dim txtPurpose As textbox = DirectCast(DetailsView1.FindControl("txtBP"), textbox)
                Dim txtPersEnt As textbox = DirectCast(DetailsView1.FindControl("txtPersEnt"), textbox)
                Dim txtDestCity As textbox = DirectCast(DetailsView1.FindControl("txtDestCity"), textbox)
                Dim txtPartyInfo As textbox = DirectCast(DetailsView1.FindControl("txtPartyInfo"), textbox)
                
                sqlds_SelectedRec.UpdateCommand = "UPDATE [CorpCard_new].[dbo].[Trans] SET ACCT_UNIT=@ENTRYID, ActivityNum=@ActivityCode, ExpID=@ExpID, Purpose=@Purpose, EntertainWho=@EntertainWho, TravelTo=@TravelTo, ThirdPartyInfo=@ThirdPartyInfo, LastSaved=@LastSaved WHERE TID=@TID"
                sqlds_SelectedRec.UpdateParameters("TID").DefaultValue = TID
                sqlds_SelectedRec.UpdateParameters("ENTRYID").DefaultValue = sACCT_UNIT
                sqlds_SelectedRec.UpdateParameters("ExpID").DefaultValue = sExpType
                sqlds_SelectedRec.UpdateParameters("ActivityCode").DefaultValue = sAcctNum
                sqlds_SelectedRec.UpdateParameters("Purpose").DefaultValue = txtPurpose.text
                sqlds_SelectedRec.UpdateParameters("EntertainWho").DefaultValue = txtPersEnt.text
                sqlds_SelectedRec.UpdateParameters("TravelTo").DefaultValue = txtDestCity.text
                sqlds_SelectedRec.UpdateParameters("ThirdPartyInfo").DefaultValue = txtPartyInfo.text
                sqlds_SelectedRec.UpdateParameters("LastSaved").DefaultValue = System.DateTime.Now().ToString()
                
                sqlds_SelectedRec.Update()
                DetailsView1.DataBind()
                RadGrid1.Rebind()
            End Using
        End If
        
    End Sub

Open in new window

0
Comment
Question by:Ahelbling
  • 4
  • 4
8 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 41741083
ddept might be null if the control isn't found.
0
 

Author Comment

by:Ahelbling
ID: 41741097
Dim ddDept As DropDownList = DirectCast(DetailsView1.FindControl("ddDept"), DropDownList)

ddDept shows as NOTHING when I step through up to the error.  Why is that?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 41741106
i would say that it isn't finding ddept. i think there is another parameter in findcontrol that would make the search more thorough. also make sure that it is really a dropdownlist.
0
 

Author Comment

by:Ahelbling
ID: 41741114
it is definitely a dropdownlist
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Ahelbling
ID: 41741115
<asp:TemplateField HeaderText="Department" HeaderStyle-CssClass="bold">
                <EditItemTemplate>
                     <asp:DropDownList ID="ddDept" runat="server" AutoPostBack="True" DataSourceID="sqlds_DDDept" DataTextField="DESCRIPTION"
                                       DataValueField="ENTRYID" AppendDataBoundItems="True" OnSelectedIndexChanged="ddDept_SelectedIndexChanged" OnDataBound="ddDept_OnDataBound">
                        <asp:ListItem></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvDept" runat="server" ErrorMessage="REQUIRED" CssClass="RequiredField" ControlToValidate="ddDept"></asp:RequiredFieldValidator>
                </EditItemTemplate>
            </asp:TemplateField>
0
 
LVL 22

Expert Comment

by:p_davis
ID: 41741121
to trouble shoot i would do a recursive search of all controls and make sure the id isn't getting change and/or that its there at all.
0
 

Author Comment

by:Ahelbling
ID: 41741144
this ddDept control within the detailview also resides inside a panel.  Could that have something to do with it?
0
 
LVL 22

Accepted Solution

by:
p_davis earned 500 total points
ID: 41741148
yes you would probably have to drill down through those controls.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now