ArgumentOutOfRangeException was unhandled

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sinhzun
    New Member
    • Oct 2009
    • 4

    ArgumentOutOfRangeException was unhandled

    Hi everybody, I have a trouble with ArgumentOutOfRa ngeException. When I type a French word in an AutoCompleteCom boBox then submit it to add new record to database, the combo box throw an exception:
    ArgumentOutOfRa ngeException 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 AutoCompleteCom boBox class:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Data.OleDb;
    using System.Data.DataSetExtensions;
    using System.Data;
    
    namespace WindowsFormsApplication1
    {
    
        public class AutoCompleteComboBox : System.Windows.Forms.ComboBox
        {
            public event System.ComponentModel.CancelEventHandler NotInList;
    
            //private bool _limit;
           
            private bool _limitToList = true;
            private bool _inEditMode = false;
    
            public AutoCompleteComboBox() : base()
            {
            }
    
            public bool LimitToList
            {
                get { return _limitToList; }
                
                set { _limitToList = value; }
            }
    
            protected virtual void OnNotInList(System.ComponentModel.CancelEventArgs e)
            {
                if (NotInList != null)
                {
                    NotInList(this, e);
                }
            }
    
            protected override void OnTextChanged(System.EventArgs e)
            {
                if (_inEditMode)
                {
                    
                    string input = Text;
                    int i;
                    
                    //int index = FindString(input);
                    while (this.Items.Count > 0)
                    {
                        this.Items.RemoveAt(0);
                    }
                    this.DroppedDown = true;
                    Controldb db = new Controldb();
                    DataTable myTable = new DataTable();
                    string combox_text = this.Text;
                    string query = "";
                    query = "SELECT DISTINCT adresse FROM tblMain WHERE trans = 'in' AND " + "adresse LIKE '%" + combox_text + "%'";
                    try
                    {
                        myTable = db.getTable(query);
                    }
                    catch (Exception e1) {
                        MessageBox.Show(e1.ToString());
                    }
                    if (myTable.Rows.Count > 0)
                    {
                        foreach (DataRow myDataRow in myTable.Rows)
                        {
                            this.Items.Add(myDataRow["adresse"].ToString());
    
                        }
                    }
    
                }
    
                base.OnTextChanged(e);
            }
    
            protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
            {
                if (this.LimitToList)
                {
                    int pos = this.FindStringExact(this.Text);
    
                    if (pos == -1)
                    {
                        OnNotInList(e);
                    }
                    else
                    {
                        this.SelectedIndex = pos;
                    }
                }
    
                base.OnValidating(e);
            }
    
            protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
            {
                _inEditMode = (e.KeyCode !=Keys.Up && e.KeyCode != Keys.Down);
                base.OnKeyDown(e);
                
            }
            
        }
    }

    Can somebody help me?? Is there anything wrong with my AutoCompleteCom boBox??
    Thanks in advanced!
    Last edited by tlhintoq; Oct 22 '09, 08:39 AM. Reason: [CODE] ...your code here...[/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Comment

    • sinhzun
      New Member
      • Oct 2009
      • 4

      #3
      Originally posted by tlhintoq
      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.
      Yep, thanks tlhintoq! I'll remember next time

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        When I type a French word in an AutoCompleteCom boBox then submit it to add new record to database, the combo box throw an exception:
        ArgumentOutOfRa ngeException 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?

        ArgumentOutOfRa ngeException 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.

        Comment

        • sinhzun
          New Member
          • Oct 2009
          • 4

          #5
          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:
          Code:
          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:
          Code:
          private void btnOK_Click(object sender, EventArgs e)
                  {
                      DateTime date = Convert.ToDateTime( dateTimePicker1.Value);
                      double montant;
                      
                     
                      montant = Convert.ToDouble(txtMontant.Text);
                      string type = "";
                      string nom;
                      string addr;
                      if (radCheque.Checked)
                          type = "cheque";
                      if (radEspese.Checked)
                          type = "espese";
                      if (radCredit.Checked)
                          type = "credit";
                      
                      nom = txtNewClient.Text;
                      addr = cboAddr.Text;
                      
                      string sqlQuery;
                      if (status == 2)
                      {
                          if (MessageBox.Show("Edit?", "Confirm!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                              sqlQuery = "UPDATE tblMain SET [date] = '"+date+"',  type= '" + type + "', montant = " + montant + ", nom = '" + nom + "', adresse = '" + addr + "'WHERE id = " + edit_id + ";";
                          else
                              return;
                      }
                      else
                      {
                          if (MessageBox.Show("Add?", "Confirm!", MessageBoxButtons.YesNo) == DialogResult.Yes)
          
                              sqlQuery = "INSERT INTO tblMain ( [date], type, montant, nom, adresse, trans )VALUES ('"+date+"' , '" + type + "', " + montant + ",'" + nom + "' ,'" + addr + "' ,'in' );";
                          else
                              return;
                      }
                      Controldb db = new Controldb();
                      db.executeQuery(sqlQuery);
                      edit_id = 0;
                      status = 1;
                      this.Close();
                      frmIn_FormClosed(sender, null);
                      
                  }
          But I think this is because cboAddr is an AutoCompleteCom boBox, not an ordinary combobox, so I provide the AutoCompleteCom boBox class. I don't know why the AutoCompleteCom bobox can not be assigned a French character??

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Check if this will at least keep it from producing and error

            if (cboAddr.Text != null) addr = cboAddr.Text;

            Comment

            • sinhzun
              New Member
              • Oct 2009
              • 4

              #7
              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 ^^.

              Comment

              Working...