Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Object reference not set to an instance of an object.

Posted on 2016-08-03
8
Medium Priority
?
54 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 total points
ID: 41741148
yes you would probably have to drill down through those controls.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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