Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2005, 02:32 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default Index out of range error

I am working on Microsoft visual studio do net 2003.I am facing some problem while edit/delete data on database through datagrid.
When I click on button column delete it gives error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index "

Can you help me?


regards,
muskaan.


 
Old May 20th, 2005, 09:14 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You have an array index value that is out of range. .NET arrays and collections are 0 based unlike VB/Script where they are 1 based.

-Peter
 
Old May 23rd, 2005, 05:00 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter.

private void dgemp_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    try
    {
        SqlConnection myconn=new SqlConnection(strconn);
        string mysql="delete from insertdata where fname=@fname";
        SqlCommand cmd=new SqlCommand(mysql, myconn);
        cmd.Parameters.Add(new SqlParameter("@fname", dggrid.DataKeys[e.Item.ItemIndex]).ToString());
        myconn.Open();
        cmd.ExecuteNonQuery();
        myconn.Close();
        binddata();

}
    catch(SqlException ex)
    {
        lblexec.Text =ex.Message;
    }

    catch(Exception ex)
    {
        lblexec.Text =ex.Message;
    }
}

Plz help me out.
Regards,
Muskaan.

 
Old May 27th, 2005, 02:41 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

Please Look at code

, dggrid.DataKeys[e.Item.ItemIndex]).ToString()

I think you missed a "(" before dggrid

Also try to print the e.Item.ItemIndex
and dggrid.DataKeys[e.Item.ItemIndex].ToString
and see whether you get the value

Prashant






Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative raghavendra_das ASP.NET 2.0 Professional 0 July 24th, 2008 08:16 AM
Crystal reports error : Index was out of range. vhrao Crystal Reports 0 October 13th, 2006 12:00 PM
index out of range datagrid muchengeti ASP.NET 2.0 Professional 0 April 21st, 2006 12:43 PM
Index Out of range exception PbsiGuru General .NET 3 March 22nd, 2004 11:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.