Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys , I tried in different ways. I am learning to Linq and Edm. I have a gridview with Edit, Update Deleting Events. If i am trying to update the row , i go the errors.


Source Code of Gridview :

XML
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#E7E7FF"
                        BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="false"
                        OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
                        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                        <AlternatingRowStyle BackColor="#F7F7F7" />
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <SortedAscendingCellStyle BackColor="#F4F4FD" />
                        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                        <SortedDescendingCellStyle BackColor="#D8D8F0" />
                        <SortedDescendingHeaderStyle BackColor="#3E3277" />
                        <Columns>
                            <asp:TemplateField HeaderText="Edit">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lbnEdit" runat="server" CausesValidation="False" CommandName="Edit"
                                        Text="Edit"></asp:LinkButton>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:LinkButton ID="lbnUpdate" runat="server" CausesValidation="True" CommandName="Update"
                                        Text="Update" OnClientClick="return confirm('Update?')"></asp:LinkButton>
                                    <asp:LinkButton ID="lbnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                        Text="Cancel"></asp:LinkButton>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Delete">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lbnDelete" runat="server" CausesValidation="False" CommandName="Delete"
                                        Text="Delete" OnClientClick="return confirm('Delete?')"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="SNO">
                                <ItemTemplate>
                                    <asp:Label ID="lblSno" runat="server" Text='<%# Bind("SNO") %>'>'></asp:Label>
                                </ItemTemplate>
                              <%--  <EditItemTemplate>
                                    <asp:TextBox ID="txtSno" runat="server" Text='<%# Bind("SNO") %>'>' ></asp:TextBox>
                                </EditItemTemplate>--%>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="BATCHNO">
                                <ItemTemplate>
                                    <asp:Label ID="lblBatch" runat="server" Text='<%# Bind("BATCHNO") %>'>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtBatch" runat="server" Text='<%# Bind("BATCHNO") %>'>' ></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="JOBNO">
                                <ItemTemplate>
                                    <asp:Label ID="lblJobNo" runat="server" Text='<%# Bind("JOBNO") %>'>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtJobNo" runat="server" Text='<%# Bind("JOBNO") %>'>' ></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="LOTNO">
                                <ItemTemplate>
                                    <asp:Label ID="lblLotNo" runat="server" Text='<%# Bind("LOTNO") %>'>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLotNo" runat="server" Text='<%# Bind("LOTNO") %>'>' ></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="MODIFIEDDATE">
                                <ItemTemplate>
                                    <asp:Label ID="lblModifiedDate" runat="server" Text='<%# Bind("MODIFIEDDATE") %>'>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtModifiedDate" runat="server" Text='<%# Bind("MODIFIEDDATE") %>'>' ></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>




DataBinding to Gridview :



C#
protected void Page_Load(object sender, EventArgs e)
   {
    binddata();
    }



C#
private void binddata()
    {
        LinqDataContext ld = new LinqDataContext();

        var query = (from p in ld.BATCH_LOTs select new { p.SNO, p.BATCHNO, p.JOBNO, p.LOTNO, p.MODIFIEDDATE }).Distinct().Take(10);

        GridView1.DataSource = query;

        GridView1.DataBind();
    }


Updating the Row :

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       
LinqDataContext ld = new LinqDataContext();
        int Sno = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("lblSno") as Label).Text);
        if (Sno == 0)
        {
            return;
        }
        var result = (from x in ld.BATCH_LOTs where x.SNO  == Sno select x).FirstOrDefault();
        result.BATCHNO  = (GridView1.Rows[e.RowIndex].FindControl("txtBatch") as TextBox).Text;
        result.JOBNO = (GridView1.Rows[e.RowIndex].FindControl("txtJobNo") as TextBox).Text;
        result.LOTNO = (GridView1.Rows[e.RowIndex].FindControl("txtLotNo") as TextBox).Text;
        result.MODIFIEDDATE = (GridView1.Rows[e.RowIndex].FindControl("txtModifiedDate") as TextBox).Text;
       ld.SubmitChanges();
        GridView1.EditIndex = -1;
        binddata();

   }





Deleting the Row of Gridview :

C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        LinqDataContext ld = new LinqDataContext();

        //Label sno = (Label)GridView1.Rows[e.RowIndex].FindControl("SNO");

        //int x1 = Convert.ToInt32(sno);

        string favoritesId = GridView1.Rows[e.RowIndex].Cells[0].Text;

        var delQuery =  from del in ld.BATCH_LOTs where del.SNO ==Convert.ToInt32( favoritesId) select del ;

 foreach (var x in delQuery)
        {
            ld.BATCH_LOTs.DeleteOnSubmit(x);
        }


        try
        {
            ld.SubmitChanges();

        }
        catch
        {
        }
        binddata();
    }





I got the Errors in Updating and Deleting the Rows of Gridview.
Posted
Updated 24-Oct-12 19:38pm
v4
Comments
Rajesh Kariyavula 23-Oct-12 2:03am    
What is the error that occurred on update and delete?
CH Guravaiah 23-Oct-12 2:06am    
In Update : Object reference not set to an instance of an object.

in Delete : Input string was not in a correct format.
Rajesh Kariyavula 23-Oct-12 2:16am    
In delete where exactly you are getting the error.

1 solution

hi
Update:
--------------
ID of label control that holds SNO is... "lblSno"
So change the code from...
int Sno = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("SNO") as Label).Text);


to
int Sno = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("lblSno") as Label).Text);


better code will be..

C#
int Sno = 0 ;
Label lblSno = GridView1.Rows[e.RowIndex].FindControl("lblSno") as Label
if (lblSno != null)
{
    int.TryParse(lblSno.Text, out Sno);
}


This way you can prevent error.

For Delete
---------------------

I think your cell index (i.e
Cells[0]
) is not right. plesae check the following line

Hope this will help you

string favoritesId = GridView1.Rows[e.RowIndex].Cells[0].Text;
 
Share this answer
 
Comments
sheshu036 6-Apr-14 13:02pm    
How update checkbox value?

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