Sto provando a utilizzare un controllo objectDataSource in un listato per accedere ai dati di un database.In questo listato è stata creata una classe che gestisce tutte le operazioni sul database.Ora la documentazione richiede che nel campo TypeName di objectDataSource sia inserito il nome della classe.Ho provato ma non funziona.Mi sapreste aiutare? Grazie mille.
Per comodità vi posto il codice:
Imports System.Data
Imports System.Data.SqlClient
Public Class ProductInfo3
Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database=banking"
Public Shared Function GetProducts() As SqlDataReader
Dim con As New SqlConnection(conString)
Dim selectString As String = "SELECT *, " & _
FROM tblUsers "
Dim cmd As New SqlCommand(selectString, con)
con.Open()
Dim dtr As SqlDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dtr
End Function
Public Shared Sub UpdateProduct(ByVal original_productId _
As Integer, ByVal productName As String, _
ByVal unitPrice As Decimal)
Dim con As New SqlConnection(conString)
Dim updateString As String = "UPDATE tblUsers " & _
"SET FirstName=@FirstName & _
"WHERE UserID=@UserID"
Dim cmd As New SqlCommand(updateString, con)
cmd.Parameters.AddWithValue("@FirstName", FirstName)
cmd.Parameters.AddWithValue("@LastName", LastName)
cmd.Parameters.AddWithValue("@Zip", Zip)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Public Shared Sub DeleteProduct(ByVal original_productId _
As Integer)
Dim con As New SqlConnection(conString)
Dim deleteString As String = "DELETE tblUsers " & _
"WHERE FirstName=@FirstName"
Dim cmd As New SqlCommand(deleteString, con)
cmd.Parameters.AddWithValue("@FirstName", FirstName)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
End Class
<html>
<head>
<title>Show Users</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView
ID="GridView1"
DataSourceID="ObjectDataSource1"
DataKeyNames="UserID"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
Runat="Server">
<Columns>
<asp:BoundField
DataField="FirstName"/>
<asp:BoundField
DataField="LastName"
/>
</Columns>
</asp:GridView>
<asp:ObjectDataSource
ID="ObjectDataSource1"
TypeName="ProductsInfo3"
SelectMethod="GetProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct"
Runat="Server" />
</form>
</body>
</html>
Per comodità vi posto il codice:
Imports System.Data
Imports System.Data.SqlClient
Public Class ProductInfo3
Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database=banking"
Public Shared Function GetProducts() As SqlDataReader
Dim con As New SqlConnection(conString)
Dim selectString As String = "SELECT *, " & _
FROM tblUsers "
Dim cmd As New SqlCommand(selectString, con)
con.Open()
Dim dtr As SqlDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dtr
End Function
Public Shared Sub UpdateProduct(ByVal original_productId _
As Integer, ByVal productName As String, _
ByVal unitPrice As Decimal)
Dim con As New SqlConnection(conString)
Dim updateString As String = "UPDATE tblUsers " & _
"SET FirstName=@FirstName & _
"WHERE UserID=@UserID"
Dim cmd As New SqlCommand(updateString, con)
cmd.Parameters.AddWithValue("@FirstName", FirstName)
cmd.Parameters.AddWithValue("@LastName", LastName)
cmd.Parameters.AddWithValue("@Zip", Zip)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
Public Shared Sub DeleteProduct(ByVal original_productId _
As Integer)
Dim con As New SqlConnection(conString)
Dim deleteString As String = "DELETE tblUsers " & _
"WHERE FirstName=@FirstName"
Dim cmd As New SqlCommand(deleteString, con)
cmd.Parameters.AddWithValue("@FirstName", FirstName)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
End Class
<html>
<head>
<title>Show Users</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView
ID="GridView1"
DataSourceID="ObjectDataSource1"
DataKeyNames="UserID"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
Runat="Server">
<Columns>
<asp:BoundField
DataField="FirstName"/>
<asp:BoundField
DataField="LastName"
/>
</Columns>
</asp:GridView>
<asp:ObjectDataSource
ID="ObjectDataSource1"
TypeName="ProductsInfo3"
SelectMethod="GetProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct"
Runat="Server" />
</form>
</body>
</html>