Solved

Object reference not set to an instance of an object.

Posted on 2016-08-03
8
45 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

21 Experts available now in Live!

Get 1:1 Help Now