Saturday, March 31, 2012

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:

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 (@dotnet.itags.org.loginid, @dotnet.itags.org.password, @dotnet.itags.org.Name, @dotnet.itags.org.Designation,@dotnet.itags.org.Company," _
& " @dotnet.itags.org.Address, @dotnet.itags.org.Zip, @dotnet.itags.org.City, @dotnet.itags.org.Country,@dotnet.itags.org.Fax,@dotnet.itags.org.Email,@dotnet.itags.org.Phone,@dotnet.itags.org.EmployeeSize,@dotnet.itags.org.Sales)"
cmdInsert= New OleDbCommand(strInsert,conUser)
cmdInsert.Parameters.Add("@dotnet.itags.org.loginid", txtLoginID.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Password", txtPassword.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Name", txtName.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Designation", txtDesignation.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Company", txtCompany.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Address", txtAddress.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Zip", txtZip.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.City", txtCity)
cmdInsert.Parameters.Add("@dotnet.itags.org.Country", txtCountry.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Fax", txtFax.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Email", txtEmail.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Phone", txtPhone.text)
cmdInsert.Parameters.Add("@dotnet.itags.org.EmployeeSize", rbEmployeeSize.SelectedItem.Text)
cmdInsert.Parameters.Add("@dotnet.itags.org.Sales", rbSalesSize.SelectedItem.Text)
conUser.Open()
cmdInsert.ExecuteNonQuery()
conUser.Close()

Help! Please... :(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
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.
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
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:

<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> </td>
<td> </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> </td>
<td> </td>
</tr>
<tr>
<td><asp:button ID="Submit" OnClick="CheckLogin" runat="server" Text="Submit" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
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?
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

0 comments:

Post a Comment