locked
Gridview row is editing wrong row when search row and click on edit link RRS feed

  • Question

  • Hi,

    I have gridview in that there is edit link by using edit link i can edit the record.it's working perfect.but when i search record using search option in the gridview its showing me search record from gridview but when i click on edit link of search record its not editing search record its editing first record of gridview.For reference i have attached screenshots of gridview.Please provide me soution to overcome this issue.

    Thanks,

    Masroor

    Tuesday, July 19, 2016 6:32 AM

All replies

  • Hi,

    Please check if you have set the gridview editindex in Edit_Row void and trace if the editindex is reset in search void.

    Here is a detailed code demo about edit row in SharePoint gridview for your reference:

    Update SharePoint List Data in a GridView

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, July 20, 2016 7:23 AM
  • Hi Jerry,

    Thanks for your precious time.

    As per your suggestion already i have set the gridview editindex in Edit_Row.

    For your reference i have attached my snippet code.Please review my code and provide me better solution.

    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
        Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
        Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DashBoardUserControl.ascx.cs"
        Inherits="BMS.WebParts.DashBoard.DashBoardUserControl" %>
    <script type="text/javascript">
    
        function validateSequenceNo(source, arguments) {
            if (arguments.Value.length > 4) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateDescription(source, arguments) {
            if (arguments.Value.length > 40) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateMedia(source, arguments) {
            if (arguments.Value.length > 25) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateLocation(source, arguments) {
            if (arguments.Value.length > 25) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateAddress(source, arguments) {
            if (arguments.Value.length > 25) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateBackupType(source, arguments) {
            if (arguments.Value.length > 4) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateCode(source, arguments) {
            if (arguments.Value.length > 4) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }
    
        function validateName(source, arguments) {
            if (arguments.Value.length > 25) {
                arguments.IsValid = false;
            }
            else {
                arguments.IsValid = true;
            }
        }    
    </script>
    <style type="text/css">
        tr.ms-itmhover:hover > td, table.ms-listviewtable tbody tr:hover td.ms-vb-firstcell, .ms-listviewtable tbody tr:hover td.ms-vb2, .ms-listviewtable tbody tr:hover td.ms-vb-icon, .ms-listviewtable tbody tr:hover td.ms-vb-title
        {
            border-color: #BEA55A !important;
            background-image: none;
            background-color: #CACACA;
        }
        .btnStyle
        {
            background-color: #da342b;
            border: none;
            color: White;
            padding: 6px 15px;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }
        .ms-rte-layoutszone-inner
        {
            margin-left: -100px !important;
        }
        .ms-viewheadertr
        {
            color: #0072bc !important;
        }
        .ms-WPHeader TD
        {
            display: none;
        }
        .ms-vb2
        {
            padding-left: 10px !important;
            color: #959cad !important;
        }
        .ms-WPBody TD
        {
            font-size: 9pt;
        }
        .pager span
        {
            background-color: #da342b;
        }
    </style>
    <div id="tapeContent" style="width: 1400px; margin-top: -15px">
        <div id="searchContent" style="width: 100%">
            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr style="text-align: left">
                    <td>
                        <label>
                            Please Enter Label Number:</label>&nbsp;
                        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>&nbsp;
                        <asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="btnStyle" OnClick="btnSearch_Click" />&nbsp;
                        <asp:Button ID="btnClear" runat="server" Text="Clear" CssClass="btnStyle" OnClick="btnClear_Click" />
                    </td>
                    <td>
                        <div id="divMsg" style="margin-left: 10px; padding-top: 10px">
                            <asp:Label ID="lblMsg" runat="server" Font-Size="12pt" ForeColor="Red" EnableViewState="false" />
                            <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <div id="divGridTape" style="width: 100%">
            <table cellpadding="0" cellspacing="0" border="1" width="100%">
                <tr>
                    <td>
                        <SharePoint:SPGridView ID="grdTapeInventory" runat="server" Width="100%" AutoGenerateColumns="false"
                            CellSpacing="2" AllowPaging="true" PageSize="20" OnPageIndexChanging="grdTapeInventory_OnPageIndexChanging"
                            OnRowEditing="grdTapeInventory_OnRowEditing" OnRowCancelingEdit="grdTapeInventory_OnRowCancelingEdit"
                            OnRowUpdating="grdTapeInventory_OnRowUpdating" OnRowCommand="grdTapeInventory_OnRowCommand"
                            OnRowDataBound="grdTapeInventory_OnRowDataBound">
                            <Columns>
                                <asp:BoundField HeaderText="Label Number" DataField="LabelNbr" ReadOnly="true" HeaderStyle-BackColor="#bdc5d7"
                                    HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="50px" />
                                <asp:TemplateField HeaderText="Sequence Number" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblSequenceNo" runat="server" Text='<%# Eval("BatchNbr") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="50px" />
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtSequenceNo" runat="server" Text='<%# Eval("BatchNbr") %>' Width="50px" />
                                        <asp:RequiredFieldValidator ID="rfvSequenceNo" ControlToValidate="txtSequenceNo"
                                            ErrorMessage="Sequence Number Can't be blank!" Text="*" SetFocusOnError="true"
                                            Display="Dynamic" runat="server" />
                                        <asp:RegularExpressionValidator ID="regvSequenceNo" ControlToValidate="txtSequenceNo"
                                            ErrorMessage="Sequence Number Should be only numeric!" Text="*" ValidationExpression="^\d+$"
                                            SetFocusOnError="true" Display="Dynamic" runat="server" />
                                        <asp:CustomValidator ID="cusvSequenceNo" ControlToValidate="txtSequenceNo" runat="server"
                                            ErrorMessage="Sequence No should not exceed more than 4 Characters!" SetFocusOnError="true"
                                            Display="Dynamic" ClientValidationFunction="validateSequenceNo" Text="*">
                                        </asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Description" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Descreption") %>' Width="200px"></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="100px" />
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtDescription" runat="server" Text='<%# Eval("Descreption") %>'
                                            TextMode="MultiLine" Rows="2" Width="220px" />
                                        <asp:RequiredFieldValidator ID="rfvDescription" ControlToValidate="txtDescription"
                                            ErrorMessage="Description Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic"
                                            runat="server" />
                                        <asp:CustomValidator ID="cusvDescription" ControlToValidate="txtDescription" runat="server"
                                            ErrorMessage="Description should not exceed more than 40 Characters!" SetFocusOnError="true"
                                            Display="Dynamic" ClientValidationFunction="validateDescription" Text="*">
                                        </asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Backup Date" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblBackupDate" runat="server" Text='<%# Eval("BackupDate") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="70px" />
                                    <EditItemTemplate>
                                        <SharePoint:DateTimeControl ID="spdtBackupDate" runat="server" DateOnly="true" />
                                        <asp:RequiredFieldValidator ID="rfvBackupDate" ControlToValidate="spdtBackupDate$spdtBackupDateDate"
                                            ErrorMessage="Backup Date Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic"
                                            runat="server" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="70px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Media Type" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblMediaType" runat="server" Text='<%# Eval("MediaType") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="100px" />
                                    <EditItemTemplate>                                    
                                        <asp:TextBox ID="txtMediaType" runat="server" Width="100px" Text='<%# Eval("MediaType") %>' />
                                        <asp:RequiredFieldValidator ID="rfvMedia" ControlToValidate="txtMediaType" runat="server"
                                            ErrorMessage="Media Type Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic" />
                                        <asp:CustomValidator ID="cusvMedia" ControlToValidate="txtMediaType" runat="server"
                                            ErrorMessage="Media should not exceed more than 25 Characters!" SetFocusOnError="true"
                                            Display="Dynamic" ClientValidationFunction="validateMedia" Text="*"></asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Location" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblLocation" runat="server" Text='<%# Eval("LocName") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="100px" />
                                    <EditItemTemplate>                                    
                                        <asp:TextBox ID="txtLocation" runat="server" Text='<%# Eval("LocName") %>' Width="100px" />
                                        <asp:RequiredFieldValidator ID="rfvLocation" ControlToValidate="txtLocation" runat="server"
                                            ErrorMessage="Location Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic" />
                                        <asp:CustomValidator ID="cusvLocation" ControlToValidate="txtLocation" runat="server"
                                            ErrorMessage="Location should not exceed more than 25 Characters!" SetFocusOnError="true"
                                            Display="Dynamic" ClientValidationFunction="validateLocation" Text="*"></asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Address" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("LocAdd") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="100px" />
                                    <EditItemTemplate>                                    
                                        <asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("LocAdd") %>' Width="100px" />
                                        <asp:RequiredFieldValidator ID="rfvAddress" ControlToValidate="txtAddress" runat="server"
                                            ErrorMessage="Address Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic" />
                                        <asp:CustomValidator ID="cusvAddress" ControlToValidate="txtAddress" runat="server"
                                            ErrorMessage="Address should not exceed more than 25 Characters!" SetFocusOnError="true"
                                            Display="Dynamic" ClientValidationFunction="validateAddress" Text="*"></asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Backup Type" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblBackupType" runat="server" Text='<%# Eval("CycleType") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="70px" />
                                    <EditItemTemplate>                                    
                                        <asp:TextBox ID="txtBackupType" runat="server" Text='<%# Eval("CycleType") %>' Width="50px" />
                                        <asp:RequiredFieldValidator ID="rfvBackupType" ControlToValidate="txtBackupType"
                                            runat="server" ErrorMessage="Backup Type Can't be blank!" Text="*" SetFocusOnError="true"
                                            Display="Dynamic" />
                                        <asp:CustomValidator ID="cusvBackupType" ControlToValidate="txtBackupType" runat="server"
                                            ErrorMessage="Backup Type should not exceed more than 4 Characters!" SetFocusOnError="true"
                                            Display="Dynamic" ClientValidationFunction="validateBackupType" Text="*"></asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Code" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblCode" runat="server" Text='<%# Eval("CycleCode") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="70px" />
                                    <EditItemTemplate>                                    
                                        <asp:TextBox ID="txtCode" runat="server" Text='<%# Eval("CycleCode") %>' Width="50px" />
                                        <asp:RequiredFieldValidator ID="rfvCode" ControlToValidate="txtCode" runat="server"
                                            ErrorMessage="Code Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic" />
                                        <asp:CustomValidator ID="cusvCode" ControlToValidate="txtCode" runat="server" ErrorMessage="Code should not exceed more than 4 Characters!"
                                            SetFocusOnError="true" Display="Dynamic" ClientValidationFunction="validateCode"
                                            Text="*"></asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Name" HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("CycleName") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle Width="120px" />
                                    <EditItemTemplate>                                    
                                        <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("CycleName") %>' Width="100px" />
                                        <asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName" runat="server"
                                            ErrorMessage="Name Can't be blank!" Text="*" SetFocusOnError="true" Display="Dynamic" />
                                        <asp:CustomValidator ID="cusvName" ControlToValidate="txtName" runat="server" ErrorMessage="Name should not exceed more than 25 Characters!"
                                            SetFocusOnError="true" Display="Dynamic" ClientValidationFunction="validateName"
                                            Text="*"></asp:CustomValidator>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <SharePoint:SPSecurityTrimmedControl runat="server" PermissionsString="FullMask">
                                            <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" />
                                        </SharePoint:SPSecurityTrimmedControl>
                                    </ItemTemplate>
                                    <ItemStyle Width="100px" />
                                    <EditItemTemplate>
                                        <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update"
                                            CommandArgument='<%#Eval("TapeNbr") %>' />
                                        <asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel"
                                            CausesValidation="false" />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderStyle-BackColor="#bdc5d7" HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <SharePoint:SPSecurityTrimmedControl runat="server" PermissionsString="FullMask">
                                            <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Remove"
                                                CausesValidation="false" CommandArgument='<%#Eval("TapeNbr") %>' OnClientClick="return confirm('Are you sure to delete this record?');" />
                                            </span>
                                        </SharePoint:SPSecurityTrimmedControl>
                                    </ItemTemplate>
                                    <ItemStyle Width="70px" />
                                </asp:TemplateField>
                            </Columns>
                            <RowStyle BackColor="#DDDDDD" Height="33px" />
                            <EditRowStyle BackColor="#efefef" />
                            <AlternatingRowStyle BackColor="White" />
                            <PagerStyle BackColor="#bdc5d7" Font-Bold="true" ForeColor="White" Height="33px"
                                HorizontalAlign="Center" CssClass="pager" />
                        </SharePoint:SPGridView>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    


    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using System.Data;
    using Microsoft.SharePoint.WebControls;
    using System.Configuration;
    using System.Data.SqlClient;
    
    
    namespace BMS.WebParts.DashBoard
    {    
        public partial class DashBoardUserControl : UserControl
        {
            public string tapeNBR;
            DataTable dtTape = new DataTable();
            string cs = ConfigurationManager.ConnectionStrings["BMSCS"].ConnectionString;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                grdTapeInventory.PagerTemplate = null;
                if (!IsPostBack)
                {
                    BindGrid();
                }
            }
    
            private void BindGrid()
            {
                try
                {                
                    using (SqlConnection con = new SqlConnection(cs))
                    {
                        SqlDataAdapter da = new SqlDataAdapter("select* from TapeInventory order by created desc", con);                    
                        da.Fill(dtTape);
                        ViewState["Inventory"] = dtTape;
                        grdTapeInventory.DataSource = ViewState["Inventory"];
                        grdTapeInventory.DataBind();
                    }
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
            }
    
            protected void btnSearch_Click(object sender, EventArgs e)
            {
                SearchTapeInventory();
            }
    
            protected void btnClear_Click(object sender, EventArgs e)
            {
                ClearControl();
            }
    
            private void ClearControl()
            {
                txtSearch.Text = "";            
                BindGrid();
            }
            
            private void SearchTapeInventory()
            {
                try
                {                
                    using (SqlConnection con = new SqlConnection(cs))
                    {
                        SqlDataAdapter da = new SqlDataAdapter("select* from TapeInventory where LabelNbr='" + txtSearch.Text + "' ", con);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        grdTapeInventory.DataSource = ds;
                        grdTapeInventory.DataBind();
                    }
                }
                catch (Exception ex)
                {
    
                    ex.Message.ToString();
                }
            }        
            
            protected void grdTapeInventory_OnRowCommand(object sender, GridViewCommandEventArgs e)
            {
                tapeNBR = e.CommandArgument.ToString();
                if (e.CommandName == "Remove")
                {
                    using (SqlConnection con = new SqlConnection(cs))
                    {
                        string sql = "Delete from TapeInventory where @TapeNbr=TapeNbr";
                        using (SqlCommand cmd = new SqlCommand(sql, con))
                        {
                            cmd.Parameters.AddWithValue("@TapeNbr", tapeNBR);
                            con.Open();
                            cmd.ExecuteNonQuery();
                        }
                    }
                    lblMsg.Text = "Item has been deleted successfully!";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                    BindGrid();
                }
            }        
    
            protected void grdTapeInventory_OnRowEditing(object sender, GridViewEditEventArgs e)
            {
                grdTapeInventory.EditIndex = e.NewEditIndex;
                BindGrid();
            }
    
            protected void grdTapeInventory_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                grdTapeInventory.EditIndex = -1;
                BindGrid();
            }
    
            protected void grdTapeInventory_OnRowDataBound(object sender, GridViewRowEventArgs e)
            {            
                DataRowView drview = e.Row.DataItem as DataRowView;
    
                //First check if the row is in Edit mode
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                    {
                        #region Backup Date
    
                        try
                        {
                            //Find the Backup Date DateTime Control
                            DateTimeControl dtcBackup = ((DateTimeControl)e.Row.FindControl("spdtBackupDate"));
                            dtcBackup.SelectedDate = Convert.ToDateTime(drview[3].ToString());
                        }
                        catch (Exception ex)
                        {
                            ex.Message.ToString();
                        }
    
                        #endregion
                        
                    }
                }            
            }
                    
            protected void grdTapeInventory_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
            {            
                //Find the controls from Gridview for the row which is going to update
                TextBox tSequence = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[1].FindControl("txtSequenceNo");
                TextBox tDescription = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[2].FindControl("txtDescription");
                DateTimeControl dtBackupDate = (DateTimeControl)grdTapeInventory.Rows[e.RowIndex].Cells[3].FindControl("spdtBackupDate");
                TextBox tMediaType = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[4].FindControl("txtMediaType");
                TextBox tLocation = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[5].FindControl("txtLocation");
                TextBox tAddress = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[6].FindControl("txtAddress");
                TextBox tBackupType = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[7].FindControl("txtBackupType");
                TextBox tCode = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[8].FindControl("txtCode");
                TextBox tName = (TextBox)grdTapeInventory.Rows[e.RowIndex].Cells[9].FindControl("txtName");
                
    
                try
                {                
                    using (SqlConnection con = new SqlConnection(cs))
                    {
                        con.Open();
                        string strUpdateCommand = "update TapeInventory set BatchNbr='" + tSequence.Text +"',Descreption='" + tDescription.Text.ToUpper() + "',BackupDate='" + dtBackupDate.SelectedDate.ToShortDateString() +
                                                    "',MediaType='" + tMediaType.Text.ToUpper() + "',LocName='" + tLocation.Text.ToUpper() + "',LocAdd='" + tAddress.Text.ToUpper() +
                                                    "',CycleType='" + tBackupType.Text.ToUpper() + "',CycleName='" + tName.Text.ToUpper() + "',CycleCode='" + tCode.Text.ToUpper() + "' where @TapeNbr=TapeNbr";                                
                        using (SqlCommand cmd = new SqlCommand(strUpdateCommand,con))
                        {
                            cmd.Parameters.AddWithValue("@TapeNbr", tapeNBR);
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
                lblMsg.Text = "TapeInventory Record has been Updated Successfully!";
                grdTapeInventory.EditIndex = -1;
                BindGrid();            
            }
    
            protected void grdTapeInventory_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                grdTapeInventory.PageIndex = e.NewPageIndex;
                BindGrid();
            }              
        }
    }
    
    
    
    
    

    Thanks,

    Masroor

    Wednesday, July 20, 2016 12:55 PM
  • Hi, 

    For a better troubleshooting, I suggest you can debug the code line step by step to trace the if the editindex is valid.

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, July 22, 2016 2:09 AM