Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {

int ipimg = (int)GridView1.DataKeys[e.RowIndex].Value;
 objcmd = new SqlCommand("DELETE FROM TBL_PDT_IMG WHERE IMG_ID='" + ipimg + "'", con);
objcmd.ExecuteNonQuery();
  bind();
}



i have error on delete Rows.

that Error is <pre lang="sql">Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


what i am say? please help me
Posted
Comments
[no name] 4-Mar-14 4:40am    
post your .cs code here so that i can help you.
jeevakumar.T 4-Mar-14 4:56am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;


public partial class ProductImages : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\SVN\Ecommerce\App_Data\ECommerce.mdf;Integrated Security=True;User Instance=True");
//dbOperations objDO = new dbOperations();
DataTable dt = new DataTable();
//dbconnection objcon = new dbconnection();
//string strValue = string.Empty, status = string.Empty, strField = string.Empty;
//bool result;

SqlCommand objcmd = new SqlCommand();
//SqlDataAdapter objda = new SqlDataAdapter();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bind();
fillProduct();
}
public void conn()
{
if (con.State == ConnectionState.Open) con.Close();
con.Open();
objcmd.Connection = con;
}

protected void Save(object sender, EventArgs e)
{

conn();
string ImgSmall = Path.GetFileName(fileuploadimgsmall.PostedFile.FileName);
fileuploadimgsmall.SaveAs(Server.MapPath("ImagesSmall/" + ImgSmall));

string ImgLarge = Path.GetFileName(fileuploadimglarge.PostedFile.FileName);
fileuploadimglarge.SaveAs(Server.MapPath("ImagesLarge/" + ImgLarge));

objcmd = new SqlCommand("Insert into TBL_PDT_IMG(PDT_ID,IMG_NAME,IMG_DESC,PDT_IMAGE_SMALL,PDT_IMAGE_LARGE,IS_PRIMARY) values(@PDT_ID,@IMG_NAME,@IMG_DESC,@PDT_IMAGE_SMALL,@PDT_IMAGE_LARGE,@IS_PRIMARY)",con);
fillProduct();
objcmd.Parameters.AddWithValue("@PDT_ID", ddlproduct.SelectedValue);
objcmd.Parameters.AddWithValue("@IMG_NAME", txtimgname.Text.Trim());
objcmd.Parameters.AddWithValue("@IMG_DESC", txtimgdesc.Text.Trim());
objcmd.Parameters.AddWithValue("@PDT_IMAGE_SMALL", "ImagesSmall/" + ImgSmall);
objcmd.Parameters.AddWithValue("@PDT_IMAGE_LARGE", "ImagesLarge/" + ImgLarge);
objcmd.Parameters.AddWithValue("@IS_PRIMARY", chkstatus.Checked);
objcmd.ExecuteNonQuery();

bind();


}


public void fillProduct()
{
//conn();
try
{
SqlCommand cmd = new SqlCommand("select PDT_ID,PDT_NAME from TBL_PDT",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddlproduct.DataSource = dt;
ddlproduct.DataTextField = "PDT_NAME";
ddlproduct.DataValueField = "PDT_ID";
ddlproduct.DataBind();
// return dt;
}
catch (Exception ex)
{
}

}



public void bind()
{
conn();
//fillProduct();
//con.Open();
//// objcon.con.Open();
objcmd = new SqlCommand("select * from TBL_PDT_IMG LEFT OUTER JOIN TBL_PDT ON TBL_PDT_IMG.PDT_ID=TBL_PDT.PDT_ID", con);
SqlDataAdapter da = new SqlDataAdapter(objcmd);
DataSet ds = new DataSet();
da.Fill(ds);
//con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
// GridView1.DataSource = objcmd.ExecuteReader();
// GridView1.DataBind();
////objcon.con.Close();
// con.Close();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Select")
{
// Img();

int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
lblpdtid.Text = GridView1.Rows[index].Cells[0].Text;
ddlproduct.SelectedItem.Text = GridView1.Rows[index].Cells[1].Text;
txtim
TrushnaK 4-Mar-14 4:52am    
check what you get in this line:-
int ipimg = (int)GridView1.DataKeys[e.RowIndex].Value;
Pranav-BiTwiser 4-Mar-14 8:57am    
solved???

Try something like,
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());

-KR
 
Share this answer
 
v2
we r revived this error when our indexing is not correct so pls check you indexing those are passing time of delete .
 
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