DetailsView: InsertMethod not function [ObjectDataSource]

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
 
W

Wiktor Zychla [C# MVP]

I am using DetailsView using ObjectDataSource with DataSourceTypeName
assigned to the specific class.

how do you pass that "employee" parameter from the DetailsView to the
ObjectDataSource's InsertEmployee method? my guess is that you do not
provide it at all or provide it incorrectly.

Wiktor Zychla
 
G

Guest

In the Profile.aspx, I had defined a ObjectDataSource with the following:
<asp:ObjectDataSource ID="EmployeeDetailsObjectDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
MaximumRowsParameterName="" StartRowIndexParameterName=""
DataObjectTypeName="HRPortal.DataAccess.HREmployee"
TypeName="HRPortal.DataAccess.EmployeeDAL">
</asp:ObjectDataSource>
DataObjectTypeName tag is set to an active class, stored in
HRPortal.DataAccess.HREmployee.vb.

The web form is capable to perform update and delete functions, except
insert function.
I had raised events for both DetailsView_ItemInserting and
EmployeeDetailsObjectDataSource_Inserting. The end result is: empty. The
contents of inputParameter are blank. None of the data entered is pass in.
Thus, when my BLL HRPortal.DataAccess.EmployeeDAL's InsertEmployee function
receive the InputParameter, aka employee, it fails to add the record.

Regards
 
W

Wiktor Zychla [C# MVP]

I had raised events for both DetailsView_ItemInserting and
EmployeeDetailsObjectDataSource_Inserting. The end result is: empty. The
contents of inputParameter are blank. None of the data entered is pass in.
Thus, when my BLL HRPortal.DataAccess.EmployeeDAL's InsertEmployee
function
receive the InputParameter, aka employee, it fails to add the record.

the EmployeeDetailsObjectDataSource_Inserting is your chance to actually
create the parameter, even though the contents of InputParameter are empty.
the problem is that unless you use two-way data-binding, a value from the UI
control will not be passed to the Inserting method. Since in insert case
there is no value you could bind to, the best you could do would be to
create it:


protected void EmployeeDetailsObjectDataSource_Inserting( object sender,
ObjectDataSourceMethodEventArgs e )

{

HREmployee employee = new HREmployee();

...

e.InputParameters.Clear();

e.InputParameters.Add( "employee", employee );

}



Wiktor Zychla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top