G
Guest
I am using DetailsView using ObjectDataSource with DataSourceTypeName
assigned to the specific class.
SelectMethod, UpdateMethod and DeleteMethod is working successfully, except
InsertMethod.
The error msg returned is: Cannot insert the employee.
With tracing trigger on, I had discovered that the parameter is actually
empty.
The data entered is not pass into the parameter at all !!!
What could be the possible reason?
Below is the setting in my profile.aspx:
<asp:ObjectDataSource ID="EmployeeDetailsObjectDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
MaximumRowsParameterName="" StartRowIndexParameterName=""
DataObjectTypeName="HRPortal.DataAccess.HREmployee"
TypeName="HRPortal.DataAccess.EmployeeDAL">
</asp:ObjectDataSource>
This is how I assign the InsertMethod in my profile.aspx.vb:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init
EmployeeDetailsObjectDataSource.InsertMethod = "InsertEmployee"
End Sub
This is the InsertMethod in my business logic layer,
HRPortal.DataAccess.EmployeeDAL.vb:
Public Shared Function InsertEmployee(ByVal employee As HREmployee) As Integer
If employee.homeAddress Is Nothing Then employee.homeAddress = String.Empty
If employee.userName Is Nothing Then employee.userName = String.Empty
If employee.birthDay.Equals(DBNull.Value) Then employee.birthDay =
DateTime.MinValue
Dim sqlCmd1 As String = "SELECT [EmployeeID] FROM [EmployeeMaster] " & _
" WHERE [EmployeeID]=@EmployeeID"
Dim sqlCmd2 As String = "INSERT INTO [EmployeeMaster] " & _
" ([EmployeeID],[EmployeeName], [UserName], [HomeAddress],
[Birthday]) " & _
" Values(@EmployeeID, @EmployeeName, @UserName, @HomeAddress,
@Birthday) "
sqlCmd2 = "INSERT INTO [EmployeeMaster] " & _
" ([EmployeeID],[EmployeeName], [UserName], [HomeAddress]) " & _
" Values(@EmployeeID, @EmployeeName, @UserName, @HomeAddress) "
Dim conn_MS As SqlConnection = New SqlConnection(_connectionString)
Dim da_MS As SqlDataAdapter
Dim ds As DataSet = New DataSet()
Dim result As Integer = 0
Try
da_MS = New SqlDataAdapter(sqlCmd1, conn_MS)
da_MS.SelectCommand.Parameters.Add("@EmployeeID",
SqlDbType.VarChar, 14).Value = employee.EmployeeID
Dim cmd_MS As SqlCommand = New SqlCommand(sqlCmd2, conn_MS)
cmd_MS.Parameters.Add("@EmployeeID", SqlDbType.VarChar, 14).Value
= employee.EmployeeID
cmd_MS.Parameters.Add("@EmployeeName", SqlDbType.VarChar,
80).Value = employee.EmployeeName
cmd_MS.Parameters.Add("@UserName", SqlDbType.VarChar, 20).Value =
employee.userName
cmd_MS.Parameters.Add("@HomeAddress", SqlDbType.VarChar,
200).Value = employee.homeAddress
'cmd_MS.Parameters.Add("@Birthday", SqlDbType.DateTime).Value =
employee.birthDay
conn_MS.Open()
da_MS.Fill(ds, "EmployeeMaster")
If ds.Tables("EmployeeMaster").Rows.Count = 0 Then
result = cmd_MS.ExecuteNonQuery()
Else
result = -1
End If
Catch ex As SqlException
Throw New Exception("Cannot insert the employee." + ex.Message + "
" + ex.LineNumber.ToString() + " " + ex.Source + " " + ex.State.ToString())
Finally
conn_MS.Close()
End Try
Return result
End Function
assigned to the specific class.
SelectMethod, UpdateMethod and DeleteMethod is working successfully, except
InsertMethod.
The error msg returned is: Cannot insert the employee.
With tracing trigger on, I had discovered that the parameter is actually
empty.
The data entered is not pass into the parameter at all !!!
What could be the possible reason?
Below is the setting in my profile.aspx:
<asp:ObjectDataSource ID="EmployeeDetailsObjectDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
MaximumRowsParameterName="" StartRowIndexParameterName=""
DataObjectTypeName="HRPortal.DataAccess.HREmployee"
TypeName="HRPortal.DataAccess.EmployeeDAL">
</asp:ObjectDataSource>
This is how I assign the InsertMethod in my profile.aspx.vb:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init
EmployeeDetailsObjectDataSource.InsertMethod = "InsertEmployee"
End Sub
This is the InsertMethod in my business logic layer,
HRPortal.DataAccess.EmployeeDAL.vb:
Public Shared Function InsertEmployee(ByVal employee As HREmployee) As Integer
If employee.homeAddress Is Nothing Then employee.homeAddress = String.Empty
If employee.userName Is Nothing Then employee.userName = String.Empty
If employee.birthDay.Equals(DBNull.Value) Then employee.birthDay =
DateTime.MinValue
Dim sqlCmd1 As String = "SELECT [EmployeeID] FROM [EmployeeMaster] " & _
" WHERE [EmployeeID]=@EmployeeID"
Dim sqlCmd2 As String = "INSERT INTO [EmployeeMaster] " & _
" ([EmployeeID],[EmployeeName], [UserName], [HomeAddress],
[Birthday]) " & _
" Values(@EmployeeID, @EmployeeName, @UserName, @HomeAddress,
@Birthday) "
sqlCmd2 = "INSERT INTO [EmployeeMaster] " & _
" ([EmployeeID],[EmployeeName], [UserName], [HomeAddress]) " & _
" Values(@EmployeeID, @EmployeeName, @UserName, @HomeAddress) "
Dim conn_MS As SqlConnection = New SqlConnection(_connectionString)
Dim da_MS As SqlDataAdapter
Dim ds As DataSet = New DataSet()
Dim result As Integer = 0
Try
da_MS = New SqlDataAdapter(sqlCmd1, conn_MS)
da_MS.SelectCommand.Parameters.Add("@EmployeeID",
SqlDbType.VarChar, 14).Value = employee.EmployeeID
Dim cmd_MS As SqlCommand = New SqlCommand(sqlCmd2, conn_MS)
cmd_MS.Parameters.Add("@EmployeeID", SqlDbType.VarChar, 14).Value
= employee.EmployeeID
cmd_MS.Parameters.Add("@EmployeeName", SqlDbType.VarChar,
80).Value = employee.EmployeeName
cmd_MS.Parameters.Add("@UserName", SqlDbType.VarChar, 20).Value =
employee.userName
cmd_MS.Parameters.Add("@HomeAddress", SqlDbType.VarChar,
200).Value = employee.homeAddress
'cmd_MS.Parameters.Add("@Birthday", SqlDbType.DateTime).Value =
employee.birthDay
conn_MS.Open()
da_MS.Fill(ds, "EmployeeMaster")
If ds.Tables("EmployeeMaster").Rows.Count = 0 Then
result = cmd_MS.ExecuteNonQuery()
Else
result = -1
End If
Catch ex As SqlException
Throw New Exception("Cannot insert the employee." + ex.Message + "
" + ex.LineNumber.ToString() + " " + ex.Source + " " + ex.State.ToString())
Finally
conn_MS.Close()
End Try
Return result
End Function