Solved

Object reference not set to an instance of an object.

Posted on 2016-08-03
8
51 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Ahelbling
ID: 41741114
it is definitely a dropdownlist
0
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

630 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