sreeni2219
New member
- Joined
- Jul 30, 2009
- Messages
- 1
- Programming Experience
- 5-10
am using ObjectDataSource and created UserDetails class and UserDB class.UserDetails calss has two constructors. My select method is working fine and is binding the values. After modifications if i click on Save button i am calling odsUsers.update() method. Whenever i goto debug mode, it is calling UpdateUser method of User class. Before that it went to userdetails default constructor (Public Sub New() End Sub ). I have noticed that
.Public Sub UpdateUser(ByVal user As UserDetails)
user object is null and all the values are either nothing or Zero. What am i missing here? Why my object is null instead having new values.
SAMPLE CODE:
ASPX CODE:
.Public Sub UpdateUser(ByVal user As UserDetails)
user object is null and all the values are either nothing or Zero. What am i missing here? Why my object is null instead having new values.
SAMPLE CODE:
VB.NET:
Public Class UserDetails
Private m_userID As Integer
Private m_firstName As String
Private m_lastName As String
Public Property UserID() As Integer
Get
Return m_userID
End Get
Set(ByVal value As Integer)
m_userID = value
End Set
End Property
Public Property FirstName() As String
Get
Return m_firstName
End Get
Set(ByVal value As String)
m_firstName = value
End Set
End Property
Public Property LastName() As String
Get
Return m_lastName
End Get
Set(ByVal value As String)
m_lastName = value
End Set
End Property
Public Sub New(ByVal UserID As Integer, ByVal firstName As String, ByVal lastName As String)
Me.m_userID = UserID
Me.m_firstName = firstName
Me.m_lastName = lastName
End Sub
Public Sub New()
End Sub
End Class
<DataObject()> _
Public Class UserDB
Private connectionString As String
Public Sub New()
' Get connection string from web.config.
connectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
End Sub
Public Sub New(ByVal connectionString As String)
Me.connectionString = connectionString
End Sub
<DataObjectMethod(DataObjectMethodType.Insert, True)> _
Public Function InsertUser(ByVal user As UserDetails) As Integer
Dim con As New OracleConnection(connectionString)
Dim insUser As String
insUser = "INSERT INTO USERS " & _
" (ID, FIRST_NAME, LAST_NAME) " & _
" Values(USERS_SEQ.nextval,@FIRST_NAME, @LAST_NAME) "
Dim cmd As New OracleCommand(insUser, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.Add(New OracleParameter("@FIRST_NAME", user.FirstName))
cmd.Parameters.Add(New OracleParameter("@LAST_NAME", user.FirstName))
Try
con.Open()
cmd.ExecuteNonQuery()
Catch err As OracleException
' Replace the error with something less specific.
' You could also log the error now.
Throw New ApplicationException("Data error.")
Finally
con.Close()
End Try
End Function
<DataObjectMethod(DataObjectMethodType.Update, True)> _
Public Sub UpdateUser(ByVal user As UserDetails)
Dim con As New OracleConnection(connectionString)
Dim insUser As String
insUser = "UPDATE LAP_USERS SET FIRST_NAME=@FIRST_NAME, LAST_NAME=@LAST_NAME " & _
" where ID=@UserID "
Dim cmd As New OracleCommand(insUser, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.Add(New OracleParameter("@UserID", user.UserID))
cmd.Parameters.Add(New OracleParameter("@FIRST_NAME", user.FirstName))
cmd.Parameters.Add(New OracleParameter("@LAST_NAME", user.FirstName))
Try
con.Open()
cmd.ExecuteNonQuery()
Catch err As OracleException
' Replace the error with something less specific.
' You could also log the error now.
Throw New ApplicationException("Data error.")
Finally
con.Close()
End Try
End Sub
<DataObjectMethod(DataObjectMethodType.[Select], False)> _
Public Function GetUsers(ByVal userID As Integer) As UserDetails
Dim con As New OracleConnection(connectionString)
Dim GetUser As String
GetUser = "Select ID,FIRST_NAME ,LAST_NAME from USERS ID=" & userID
Dim cmd As New OracleCommand(GetUser, con)
cmd.CommandType = CommandType.Text
Try
con.Open()
Dim reader As OracleDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
' Get the first row.
reader.Read()
Dim user As New UserDetails(CInt(reader("ID")), DirectCast(reader("FIRST_NAME"), String), DirectCast(reader("LAST_NAME"), String)
reader.Close()
Return user
Catch err As Exception
' Replace the error with something less specific.
' You could also log the error now.
Throw New ApplicationException("Data error.")
Finally
con.Close()
End Try
End Function
<DataObjectMethod(DataObjectMethodType.[Select], True)> _
Public Function GetUsers() As List(Of UserDetails)
Dim con As New OracleConnection(connectionString)
Dim GetAllUsers As String
GetAllUsers = "Select ID,FIRST_NAME ,LAST_NAME from USERS "
Dim cmd As New OracleCommand(GetAllUsers, con)
cmd.CommandType = CommandType.Text
' Create a collection for all the employee records.
Dim users As New List(Of UserDetails)()
Try
con.Open()
Dim reader As OracleDataReader = cmd.ExecuteReader()
While reader.Read()
Dim user As New UserDetails(CInt(reader("ID")), DirectCast(reader("FIRST_NAME"), String), DirectCast(reader("LAST_NAME"), String))
users.Add(user)
End While
reader.Close()
Return users
Catch err As OracleException
' Replace the error with something less specific.
' You could also log the error now.
Throw New ApplicationException("Data error.")
Finally
con.Close()
End Try
End Function
End Class
HTML:
<asp:ObjectDataSource ID="odsUsers" runat="server"
DataObjectTypeName="DatabaseComponent.DatabaseComponent.UserDetails"
InsertMethod="InsertUser"
UpdateMethod = "UpdateUser"
SelectMethod="GetUsers" TypeName="DatabaseComponent.DatabaseComponent.UserDB" >
<SelectParameters>
<asp:Parameter Name="userID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<table width="100%" border="0">
<asp:FormView ID="frm" runat="server" DataSourceID = "odsUsers" DefaultMode="ReadOnly" >
<EditItemTemplate>
<tr>
<td>
<table border="0" id="UserData" width="100%">
<tr>
<td><asp:Label ID="lblFirstName" Font-Bold="true" ForeColor= "red" Text="First Name: *" runat="server"></asp:Label>
</td>
<td> <asp:textbox ID="txtFirstName" runat="server" width="200px" text='<%# Bind("FirstName") %>'></asp:textbox>
</td>
<td > <asp:Label ID="lblLastName" Font-Bold="true" ForeColor= "red" Text="Last Name: *" runat="server"></asp:Label>
</td>
<td>
<asp:textbox ID="txtLastName" runat="server" width="200px" text='<%# Bind("LastName") %>'></asp:textbox>
</td>
</tr>
<tr>
<td colspan="2" align="center"><br /><br />
<asp:Button ID="btnSave" runat="server" CausesValidation="true" Text="Save"
onclick="btnSave_Click"></asp:Button>
</td>
</tr>
<tr>
</table>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
VB.NET:
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
odsUsers.update()
End Sub
Last edited by a moderator: