Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with dropdown causing error: Object reference not set to an instance of an object
    primarykey
    data
    text
    <p>I have drop down box that does not keep the selected value so i had to add this line of code: </p> <pre><code>ddl_Answer.Items.FindByValue((e.Row.FindControl("lblAns") as Label).Text).Selected = true; </code></pre> <p>But adding this line of code is causing me another problem. I have built the dropdown in a Gridview and the Gridview has an Edit link, once you click the edit then you should see the drop down box, but the issue now is when i click the Edit link and nothing has been selected in the dropdown then i get an error. It only works when something was already selected. Here is my code:</p> <pre><code>protected void RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow &amp;&amp; GridView1.EditIndex == e.Row.RowIndex) { int current_quest_sk = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value); DropDownList ddl_Answer = (DropDownList)e.Row.FindControl("ddl_Answer"); String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString; SqlConnection con2 = new SqlConnection(strConnString); SqlDataAdapter sda = new SqlDataAdapter(); SqlCommand cmd1 = new SqlCommand("select distinct DD_ANSWER from MainTable D, SecondryTable Q where Q.ANS_OPT = D.ID and QUEST_SK= '" + current_quest_sk + "' "); cmd1.Connection = con2; con2.Open(); ddl_Answer.DataSource = cmd1.ExecuteReader(); ddl_Answer.DataTextField = "DD_ANSWER"; ddl_Answer.DataValueField = "DD_ANSWER"; ddl_Answer.DataBind(); con2.Close(); ddl_Answer.Items.FindByValue((e.Row.FindControl("lblAns") as Label).Text).Selected = true; } </code></pre> <p>here is the markup for the dropdown</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" Width="870px" OnRowDataBound="RowDataBound" AutoGenerateColumns="False" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" AllowPaging="True" ShowFooter="True" OnPageIndexChanging="OnPaging" OnRowEditing="EditGridView1" OnRowUpdating="UpdateGridView1" OnRowCancelingEdit="CancelEdit" CellPadding="4" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" DataKeyNames="QUEST_SK"&gt; &lt;AlternatingRowStyle BackColor="#C2D69B" /&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText="Answer"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblAns" runat="server" Text='&lt;%# Eval("DDL_ANS")%&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:Label ID="lblAns" runat="server" Text='&lt;%# Eval("DDL_ANS")%&gt;' Visible="false"&gt;&lt;/asp:Label&gt; &lt;asp:DropDownList ID="ddl_Answer" runat="server"&gt; &lt;/asp:DropDownList&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:CommandField ShowEditButton="True" &gt; &lt;ItemStyle Font-Size="Smaller" ForeColor="#FF3300" /&gt; &lt;/asp:CommandField&gt; &lt;/Columns&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload