Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I've have a gridview on my site:

XML
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-CssClass="gridview_alter"
                        AutoGenerateColumns="False" EmptyDataText=" " GridLines="None" OnRowCancelingEdit="RowCancelingEdit"
                        OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="RowDeleting" OnRowEditing="RowEditing"
                        OnRowUpdating="RowUpdating" ShowHeaderWhenEmpty="True" Width="820px">
                        <AlternatingRowStyle CssClass="gridview_alter" />
                        <Columns>
                            <asp:TemplateField HeaderText="Leírás" Visible="false" />
                            <asp:BoundField DataField="Description" HeaderStyle-Width="150" HeaderText="Leírás" HtmlEncode="false">
                                <HeaderStyle Width="150px" />
                            </asp:BoundField>
                            <asp:BoundField HeaderText="Kritérium" DataField="IndexNumber" />
                            <asp:BoundField DataField="Weight" HeaderStyle-Width="50" HeaderText="Súly">
                                <HeaderStyle Width="50px" />
                            </asp:BoundField>
                            <asp:TemplateField HeaderStyle-Width="100" HeaderText="Cél típusa">
                <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1"
                        runat="server" AutoPostBack="False">
                    </asp:DropDownList>
                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblGoalType" runat="server"></asp:Label>
                                </ItemTemplate>
                                <HeaderStyle Width="100px" />
                            </asp:TemplateField>
                            <asp:CommandField DeleteText="Törlés" HeaderStyle-Width="60" ShowDeleteButton="true">
                                <HeaderStyle Width="60px" />
                            </asp:CommandField>
                                        <asp:CommandField CancelText="Mégse" DeleteText="Törlés" EditText="Szerkesztés"
                                            HeaderStyle-Width="60" ShowEditButton="True" UpdateText="Frissítés"
                                            ValidationGroup="Leírás, Súly">
                                <HeaderStyle Width="60px" />
                            </asp:CommandField>
                        </Columns>
                    </asp:GridView>


The "Cél típusa" column gets data from a list, that contains 1, 2 or 3. In code behind:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Goal row = (Goal)e.Row.DataItem;
        Label lblGoalType = (Label)e.Row.FindControl("lblGoalType");

        e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("\n", "<br />");

        switch (row.GoalTypeId)
        {
            case 1:
                lblGoalType.Text = "Személyes";
                break;
            case 2:
                lblGoalType.Text = "Egyéni";
                break;
            case 3:
                lblGoalType.Text = "Szervezeti";
                break;
        }
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // reference the Delete LinkButton
        LinkButton db = (LinkButton)e.Row.Cells[5].Controls[0];
        db.OnClientClick = "return confirm('Biztos benne, hogy törölni akarja a kiválasztott célt?');";
    }
}


I give strings to the specified numbers.
My problem is that I need to update the cells with a dropdownlist, but when u press the Edit button on the gridview i get an error msg: Object reference not set to an instance of an object.

I am stuck here, got no idea, please help.
Posted
v3
Comments
JoCodes 14-Feb-14 3:31am    
Where and how are you binding the Dropdownlist which is in the Gridview ?
Sumit_Pathak 14-Feb-14 3:32am    
debug your code and tell us in which line u r getting this error...
kirk651 14-Feb-14 3:41am    
I get the error at different lines, depending witch row i want to update.
If i want to update a row, that contains "Egyéni", i get the error at:

lblGoalType.Text = "Egyéni";

or if I want to update a row that contains "Személyes":

lblGoalType.Text = "Személyes";

and so on...
The14thNoah 14-Feb-14 3:37am    
can you show your code snippets on Edit Button?
kirk651 14-Feb-14 3:42am    
protected void RowEditing(object sender, GridViewEditEventArgs e)
{
save = false;
GridView1.EditIndex = e.NewEditIndex;
BindData();
}

protected void RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
lblUpdateWarning.Text = "";
BindData();
}

Quote:
but when u press the Edit button on the gridview i get an error msg: Object reference not set to an instance of an object.
Debug the Event OnRowEditing="RowEditing".

Refer the most popular answer by Sergey ( @SAKryukov ) "Object Reference not set to Instance of an object", please see the code below.[^].
 
Share this answer
 
i think you are not at all binding the data to controls so try by binding them
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900