0

Newbie question: What exactly needs to be initialized to prevent a NullReferenceException? I don't understand what the variable is. What object reference do I need to set to an instance of what object? Thank you!

.aspx

        <p>
            <asp:GridView ID="gvConvo" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="gvConvo_SelectedIndexChanged">
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnConvo" OnClick="lbtnConvo_Click" Text='<%#Eval("ConvoUID") %>' runat="server">LinkButton</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Convo" HeaderText="Convo" />
                    <asp:BoundField DataField="ConvoDate" HeaderText="Date" />
                    <asp:BoundField DataField="ConvoDesc" HeaderText="Description" />
                    <asp:BoundField DataField="ConvoType" HeaderText="Type" />
                    <asp:BoundField DataField="MediaSource" HeaderText="Media Source" />
                </Columns>
            </asp:GridView>
        </p>
        <p>
            &nbsp;</p>
        <p>
            Convo:
            <asp:TextBox ID="txtConvoSelection" runat="server" OnTextChanged="txtConvoSelection_TextChanged"></asp:TextBox>
        </p>

.cs

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string PersuasionDBCon = ConfigurationManager.ConnectionStrings["PersuasionDBCon"].ConnectionString;
            using (SqlConnection con = new SqlConnection(PersuasionDBCon))
            {
                SqlCommand cmd = new SqlCommand("GetConvo", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                gvConvo.DataSource = cmd.ExecuteReader();
                gvConvo.DataBind();
            }
        }
    }

    protected void lbtnConvo_Click(object sender, EventArgs e)
    {
        txtConvoSelection.Text = gvConvo.SelectedRow.Cells[1].Text;
    }

    protected void BtnSaveQuote_Click(object sender, EventArgs e)
    {

    }

    protected void gvConvo_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void txtConvoSelection_TextChanged(object sender, EventArgs e)
    {

    }

This is the line that's throwing the exception:

txtConvoSelection.Text = gvConvo.SelectedRow.Cells[1].Text;
  • 1
    Either `txtConvoSelection` is null, `gvConvo` is null, `SelectedRow` is null, or (less likely, frankly) `Cells[1]` is null. – Llama Mar 07 '19 at 05:13
  • txtConvoSelection is just an empty text box that's waiting to be populated on lbtnConvo_Click. Is that the same as being null? If the value isn't null to start with, what should it be? – Vesper Annstas Mar 07 '19 at 05:18

2 Answers2

2

The code to load the grid is surrounded by an if (!IsPostBack){} block. Guess what? The button click event causes a postback. That's how it runs. And every time you do a postback, the entire page starts over from scratch. That's how HTTP works.

So when you click the button, the data for the grid is not bound, and there is no selected row.

The best fix for this is to make this happen in javascript, rather than C#. Reserve server events for things that really need to do work on the server. That will greatly improve perceived performance (no waiting for round-trips to a potentially-distant server) and help your server scale to handle more users by not needing to rebuild the entire html document for simple changes.

Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764
  • Thanks, Joel! I commented out the if (!IsPostBack){} lines so that the data would be bound when I click the button, but am still getting the same error. – Vesper Annstas Mar 07 '19 at 05:26
  • @VesperAnnstas That's an improvement... but ViewState is restored _before_ the page load phase, so if the data isn't loaded earlier (`Pre_Init` is one option) there still can't be a selected row. I updated the answer to suggest a better fix, though. – Joel Coehoorn Mar 07 '19 at 05:28
  • 1
    Oh: and use backtick characters (just below the escape key) in comments to mark code sections. – Joel Coehoorn Mar 07 '19 at 05:30
0

Index starts from 0 gvConvo.SelectedRow.Cells[1] this field must be null

Try

gvConvo.SelectedRow.Cells[0]

Also check for gvConvo.SelectedRow may be there is no selected in Grid

Ashish Sapkale
  • 520
  • 2
  • 13