473,398 Members | 2,343 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,398 software developers and data experts.

How to fix "index out of range" error when user submits no data in form?

Hi,

I am receiving an error if user is not selecting any row in data grid and clicking on the Apply button. The message is
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I want to put a check for it but unable to do it.

Thanks in advance
Feb 6 '11 #1
5 3533
Stewart Ross
2,545 Expert Mod 2GB
Please post the event code from your Apply button.

-S
Feb 6 '11 #2
if you are getting the index of a list control, the selected index will be '-1' if nothing is selected. That way you can check if anything was selected because selections start at 0.
Feb 6 '11 #3
Thanks, sending you code for Apply button

Expand|Select|Wrap|Line Numbers
  1.  void btnApply_Click(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 if (SelectedID == null)
  6.                 {
  7.                     MessageBox.Show("Select a row from grid");
  8.                     dgriduser.Focus();
  9.  
  10.                 }
  11.  
  12.                 depinfo = new deptdata();
  13.                 depinfo.dept_no = dgriduser.SelectedRows[0].Cells[0].Value.ToString();
  14.  
  15.                 depinfo.dept_name = dgriduser.SelectedRows[0].Cells[1].Value.ToString();
  16.  
  17.                 this.DialogResult = DialogResult.OK;
  18.                 this.Close();
  19.             }
  20.             catch (Exception ex)
  21.             {
  22.  
  23.                 MessageBox.Show(ex.Message);
  24.             }
  25.         } // End of btnApply_Click Event
Feb 7 '11 #4
Stewart Ross
2,545 Expert Mod 2GB
Have you tried Jason's suggestion about the index for the list control?

-S
Feb 7 '11 #5
I am using this code but still i am getting the same message.
if (cmbsearch.SelectedIndex < 0)
{
MessageBox.Show("Select a row from grid");
dgriduser.Focus();
}
The control is shifting back to combo box but the exception is generating
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Please guide me.
Feb 17 '11 #6

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

Similar topics

2
by: Jordan O'Hare | last post by:
Hello Everyone, I am after some help with the following: I have a windows application that contains a list box and two data grids. All three controls are binded to a dataset that contains...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
3
by: Snake | last post by:
I have a vb .net program which fills a data grid upon form load from an acccess database. This works great. Now, I have to add a combo box and use it to alter the underlying sql statement and...
2
by: Brian Henry | last post by:
Hi, I have a data grid that is set up like this Page items displayed = 10 EnableViewState = false (i dont want to send large amounts of data over the internet!) CustomPaging = false...
1
by: Linda | last post by:
Hi, I'm new to asp.net and tryign to learn how to work with data grids. I'm retrieving data from a stored procedure and putting it in a data grid. I then want to be able to sort the data when...
3
by: pmud | last post by:
Hi, I have a web page (asp.net, code:c#). I havean html table with text boxes. Based on the user input , records are displayed in the data grid below it. Now the datagrid has a large no. of...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
0
by: hlam | last post by:
Help - Calculating the total of a column in a data grid -- when data grid is part of Master-Detail set-up I have setup a Master-Detail form using Visual Studio.Net. A ListBox is the (Master)...
2
by: nullref | last post by:
Hello. I am wondering about the ability of the data grid for being able to maintain state of data when paginating. I am trying to figure out if I can use the grid to enable editing of data that...
0
by: zafar | last post by:
I don't know what property should be used for hiding colums in Data Grid, whereas in Data Grid View we have DataGridView1.Colums(index).Visible = False , But how can I hide Colums in Data Grid.....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.