user2918543
user2918543

Reputation: 29

Object Reference Not set to an instance of an object- Am getting this error

<asp:TemplateField HeaderText="Team Leader">
    <ItemTemplate>
        <asp:Label ID="gvuser_teamleader" runat="server" Text='<%# Bind("TeamLeaderID")  %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtuserteamleader" runat="server" Width="100px" Text='<%# Eval("TeamLeaderID") %>' CssClass="textboxstyle roundedcorner aligncenter gradientskyblue"></asp:TextBox>
        <asp:ListBox ID="listboxuserteamleader" runat="server" Width="110px" AutoPostBack="true" OnSelectedIndexChanged="listboxuserteamleader_SelectedIndexChanged" CssClass="textboxstyle roundedcorner aligncenter gradientskyblue"></asp:ListBox>
        <asp:DropDownExtender ID="DropDownExtender3" runat="server" TargetControlID="txtuserteamleader" DropDownControlID="listboxuserteamleader"></asp:DropDownExtender>                                    
    </EditItemTemplate>
</asp:TemplateField>

When I am trying to fire the selected listbox index changed event and trying to bind the listbox selected value to textbox am getting this error. Also both controls are inside the gridview edit item template field.

My code:

protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in gvusers.Rows)
        {
            TextBox txtuserteamleader = (TextBox)gvusers.FindControl("txtuserteamleader");
            ListBox listboxuserteamleader = (ListBox)gvusers.FindControl("listboxuserteamleader");
            txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
        }
    }

Upvotes: 0

Views: 1266

Answers (2)

tariq
tariq

Reputation: 2258

try this:

protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in gvusers.Rows)
    {
        TextBox txtuserteamleader = (TextBox)gvr.FindControl("txtuserteamleader");
        ListBox listboxuserteamleader = (ListBox)gvr.FindControl("listboxuserteamleader");
        txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
    }
}

When you iterate through the rows of the GridView, you have to search for the control inside that row, so the error you made is that you were searching it in the entire GridView, where each row would have this control. So you need to search it in a row.

Upvotes: 0

Ram Singh
Ram Singh

Reputation: 6938

just try the below code:

protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvusers.Rows)
{
    TextBox txtuserteamleader = (TextBox)gvr.FindControl("txtuserteamleader");
    ListBox listboxuserteamleader = (ListBox)gvr.FindControl("listboxuserteamleader");
    if(txtuserteamleader !=null && listboxuserteamleader !=null)
     {
       txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
     }
 }
}

Actually what was you problem : you have created instance of gridview "gvusers" as gvr for each row... so in foreach you must have to use that instance not the "gvusers" ... here you were making mistake...

That's all

Upvotes: 2

Related Questions