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

ArgumentOutOfRangeException was unhandled

Hi everybody, I have a trouble with ArgumentOutOfRangeException. When I type a French word in an AutoCompleteComboBox then submit it to add new record to database, the combo box throw an exception:
ArgumentOutOfRangeException was unhandled:
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

This exception appears in line:
addr = cboAddr.Text;

And here's the code of AutoCompleteComboBox class:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.ComponentModel;
  7. using System.Data.OleDb;
  8. using System.Data.DataSetExtensions;
  9. using System.Data;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.  
  14.     public class AutoCompleteComboBox : System.Windows.Forms.ComboBox
  15.     {
  16.         public event System.ComponentModel.CancelEventHandler NotInList;
  17.  
  18.         //private bool _limit;
  19.  
  20.         private bool _limitToList = true;
  21.         private bool _inEditMode = false;
  22.  
  23.         public AutoCompleteComboBox() : base()
  24.         {
  25.         }
  26.  
  27.         public bool LimitToList
  28.         {
  29.             get { return _limitToList; }
  30.  
  31.             set { _limitToList = value; }
  32.         }
  33.  
  34.         protected virtual void OnNotInList(System.ComponentModel.CancelEventArgs e)
  35.         {
  36.             if (NotInList != null)
  37.             {
  38.                 NotInList(this, e);
  39.             }
  40.         }
  41.  
  42.         protected override void OnTextChanged(System.EventArgs e)
  43.         {
  44.             if (_inEditMode)
  45.             {
  46.  
  47.                 string input = Text;
  48.                 int i;
  49.  
  50.                 //int index = FindString(input);
  51.                 while (this.Items.Count > 0)
  52.                 {
  53.                     this.Items.RemoveAt(0);
  54.                 }
  55.                 this.DroppedDown = true;
  56.                 Controldb db = new Controldb();
  57.                 DataTable myTable = new DataTable();
  58.                 string combox_text = this.Text;
  59.                 string query = "";
  60.                 query = "SELECT DISTINCT adresse FROM tblMain WHERE trans = 'in' AND " + "adresse LIKE '%" + combox_text + "%'";
  61.                 try
  62.                 {
  63.                     myTable = db.getTable(query);
  64.                 }
  65.                 catch (Exception e1) {
  66.                     MessageBox.Show(e1.ToString());
  67.                 }
  68.                 if (myTable.Rows.Count > 0)
  69.                 {
  70.                     foreach (DataRow myDataRow in myTable.Rows)
  71.                     {
  72.                         this.Items.Add(myDataRow["adresse"].ToString());
  73.  
  74.                     }
  75.                 }
  76.  
  77.             }
  78.  
  79.             base.OnTextChanged(e);
  80.         }
  81.  
  82.         protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
  83.         {
  84.             if (this.LimitToList)
  85.             {
  86.                 int pos = this.FindStringExact(this.Text);
  87.  
  88.                 if (pos == -1)
  89.                 {
  90.                     OnNotInList(e);
  91.                 }
  92.                 else
  93.                 {
  94.                     this.SelectedIndex = pos;
  95.                 }
  96.             }
  97.  
  98.             base.OnValidating(e);
  99.         }
  100.  
  101.         protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
  102.         {
  103.             _inEditMode = (e.KeyCode !=Keys.Up && e.KeyCode != Keys.Down);
  104.             base.OnKeyDown(e);
  105.  
  106.         }
  107.  
  108.     }
  109. }

Can somebody help me?? Is there anything wrong with my AutoCompleteComboBox??
Thanks in advanced!
Oct 22 '09 #1
6 7612
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Oct 22 '09 #2
@tlhintoq
Yep, thanks tlhintoq! I'll remember next time
Oct 22 '09 #3
tlhintoq
3,525 Expert 2GB
When I type a French word in an AutoCompleteComboBox then submit it to add new record to database, the combo box throw an exception:
ArgumentOutOfRangeException was unhandled:
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Does it really only happen with french words?

This exception appears in line:
addr = cboAddr.Text;
That line of code doesn't appear in the code you provided. How is it meant to help?

ArgumentOutOfRangeException was unhandled:
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
Whatever range you are using, '0' is not a valid choice.
If you have an array, 0 would be the first item. If your array has NO items, then you can't access the first one which is index 0.
Oct 22 '09 #4
First, it happens with French words such as: é; è; ç... and Vietnamese words when I'm trying to submit data from an input form to my database.
Second, the exception is thrown on the line:
Expand|Select|Wrap|Line Numbers
  1. addr = cboAddr.Text;
The code above belong to the input form when I assign value of cboAddr.Text to addr.

