Need help creating reusable code in a class.
Hi all,
This is what I am trying to do. I am converting an ASP app to ASP.NET. They put their code into an ASP include file. They fill dropdowns etc by calling stored procedures and looping through a recordset and creating an HTML string. They send it back to the calling page to create the dropdown <%=GetPlans()%>
I want to create the same reusable code in .NET. I created a classfile and put a function into it(there will be many). Here is the code:
Imports System.Data.SqlClient
Public Class TestClass
Public Function GetPlans()
Dim dsPlans As New DataSet()
Dim sqlConn As New SqlConnection()
sqlConn.ConnectionString = "data source=MOTLEYCRUE;initial catalog=PATII;integrated security=SSPI;persist security info=False;packet size=4096"
Dim sdaPlans As New SqlClient.SqlDataAdapter("dbo.[CaseManagementGetPlans]", sqlConn)
sdaPlans.Fill(dsPlans, "Plans")
Return dsPlans
End Function
End Class
Here is the code in my aspx page:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim x As New TestClass()
Protected WithEvents ddl1 As System.Web.UI.WebControls.DropDownList
Dim ds As New DataSet()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
ds = x.GetPlans
ddl1.DataSource = ds
ddl1.DataMember = ds.Tables(0).TableName
ddl1.DataTextField = ds.Tables(0).Columns(1).ColumnName
ddl1.DataValueField = ds.Tables(0).Columns(0).ColumnName
End Sub
End Class
The problem is that it runs with no errors, but my drop down list is empty. Am I doing this correctly, is there a better way? Any help and advice would be greatly appreciated.
Thanks
Jim
|