Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using a datagrid in a webpage to update or delete data but get the above error.

here is the code of the datagrid to delete data:
(dgadvertise is the datagrid)
C#
protected void dgadvetise_Delete(object source, DataGridCommandEventArgs e)
    {
        try
        {
            string AdName = cls.FetchSingle("Select AdName from AdDetails where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], this.Page);
            if (File.Exists(Server.MapPath("Ad_Picture/" + AdName + ".jpg")))
                File.Delete(Server.MapPath("Ad_Picture/" + AdName + ".jpg"));
            cls.DeleteData("ADetails", "Where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], "Advertise Details Deleted", this.Page);
            dgadvetise.EditItemIndex = -1;
            BindGrid();
        }
        catch (Exception ex)
        {
            cls.MsgBox(ex.Message,this.Page);
        }
    }


Here is the app_Code/ClsCommon

C#
public string FetchSingle(string sqlQuery, Page form)
    {
        string functionReturnValue;
        functionReturnValue = "";
        try
        {
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            if (tbl.Rows.Count == 0)
            {
                return "";
            }
            functionReturnValue = tbl.Rows[0][0].ToString();
            tbl.Dispose();
            adp.Dispose();
        }
        catch (Exception ex)
        {
            MsgBox("Fetch Single Error: " + ex.Message, form);
            return "";
        }
        return functionReturnValue;
    }


C#
public void DeleteData(string tblName, string searchCodn, string msg, Page form)
    {
        try
        {
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["nayumaConnectionString"].ToString());
            conn.Open();
            cmd = new SqlCommand("Delete " + tblName + " " + searchCodn, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            if (msg != null)
                MsgBox(msg, form);
        }
        catch (Exception ex)
        {
            MsgBox("Delete Data Error: " + ex.Message, form);
        }
    }


Anyone can help me to solve this problem.
Thanks in advance.
Posted
Updated 11-Sep-11 18:30pm
v2
Comments
hitech_s 12-Sep-11 0:34am    
if u dont mind debug line by line u will get the place where exactly this error is raising..
then it is easy to fix... do it fast dont worry
sahabiswarup 12-Sep-11 0:55am    
i have added a break point on delete function; but after debugging the first line it goes to catch and shows the error.

You should indicate the line where the exception was thrown.

Anyway, you need to guarantee exactly what error message requires: use index such as 0 <= index <= Collection.Count.

—SA
 
Share this answer
 
Comments
sahabiswarup 12-Sep-11 1:02am    
after adding a break point at dgadvetise_Delete
1st line is debugged ie
string AdName = cls.FetchSingle("Select AdName from AdDetails where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], this.Page);
but then it goes to exception and display the above error message.
Sergey Alexandrovich Kryukov 12-Sep-11 9:59am    
Well, if your observation is correct, e.Item.ItemIndex should be in the range range 0 to dgadvetise.DataKeys-1, but it is not. Check it up before call and figure out if it's outside the range and why. Always use debugger or/and EventLog to see what's going on during runtime.
--SA
Change the following line

cls.DeleteData("ADetails", "Where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], "Advertise Details Deleted", this.Page);


to this following line


cls.DeleteData("AdDetails", "Where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], "Advertise Details Deleted", this.Page);



table name was changed.....



then also give reference to the DataKeyField as AdID



in design page like this...


<asp:DataGrid ID="dgadvetise" runat="server" DataKeyField="AdID">
 
Share this answer
 
v2
Comments
sahabiswarup 12-Sep-11 1:25am    
i have changed the table name, but at the time of debugging after debug the first line ie
string AdName = cls.FetchSingle("Select AdName from AdDetails where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], this.Page);
it goes to catch section and shows the above error message.

in my aspx i have written this:

<asp:DataGrid ID="dgadvetise" runat="server" AutoGenerateColumns="false"

onpageindexchanged="dgadvetise_PageIndexChanged" Height="459px"
oncancelcommand="dgadvetise_Cancel" ondeletecommand="dgadvetise_Delete"
oneditcommand="dgadvetise_Edit" onupdatecommand="dgadvetise_Update"
PageSize="5" Width="750px" AllowPaging="true" AllowSorting="true">
<PagerStyle HorizontalAlign="Center" Mode="NumericPages"
Position="TopAndBottom"/>

<columns>
<asp:BoundColumn DataField="AdID" HeaderText="Image ID" Visible="False" />
<asp:BoundColumn DataField="AdName" HeaderText="Name" />
<asp:BoundColumn DataField="AdLink" HeaderText="Link" />
<asp:TemplateColumn HeaderText="Image">
<itemtemplate>
<ul class="hoverbox">
<li>
<img id="imgSmall" src='Ad_Picture/<%# DataBinder.Eval(Container, "DataItem.AdImagePath") %>' alt="Gallery Image" width="100" height="100" />
</li>
</ul>

<edititemtemplate>
<asp:FileUpload ID="flUploadGrid" runat="server" ForeColor="#2A2F0F">

<HeaderStyle Width="110px" />

<asp:EditCommandColumn CancelText="Cancel" EditText="Edit" HeaderText="Modify" UpdateText="Update" />
<asp:ButtonColumn CommandName="Delete" Text="Delete" HeaderText="Remove" />
dgadvetise.DataKeys[e.Item.ItemIndex]

Check the value of dgadvetise.DataKeys.Count and e.Item.ItemIndex. The later should be smaller than the former.
 
Share this answer
 
Comments
sahabiswarup 12-Sep-11 1:48am    
yes i understand what you are trying to say; but can you give me any suggetion to solve this problem?

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