Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 4th, 2005, 12:48 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Weird error when inserting into Access!!!

Hey guys, I get this error when I try to insert a new record to my Access database.

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

myCon.Open()
cmdInsert.ExecuteNonQuery()
myCon.Close()

-----------------------------------------------------------------------------------------

Here is the complete code. It's the last method in the code called Save_Data()
-----------------------------------------------------------------
Code:
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

dim myCon as OleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\database\workout.mdb;")
dim cmdSelect as OleDbCommand
dim dtrMuscle as OleDbDataReader
dim dtrExcersise as OleDbDataReader
dim strSelectMuscle as string
dim strSelectExcersise as string



'=================================================================
sub Page_Load

if not IsPostBack then 

strSelectMuscle = "select muscle_id, muscle_group FROM muscle_groups"

cmdSelect = new OleDbCommand(strSelectMuscle, myCon)

myCon.Open()
dtrMuscle = cmdSelect.ExecuteReader

ddlMuscle.DataSource = dtrMuscle
ddlMuscle.DataTextField = "muscle_group"
ddlMuscle.DataValueField = "muscle_id"
ddlMuscle.DataBind()

ddlMuscle.Items.Insert(0, "-Select-")

myCon.Close()

end if
end sub


'===================================================================
sub FillExcersise(s as Object, e as EventArgs)

strSelectExcersise = "SELECT excersise, excersise_id FROM excersises WHERE muscle_id = @muscle"

cmdSelect = new OleDbCommand(strSelectExcersise, myCon)
cmdSelect.Parameters.Add("@muscle", SqlDbType.Int).Value = ddlMuscle.SelectedValue()

myCon.Open()
dtrExcersise = cmdSelect.ExecuteReader

ddlExcersise.DataSource = dtrExcersise
ddlExcersise.DataTextField = "excersise"
ddlExcersise.DataValueField = "excersise_id"
ddlExcersise.DataBind()

'ddlExcersise.Items.Insert(0, "-Select-")

myCon.Close()

end sub


'====================================================================
'This is the method that is given me all the trouble


sub Save_Data(s as Object, e as EventArgs)

dim cmdInsert as OleDbCommand

dim strInsert as string = "INSERT INTO workout_profiles2 (user_id, muscle_worked, excersise_performed, reps, sets, weight_lifted " & _
   "body_weight, type) VALUES (@session, @muscle, @excersise, @reps, @sets, @weight, @bodyweight, @type)"

cmdInsert = new OleDbCommand(strInsert, myCon)
cmdInsert.Parameters.Add("@session", SqlDbType.Int).Value = Session("USER_ID")
cmdInsert.Parameters.Add("@muscle", ddlMuscle.SelectedItem)
cmdInsert.Parameters.Add("@excersise", ddlExcersise.SelectedItem)
cmdInsert.Parameters.Add("@reps", SqlDbType.Int).Value = CInt(txtReps.Text)
cmdInsert.Parameters.Add("@sets", SqlDbType.Int).Value = Cint(txtSets.Text)
cmdInsert.Parameters.Add("@weight", SqlDbType.Int).Value = Cint(txtWeightLifted.Text)
cmdInsert.Parameters.Add("@bodyweight", SqlDbType.Int).Value = CInt(txtBodyWeight.Text)
cmdInsert.Parameters.Add("@type", ddlType.SelectedItem)

myCon.Open()
cmdInsert.ExecuteNonQuery() 'this is the line with the error on it
myCon.Close()

lblMessage.Text = "Your data has been saved!"

end sub


</script>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<form runat="server">
Muscle worked:<br>
<asp:dropdownlist id="ddlMuscle" runat="server" AutoPostBack="true" OnSelectedIndexChanged="FillExcersise" />
<p></p>
Excersise performed<br>
<asp:dropdownlist id="ddlExcersise" runat="server" />
<p></p>
Sets:
<asp:textbox id="txtSets" runat="server" Width="30" />
<p></p>
Reps:
<asp:textbox id="txtReps" runat="server" Width="30" />
<p></p>
Weight lifted:
<asp:textbox id="txtWeightLifted" runat="server" width="40" />
<p></p>
Current Body Weight:
<asp:textbox id="txtBodyWeight" runat="server" Width="40" />
<p></p>
Workout Type:
<asp:dropdownlist id="ddlType" runat="server">
 <asp:listitem Text="Free Weights" />
 <asp:listitem Text="Machine" />
 <asp:listitem Text="Hammer Strength" />
 <asp:listitem Text="Natural" />
</asp:dropdownlist>
<p></p>
<asp:button Text="Add!" runat="server" OnClick="Save_Data"/>

</form>
<p></p><br>
<asp:label id="lblMessage" runat="server" ForeColor="#FF0000" />

</body>
</html>
----------------------------------------------------------------------

Thanks for any help you can offer me,

Chris
 
Old April 6th, 2005, 07:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Make sure all your variable datatypes are matching up to the database types. If someone entered "151 and a half" into the weight textbox what happens?






Similar Threads
Thread Thread Starter Forum Replies Last Post
VWD Weird Error bmains Visual Studio 2005 1 May 2nd, 2005 09:46 AM
weird access violation Yosi Visual C++ 0 January 6th, 2005 05:19 AM
Weird Access Problem MattyJ Access 2 October 1st, 2004 02:10 PM
Weird Combobox Error kpowers@wppisys.org ADO.NET 0 October 1st, 2003 10:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.