ramnujs
Well-known member
I am writing a code that will insert to an MySQL database in the table Amenities. Code 01 is class definition named ParameterHelper Class. It's primary purpose is to add parameter's dynamically in a MySQLCommand Objects.
Code s: is the code for the creation of the stored procedure in MySQL version 5.1.16.(no problems in the database)
Code 02 is code fragment of AminityDAO Class specifically the method InsertAminity. InsertAmenity method accepts an argument Amenity Object byref which is an Instance of a Entity class Aminity that inherit's the Dataset class. The red line code indicates the line which an exception occur and the exception is the figure 1.0.
What seems to be the problem? Can anybody help plaese? Urgent! Thanks!
'''''Code 01:
Imports System.Reflection
Imports MySql.Data.MySqlClient
Public Class ParameterHelper
Public Sub New()
End Sub
Public Shared Sub AddParameters(ByRef obj As DataSet, ByRef ExemptedFields As ArrayList, ByRef InsertCommand As MySqlCommand, ByVal TableName As String)
Dim dataColumn As Data.DataColumn
Dim propertyInfos() As PropertyInfo
Dim objectType As Type = obj.GetType
propertyInfos = objectType.GetProperties
For Each dataColumn In obj.Tables(TableName).Columns
If Not ExemptedFields.Contains(dataColumn.ColumnName.ToLower) Then
Dim pInfo As PropertyInfo
For Each pInfo In propertyInfos
If dataColumn.ColumnName.ToLower = pInfo.Name.ToLower Then
Dim param As MySqlParameter = InsertCommand.CreateParameter()
param.ParameterName = "p" & dataColumn.ColumnName
param.SourceColumn = dataColumn.ColumnName
param.Value = pInfo.GetValue(obj, Nothing)
param.Direction = ParameterDirection.Input
InsertCommand.Parameters.Add(param)
End If
Next
End If
Next
End Sub
End Class
Code s:
create procedure spInsertAmenity(in pamenityid int,in pname varchar(30),int pdescription varchar(100))
begin
Insert into Amenities(amenityid,`name`,description) values(pamenityid,pname,dpdescription);
end
Code 02:
Public Sub InsertAmenity(ByRef Amenity As Jinisys.Hotel.Room.BusinessLayer.Amenity)
Dim Connection As New MySqlConnection(connectionStr)
'Try
Connection.Open()
Dim sqlStatement As String = "spInsertAmenity"
Dim InsertCommand As New MySqlCommand(sqlStatement, Connection)
InsertCommand.CommandType = CommandType.StoredProcedure
Dim ExemptedFields As New ArrayList
ExemptedFields.Add("stateflag")
ParameterHelper.AddParameters(Amenity, ExemptedFields, InsertCommand, "Amenities")
MsgBox(InsertCommand.ExecuteNonQuery() & "'s affected!")
Dim D As DataRow = Amenity.Tables("Amenities").NewRow()
D("amenityid") = Amenity.AmenityId
D("name") = Amenity.Name
D("description") = Amenity.Description
Amenity.Tables("Amenities").Rows.Add(D)
Amenity.Tables("Amenities").AcceptChanges()
'Catch ex As Exception
' MsgBox("ERROR: diri dapit" & ex.Message)
'Finally
Connection.Close()
'End Try
End Sub
[/IMG]
Code s: is the code for the creation of the stored procedure in MySQL version 5.1.16.(no problems in the database)
Code 02 is code fragment of AminityDAO Class specifically the method InsertAminity. InsertAmenity method accepts an argument Amenity Object byref which is an Instance of a Entity class Aminity that inherit's the Dataset class. The red line code indicates the line which an exception occur and the exception is the figure 1.0.
What seems to be the problem? Can anybody help plaese? Urgent! Thanks!
'''''Code 01:
Imports System.Reflection
Imports MySql.Data.MySqlClient
Public Class ParameterHelper
Public Sub New()
End Sub
Public Shared Sub AddParameters(ByRef obj As DataSet, ByRef ExemptedFields As ArrayList, ByRef InsertCommand As MySqlCommand, ByVal TableName As String)
Dim dataColumn As Data.DataColumn
Dim propertyInfos() As PropertyInfo
Dim objectType As Type = obj.GetType
propertyInfos = objectType.GetProperties
For Each dataColumn In obj.Tables(TableName).Columns
If Not ExemptedFields.Contains(dataColumn.ColumnName.ToLower) Then
Dim pInfo As PropertyInfo
For Each pInfo In propertyInfos
If dataColumn.ColumnName.ToLower = pInfo.Name.ToLower Then
Dim param As MySqlParameter = InsertCommand.CreateParameter()
param.ParameterName = "p" & dataColumn.ColumnName
param.SourceColumn = dataColumn.ColumnName
param.Value = pInfo.GetValue(obj, Nothing)
param.Direction = ParameterDirection.Input
InsertCommand.Parameters.Add(param)
End If
Next
End If
Next
End Sub
End Class
Code s:
create procedure spInsertAmenity(in pamenityid int,in pname varchar(30),int pdescription varchar(100))
begin
Insert into Amenities(amenityid,`name`,description) values(pamenityid,pname,dpdescription);
end
Code 02:
Public Sub InsertAmenity(ByRef Amenity As Jinisys.Hotel.Room.BusinessLayer.Amenity)
Dim Connection As New MySqlConnection(connectionStr)
'Try
Connection.Open()
Dim sqlStatement As String = "spInsertAmenity"
Dim InsertCommand As New MySqlCommand(sqlStatement, Connection)
InsertCommand.CommandType = CommandType.StoredProcedure
Dim ExemptedFields As New ArrayList
ExemptedFields.Add("stateflag")
ParameterHelper.AddParameters(Amenity, ExemptedFields, InsertCommand, "Amenities")
MsgBox(InsertCommand.ExecuteNonQuery() & "'s affected!")
Dim D As DataRow = Amenity.Tables("Amenities").NewRow()
D("amenityid") = Amenity.AmenityId
D("name") = Amenity.Name
D("description") = Amenity.Description
Amenity.Tables("Amenities").Rows.Add(D)
Amenity.Tables("Amenities").AcceptChanges()
'Catch ex As Exception
' MsgBox("ERROR: diri dapit" & ex.Message)
'Finally
Connection.Close()
'End Try
End Sub