Results 1 to 7 of 7

Thread: System.FormatException: Index (zero based) must be greater than or equal to zero...

  1. #1

    Thread Starter
    Addicted Member diban's Avatar
    Join Date
    Aug 2002
    Location
    IN
    Posts
    152

    Unhappy System.FormatException: Index (zero based) must be greater than or equal to zero...

    Hi

    I am absolutely out of my wits on how to handle this problem. I'm just beginning to learn ASP.NEt.

    I get this error while trying to insert new registration info on an access table:

    System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

    Here's my code:

    Code:
    Dim cmdInsert as OleDbCommand
    Dim strInsert as String
    strInsert="Insert into Userdetail (login_id,password, Name, Designation, Company, " _
    	& "Address, Zip, City, Country, Fax, email, phone, employee_size, sales)" _
    	& " VALUES (@loginid, @password, @Name, @Designation,@Company," _
    	& " @Address, @Zip, @City, @Country,@Fax,@Email,@Phone,@EmployeeSize,@Sales)"	
    cmdInsert= New OleDbCommand(strInsert,conUser)
    cmdInsert.Parameters.Add("@loginid", txtLoginID.text)	
    cmdInsert.Parameters.Add("@Password", txtPassword.text)	
    cmdInsert.Parameters.Add("@Name", txtName.text)
    cmdInsert.Parameters.Add("@Designation", txtDesignation.text)	
    cmdInsert.Parameters.Add("@Company", txtCompany.text)	
    cmdInsert.Parameters.Add("@Address", txtAddress.text)
    cmdInsert.Parameters.Add("@Zip", txtZip.text)
    cmdInsert.Parameters.Add("@City", txtCity)
    cmdInsert.Parameters.Add("@Country", txtCountry.text)	
    cmdInsert.Parameters.Add("@Fax", txtFax.text)	
    cmdInsert.Parameters.Add("@Email", txtEmail.text)
    cmdInsert.Parameters.Add("@Phone", txtPhone.text)
    cmdInsert.Parameters.Add("@EmployeeSize", rbEmployeeSize.SelectedItem.Text)
    cmdInsert.Parameters.Add("@Sales", rbSalesSize.SelectedItem.Text)
    conUser.Open()
    cmdInsert.ExecuteNonQuery()
    conUser.Close()
    Help! Please...

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: System.FormatException: Index (zero based) must be greater than or equal to zero.

    I think it is something to do with rbEmployeeSize.SelectedItem.Text or/and rbSalesSize.SelectedItem.Text - what are the controls that relate to these?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Addicted Member diban's Avatar
    Join Date
    Aug 2002
    Location
    IN
    Posts
    152

    Re: System.FormatException: Index (zero based) must be greater than or equal to zero...

    Code:
    I think it is something to do with rbEmployeeSize.SelectedItem.Text or/and rbSalesSize.SelectedItem.Text - what are the controls that relate to these?
    These are RadioButtonLists that give out the value of the text of the Radio Button.

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: System.FormatException: Index (zero based) must be greater than or equal to zero.

    It may seem like a silly question but has there been an item selected in either list? - if not then the SelectedIndex property is null and therefore the Index (zero based) is not greater than or equal to zero or less than the size of the argument list.

    Maybe worth adding some code to check an item was selected.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  5. #5

    Thread Starter
    Addicted Member diban's Avatar
    Join Date
    Aug 2002
    Location
    IN
    Posts
    152

    Re: System.FormatException: Index (zero based) must be greater than or equal to zero...

    Yes, the radio buttons are being selected.

    I just checked if the item was being selected and found that both of them are being selected.

    Here's the html script to define asp form controls:

    Code:
    <form runat="server">
      <table width="100%" border="0" cellspacing="2" cellpadding="2">
        <tr> 
          <td colspan="2"><strong>Enter Registration Detail</strong></td>
        </tr>
        <tr> 
          <td width="18%">Login ID</td>
          <td width="82%"><asp:textbox ID="txtLoginID" runat="server" /> 
    	  <asp:Label ID="lblMessage" runat="server" />
    	  <asp:requiredfieldvalidator 
    	  ControlToValidate="txtLoginID" 
    	  Text="Login ID Field Cannot be left Blank" 
    	  runat="server" /> 
    	  
    	  </td>
        </tr>
        <tr> 
          <td>Password</td>
          <td><asp:textbox  ID="txtPassword" MaxLength="20" runat="server" TextMode="Password" /> 
            <asp:requiredfieldvalidator 
    	  ControlToValidate="txtPassword" 
    	  Text="Password Cannot be left Blank" 
    	  runat="server" /> <asp:regularexpressionvalidator 
    	  ControlToValidate="txtPassword" 
    	  Text= "Your Password must contain between 3 and 20 characters!" 
    	  ValidationExpression="\w{3,30}" 
    	  runat="server" /> <asp:comparevalidator 
    	  ControlToValidate="txtPassWord" 
    	  ControlToCompare="txtConfirmPassword" 
    	  Text="Please Reconfirm your password" 
    	  Operator="Equal" 
    	  Type="String" 
    	  runat="server" /> </td>
        </tr>
        <tr> 
          <td>Confirm Password</td>
          <td><asp:textbox  ID="txtConfirmPassword" MaxLength="20" runat="server" TextMode="Password" /> 
            <asp:requiredfieldvalidator ControlToValidate="txtConfirmPassword" Text="Please Reconfirm the password" runat="server" /> </td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td>Name</td>
          <td><asp:textbox ID="txtName" runat="server" /> <asp:requiredfieldvalidator ControlToValidate="txtName" Text="Name Cannot be left Blank" runat="server" /> </td>
        </tr>
        <tr> 
          <td>Designation</td>
          <td><asp:textbox ID="txtDesignation" runat="server" /> </td>
        </tr>
        <tr> 
          <td>Company</td>
          <td><asp:textbox ID="txtCompany" runat="server" /> </td>
        </tr>
        <tr> 
          <td>Address</td>
          <td> <asp:textbox Columns="30" ID="txtAddress" Rows="4" runat="server" TextMode="MultiLine" />
            <asp:requiredfieldvalidator ControlToValidate="txtAddress" Text="Address Cannot be left Blank" runat="server" /> </td>
        </tr>
        <tr> 
          <td>City</td>
          <td><asp:textbox ID="txtCity" runat="server" /> </td>
        </tr>
        <tr> 
          <td>Zip</td>
          <td><asp:textbox ID="txtZip" runat="server" /> </tr>
        <tr> 
          <td>Country</td>
          <td><asp:textbox ID="txtCountry" runat="server" /> </tr>
        <tr> 
          <td>Fax</td>
          <td><asp:textbox ID="txtFax" runat="server" /> </tr>
        <tr> 
          <td>Email</td>
          <td><asp:textbox ID="txtEmail"  runat="server" /> <asp:regularexpressionvalidator 
    	  ControlToValidate="txtEmail" 
    	  Text="Invalid Email Address" 
    	  Display="Dynamic"
    	  ValidationExpression="\S+@\S+\.\S{2,3}"
    	  runat="server" /> </tr>
        <tr> 
          <td>Phone</td>
          <td><asp:textbox ID="txtPhone" runat="server" /> </tr>
        <tr> 
          <td>Employee Size</td>
          <td>
    	  <asp:radiobuttonlist 
    	  ID="rbEmployeeSize" 
    	  runat="server" >
    	  <asp:ListItem Text="Less Than 10" />
    	  <asp:ListItem Text="10 to 49" />
    	  <asp:listitem Text="50 to 100" />
    	  <asp:listItem text= "Over 50" />
    	  </asp:radiobuttonlist></td>
        </tr>
        <tr> 
          <td>Sales Size</td>
          <td><asp:radiobuttonlist 
    	  ID="rbSalesSize" 
    	  runat="server" > 
              <asp:ListItem Text="Less Than 50,000" /> 
              <asp:ListItem Text="50,000 to 100,000" /> 
              <asp:listitem Text="100,000 to 100" /> 
              <asp:listItem text= "Over 50" /> </asp:radiobuttonlist></td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td><asp:button ID="Submit" OnClick="CheckLogin" runat="server" Text="Submit" /></td>
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form>

  6. #6

    Thread Starter
    Addicted Member diban's Avatar
    Join Date
    Aug 2002
    Location
    IN
    Posts
    152

    Re: System.FormatException: Index (zero based) must be greater than or equal to zero...

    Hi DJ4UK,

    I just found out the problem. The problem was in my Parameter.Add Declaration of @City where I was not assigning the value of txtCity (should have been TxtCity.Text)

    But now I get a Syntax error in INSERT INTO Statement:

    System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.

    This seems to be a very simple insert statement. What could possibly be wrong here?

  7. #7

    Thread Starter
    Addicted Member diban's Avatar
    Join Date
    Aug 2002
    Location
    IN
    Posts
    152

    Resolved Re: System.FormatException: Index (zero based) must be greater than or equal to zero...

    I just found out the solution to the second problem too. I was using a reserved word password in the Database and I had to use [] to resolve this.

    Thanks for looking into my problem.

    Diban

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width