473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Index was out of range

i am deleting a row from grid view my code is as follows. I got an exception i.e.

Index was out of range. Must be non negative and less then the size of the collection. Parameter name: INDEX

Code:
Expand|Select|Wrap|Line Numbers
  1. private void bindingNavigatorDeleteItem_Click()
  2. {
  3. try
  4. {
  5. SqlDataAdapter da;
  6. DataSet ds = new DataSet();
  7. SqlConnection conn = new SqlConnection(@"Data Source=HARIS\SQLEXPRESS;Initial Catalog=Nesk;Integrated Security=True");
  8. da = new SqlDataAdapter();
  9.  
  10. SqlCommand cmd = new SqlCommand("sp_Nsk_PromoCriteriaDelete");
  11. cmd.CommandType = CommandType.StoredProcedure;
  12. cmd.Parameters.Add("@promoID", SqlDbType.VarChar).Value = System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0].Cells[0].Value);
  13. cmd.Connection = conn;
  14.  
  15. da.SelectCommand = cmd;
  16.  
  17. da.Fill(ds);
  18. conn.Close();
  19. }
  20. catch(Exception ex)
  21. {
  22. MessageBox.Show(ex.Message);
  23. }
  24. }
  25.  
Stored Procedure:
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[sp_Nsk_PromoCriteriaDelete]
  2. @PromoID varchar(20)
  3. AS BEGIN
  4.  
  5. SET NOCOUNT ON;
  6.  
  7. delete from Nsk_PromoCriteria where PromoId = @PromoID
  8.  
  9. END
  10.  
this piece of Code:
Expand|Select|Wrap|Line Numbers
  1. System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0].Cells[0].Value);
  2.  
Selected rows gives a value which is next to what i selected actually. Lets suppose if i select PromoID= 1 they take PromoID=2 and hence delete ID =2.

when i use:
Expand|Select|Wrap|Line Numbers
  1. System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0].Cells[0].Count);
  2.  
it returns 4 selected rows while i select 5
Jul 1 '08 #1
1 743
Plater
7,872 Expert 4TB
when i use:
Expand|Select|Wrap|Line Numbers
  1. System.Convert.ToInt32(nsk_PromoCriteriaDataGridView.SelectedRows[0].Cells[0].Count);
  2.  
it returns 4 selected rows while i select 5
Actually THAT code is invalid (copy/paste error I suspect). You probably ment nsk_PromoCriteriaDataGridView.SelectedRows.Count

When do you run this code? Some of the Selecting events occur "before" the selection is made. Your code example also shows you always using the "first" row in the selection. Are you checking to make sure there IS a row in the selectedrows ?
Jul 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Craig O'Shannessy | last post by:
Hi all, Just thought I'd mention that I really think this problem needs to be fixed. I I'm patching the 7.4RC1 JDBC drivers as we speak due to this optimiser bug, and it's the third time...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
2
by: kscdavefl | last post by:
When I run the following code: private void applicationPermissionGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType ==...
1
by: Clark Choi | last post by:
I ran the sample application called Petstore from msdn. Everything went fine until I tested Update button on the web form to update the database through Datagrid. I have been searching the web to...
14
by: micklee74 | last post by:
hi say i have string like this astring = 'abcd efgd 1234 fsdf gfds abcde 1234' if i want to find which postion is 1234, how can i achieve this...? i want to use index() but it only give me the...
0
by: ssims | last post by:
I've got a GridView that's sorted by a stored procedure with ROW_NUMBER: PROCEDURE dbo.GetCalendarsByStatusIDPaged ( @startRowIndex int, @maximumRows int, @statusID int ) AS
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello As I debug the C# code with a break point and by pressing F11 I eventually get a message stating: ContextSwitchDeadlock was detected Message: The CLR has been unable to transition from...
6
by: shashi shekhar singh | last post by:
Respected Sir, I am facing problem when i try to deploy my website on iis 7.0 on test page. i have to display some .mht files on iframe in gridview and error looks like below, Server Error in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.