Here's the input form code, but I don't think it's nessecery:
Expand|Select|Wrap|Line Numbers
  1. private void btnOK_Click(object sender, EventArgs e)
  2.         {
  3.             DateTime date = Convert.ToDateTime( dateTimePicker1.Value);
  4.             double montant;
  5.  
  6.  
  7.             montant = Convert.ToDouble(txtMontant.Text);
  8.             string type = "";
  9.             string nom;
  10.             string addr;
  11.             if (radCheque.Checked)
  12.                 type = "cheque";
  13.             if (radEspese.Checked)
  14.                 type = "espese";
  15.             if (radCredit.Checked)
  16.                 type = "credit";
  17.  
  18.             nom = txtNewClient.Text;
  19.             addr = cboAddr.Text;
  20.  
  21.             string sqlQuery;
  22.             if (status == 2)
  23.             {
  24.                 if (MessageBox.Show("Edit?", "Confirm!", MessageBoxButtons.YesNo) == DialogResult.Yes)
  25.                     sqlQuery = "UPDATE tblMain SET [date] = '"+date+"',  type= '" + type + "', montant = " + montant + ", nom = '" + nom + "', adresse = '" + addr + "'WHERE id = " + edit_id + ";";
  26.                 else
  27.                     return;
  28.             }
  29.             else
  30.             {
  31.                 if (MessageBox.Show("Add?", "Confirm!", MessageBoxButtons.YesNo) == DialogResult.Yes)
  32.  
  33.                     sqlQuery = "INSERT INTO tblMain ( [date], type, montant, nom, adresse, trans )VALUES ('"+date+"' , '" + type + "', " + montant + ",'" + nom + "' ,'" + addr + "' ,'in' );";
  34.                 else
  35.                     return;
  36.             }
  37.             Controldb db = new Controldb();
  38.             db.executeQuery(sqlQuery);
  39.             edit_id = 0;
  40.             status = 1;
  41.             this.Close();
  42.             frmIn_FormClosed(sender, null);
  43.  
  44.         }
  45.  
But I think this is because cboAddr is an AutoCompleteComboBox, not an ordinary combobox, so I provide the AutoCompleteComboBox class. I don't know why the AutoCompleteCombobox can not be assigned a French character??
Oct 22 '09 #5
tlhintoq
3,525 Expert 2GB
Check if this will at least keep it from producing and error

if (cboAddr.Text != null) addr = cboAddr.Text;
Oct 22 '09 #6
Yep, thanks tlhintoq!
I tried the code you provided and there's no exception. But whenever I enter a French word the cboAddr.Text becomes null and the query can not be executed.
I did it in another way:
First I insert a new label in the input form and set its visible to false.
When there is a text update event on the combo box, I push the cboAddr.Text value to label.Text.
And then I push the label.Text value to the query, so I can type any word I need ^^.
Of course, I'll find out the reason later ^^.
Oct 23 '09 #7

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

Similar topics

1
by: Brian | last post by:
I'm having intermittent trouble with a call to the Read method of the HttpWebResponse object. I get an ArumentOutOfRangeException claiming that deep down inside of the Read method, the count...
7
by: Dustin B | last post by:
Since this issue encompasses so many things - I have to generally post it I guess. I'm building an application for a Wireless PDA - when it is connected to the Network I tested a TCPListener &...
1
by: Hozi | last post by:
I am getting the below error when asp.net tries to parse the .aspx file. The funny part is that the error only seems to be happening when viewing the page throuh IE 6 and not Netscape browsers. I...
3
by: Pieter | last post by:
Hi, I have a Windows Forms application (VB.NET 2.0) which uses a Class Library in C#. The application runs fine, but when I run it in debug mode, I get several "A first chance exception of type...
1
by: ÀÏÆÅ»³ÔÐ6¸öÔ | last post by:
how can i override the "ArgumentOutOfRangeException" or catch it in my dorpdownlist?
0
by: Martin Henke | last post by:
Dear all, I've got a problem with my custom combo box. When I select an entry from it, then I get the error message: "An unhandled exception of type 'System.ArgumentOutOfRangeException'...
2
by: loga123 | last post by:
Hi All, I am using Link Button for DELETE on the gridview. When I click on DELETE link, I get the ArgumentOutOfRangeException. But...it deletes the record from table in the database. On...
0
by: Mike | last post by:
Hi, I have a collection object bound to a data grid, after I remove an item from the collection, the minute I click on the datagrid I get an error saying the specified argument was out of the...
1
by: =?Utf-8?B?V2VzdGU=?= | last post by:
I am throwing the ArgumentOutOfRangeException in my code as shown below throw new ArgumentOutOfRangeException("value", "Value must be 0"); my catch block is below ...
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:
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.