views:

40

answers:

2

Hi I have a gridview that shows a list of questions. Only one field is editable and if the information has been edited an email is sent to a specific "destiny". All works fine but when I try to get the Question ID in my SendEmail function (using Findcontrol) I get "Object reference not set to an instance of an object." How Can I get the Question ID?

My sqldatasource:

   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>" SelectCommand="SELECT Questions.QuestionID, Questions.Name, Questions.Question, Questions.Destiny, Questions.DateQuestion, Questions.Answer" UpdateCommand="UPDATE Questions SET Destiny = @Destiny WHERE (QuestionID = @QuestionID)">
    <SelectParameters>
   </SelectParameters>
     <UpdateParameters>
        <asp:Parameter Name="Destiny" />
        <asp:Parameter Name="QuestionID" />
    </UpdateParameters>
</asp:SqlDataSource>

My gridview:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="QuestionID" OnRowUpdated="GridView1_RowUpdated"
    DataSourceID="SqlDataSource1">
    <Columns>

        <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="QuestionID">
          <ItemTemplate>
                <asp:Label ID="LabelQuestionID" runat="server" Text='<%# Bind("QuestionID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>    

   <asp:TemplateField HeaderText="Question" InsertVisible="False" SortExpression="Question">
          <ItemTemplate>
                <asp:Label ID="LabelQuestion" runat="server" Text='<%# Bind("Question") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField> 

     <asp:TemplateField HeaderText="Date" SortExpression="DateQuestion">
                         <ItemTemplate>
                <asp:Label ID="LabelDateQuestion" runat="server" Text='<%# Bind("DateQuestion", "{0:g}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>



        <asp:TemplateField HeaderText="Destiny" SortExpression="Destiny">
                           <ItemTemplate>
                <asp:Label ID="LabelDestiny" runat="server" Text='<%# Bind("Destiny") %>'></asp:Label>

            </ItemTemplate>

            <EditItemTemplate>
            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Destiny") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>



        <asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>




      </Columns>
</asp:GridView>

My code behind:

    Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) 

    If Trim(e.OldValues("Destiny").ToString <> Trim(e.NewValues("Destiny").ToString)) Then
    QuestionID  = GridView1.FindControl("LabelQuestionID").ToString
    SendEmailtoAdviser(Trim(e.NewValues("Destiny")), QuestionID)
    End If
    End Sub
A: 

Your first step should be to break down the code to separate lines to find out which statement is returning you a null reference:

QuestionControl = GridView1.FindControl("LabelQuestionID")
QuestionID = QuestionControl.ToString

Recipient = e.NewValues("Destiny")
SendEmailtoAdviser(Trim(Recipient), QuestionID)

This will tell you whether the question control hasn't been found or the recipient isn't in the event arguments.

From there you can then figure out what hasn't been set up correctly.

ChrisF
Hi, Thanks for your help. I'm not sure what I need to do from your answer.
netNewbi3
@netNewbi3 - once you've split your code you can then step through on the debugger and find out which line is actually raising the null reference exception. For example it *might* be that `FindControl` is returning `null`. With this information you can then work out why this is the case. Is it that the label name is incorrect, or are you looking in the wrong container for the label?
ChrisF
ChrisF, thanks. The label name is correct. How can I check that I'm looking in the wrong container for the label?
netNewbi3
@netNewbi3 - I'm not 100% sure with Templates. My first port of call would be the MSDN.
ChrisF
A: 

Hi

I could get the value of a specific cell in a gridview if an update is done by using the following code. In my case I wanted to retrieve the ID which was the first column in my gridview.

 Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs)
    If Trim(e.OldValues("Destiny").ToString <> Trim(e.NewValues("Destiny").ToString)) Then
        Dim index As Integer = GridView1.EditIndex
        Dim row As GridViewRow = GridView1.Rows(index)
        SendEmailtoAdviser(Trim(e.NewValues("Destiny").ToString), row.Cells(0).Text)
    End If
 End Sub
netNewbi3