By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,081 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,081 IT Pros & Developers. It's quick & easy.

ArgumentOutOfRangeException was unhandled

P: 4
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
Share this Question
Share on Google+
6 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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

P: 4
@tlhintoq
Yep, thanks tlhintoq! I'll remember next time
Oct 22 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
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

P: 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
Expert 2.5K+
P: 3,525
Check if this will at least keep it from producing and error

if (cboAddr.Text != null) addr = cboAddr.Text;
Oct 22 '09 #6

P: 4
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

Post your reply

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