PDA

Click to See Complete Forum and Search --> : Select Method


NOTSOSURE
Mar 5th, 2004, 07:50 AM
Hey all

I'm using ASP.NET to produce a database that may insert data into a database i have attempted to do this but i keep getting errors

what i want to do is when the users have entered all the data into the textboxes and presses insert button the data will be inserted to the database

here's my code so far


Sub ButtonInsert_Click(sender As Object, e As EventArgs)


Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an"& _
"d Settings\James\Desktop\Hotel.mdb"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

Dim queryString As String = "INSERT INTO [Guest] ([FirstName], [LastName], [Title], [BirthDate], [Address], [P"& _
"ostalCode]) VALUES (@FirstName, @LastName, @Title, @BirthDate, @Address, @Postal"& _
"Code)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_firstName As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_firstName.ParameterName = "@FirstName"
dbParam_firstName.Value = TextBoxFirstName.text
dbParam_firstName.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_firstName)

Dim dbParam_lastName As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_lastName.ParameterName = "@LastName"
dbParam_lastName.Value = TextBoxLastName.text
dbParam_lastName.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_lastName)

Dim dbParam_title As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_title.ParameterName = "@Title"
dbParam_title.Value = TextBoxTitle.text
dbParam_title.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_title)

Dim dbParam_birthDate As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_birthDate.ParameterName = "@BirthDate"
dbParam_birthDate.Value = TextBoxBirthDate.text
dbParam_birthDate.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_birthDate)

Dim dbParam_address As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_address.ParameterName = "@Address"
dbParam_address.Value = TextBoxAddress.text
dbParam_address.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_address)

Dim dbParam_postalCode As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_postalCode.ParameterName = "@PostalCode"
dbParam_postalCode.Value = TextBoxPostalCode.text
dbParam_postalCode.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_postalCode)

Dim rowsAffected As Integer = 0

dbConnection.Open

Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Return rowsAffected

End Sub



the error i get is

------------------------------------------------------------------------------------
Compiler Error Message: BC30647: 'Return' statement in a Sub or a Set cannot return a value.

Line 63: End Try
Line 64:
Line 65: Return rowsAffected
Line 66:
Line 67: End Sub

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

can anyone tell me or even amend my code where im going wrong?

Thanks

nemaroller
Mar 6th, 2004, 08:00 AM
Sure...

You can't return a value from a Sub.

You CAN return a value from a function. Move your code into a function, call the function when the user clicks the button.

NOTSOSURE
Mar 6th, 2004, 10:36 AM
Ok i placed my code in a function called myInsertFunction..

How do u call it?

Thanks

nemaroller
Mar 6th, 2004, 01:14 PM
Are you being serious? I mean, if you don't know how to call a function, how are you going to retrieve data from a database?


Sub ButtonInsert_Click(sender As Object, e As EventArgs)
MyInsertFunction
End Sub

NOTSOSURE
Mar 7th, 2004, 09:20 AM
ok mate i tried that before i posted my post..it don't work

i'm using ASP.NET web matrix

nemaroller
Mar 7th, 2004, 12:50 PM
Well, I don't know how Web Matrix wires it up... but that's all you would need to do in Visual Studio.