User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 401,489 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,215 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 495 | Replies: 4
Reply
Join Date: Jul 2008
Posts: 2
Reputation: harisyahoo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harisyahoo harisyahoo is offline Offline
Newbie Poster

Index was out of range>>>>>

  #1  
Jul 2nd, 2008
Haris Hussain - Posted on Monday, June 30, 2008 23:15:15

I am facing a following exception. While am deleting a row from gridview. I am using VS 2005.


Exception: Index was out of range.Parameter must be non negative and less than the index collection. Parameter Name : INDEX





private void bindingNavigatorDeleteItem_Click()
{
try
{
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(@"Data Source=HARIS\SQLEXPRESS;Initial Catalog=Nesk;Integrated Security=True");
da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_Nsk_PromoCriteriaDelete");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@promoID", SqlDbType.VarChar).Value = System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0].Cells[0].Value);
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds);
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}




Stored Procedure:
ALTER PROCEDURE [dbo].[sp_Nsk_PromoCriteriaDelete]
@PromoID varchar(20)
AS BEGIN
SET NOCOUNT ON;
delete from Nsk_PromoCriteria where PromoId = @PromoID
END


Property Set:

RowTemplate: DataGridViewRow Index{-1
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 48
Reputation: Ken Sharpe is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
Ken Sharpe's Avatar
Ken Sharpe Ken Sharpe is offline Offline
Light Poster

Re: Index was out of range>>>>>

  #2  
Jul 2nd, 2008
What line throws your exception?
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: harisyahoo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harisyahoo harisyahoo is offline Offline
Newbie Poster

Re: Index was out of range>>>>>

  #3  
Jul 3rd, 2008
Originally Posted by Ken Sharpe View Post
What line throws your exception?


System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0]

throws an exception.


SelectedRows[0] throws an exception while i select last record. There are six rows in my table.
when i select last row it throws an exception while i select upper 5 rows it selects the very next row that i selected.


when i use selectedrows[0].count it gives value 5. While there is six rows in my tables.
Reply With Quote  
Join Date: Oct 2006
Posts: 27
Reputation: yilmazhuseyin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 3
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: Index was out of range>>>>>

  #4  
Jul 5th, 2008
I checked MSDN page for selectedrows property. it says
The SelectionMode property must be set to FullRowSelect or RowHeaderSelect for the SelectedRows property to be populated with selected rows.
just check SelectionMode property on properties window and make sure it is set to fullrow select. and make sure that it is not empty before you check. I'm sure there is an isEmpty or length function in the DataGridView.
Reply With Quote  
Join Date: Mar 2007
Posts: 50
Reputation: FaridMasood is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
FaridMasood's Avatar
FaridMasood FaridMasood is offline Offline
Junior Poster in Training

Re: Index was out of range>>>>>

  #5  
Jul 9th, 2008
Do you tried this .,
System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows.Cells[0].Value);
Thanks and Best of Lusk,

Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 2:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC