Monday, March 26, 2012

OK Need help on custom classes

I have built a simple ad rotator system that rotates ads every 30 minutes. I compiled it as a web service and everything worked fine. Now I'm trying to compile it as a standard class and I'm getting the following error.

*****************************************************************************
Compiler Error Message: BC30182: Type expected.

Source Error:

Line 8: Sub Page_Load(Source as Object, E as EventArgs)
Line 9: If Not(Page.IsPostBack)Then
Line 10: Dim MyFeaturedItems As New FeaturedProducts
Line 11: Dim MyDS as New DataSet()
Line 12: 'A Value of 1 Equals Office Supplies, 2 Equals School, 3 Equals Random

*****************************************************************************
This is my adjusted code that I'm using to create the standard class
*****************************************************************************


Imports System.Xml
Imports System.Web.Services
Imports System.Data.OleDB
Imports System.Data
Imports System
Imports System.Random
Imports System.Web

NameSpace FeaturedProducts.Net
Public Class FeaturedProducts
Inherits WebService

Public Function CurrentProducts(Dept As String)As DataSet
Dim RandNum As New Random()
Dim DisplayNow As Integer
DisplayNow = RandNum.Next(0,6)+1

Dim MySQL as String
If (Dept = 1)Then
MySQL=("SELECT * FROM OfficeProducts WHERE AdTime = "&DisplayNow)
Else If (Dept = 2)Then
MySQL=("SELECT * FROM SchoolProducts WHERE AdTime = "&DisplayNow)
Else If (Dept = 3)Then
MySQL=("SELECT * FROM RandomProducts WHERE AdTime = "&DisplayNow)
End If
Dim ConnString as String = ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=") & server.mappath("\data\featureditems.mdb")
Dim MyConn as New OleDbConnection(ConnString)
Dim MyDS as New DataSet()
Dim MyCmd as New OleDbDataAdapter(MySQL,MyConn)
MyCmd.Fill(MyDS,"FeaturedItems")
Return MyDS
End Function
End Class
End NameSpace


*****************************************************************************
Can someone please help me??

Thanks,
JackieHi jackiea,

What happens when you remove the() from

Dim MyDS as New DataSet()

Curious,
Jason
Did you import the namespace?
nicequy

Nothing changed when I removed the ().

belroy

Yes, I did import the namespace. Here is the actually code.
*****************************************************************************
<%@. Control Language="VB" Debug="True" %>
<%@. Import Namespace="System.Net" %>
<%@. Import NameSpace="System.Data" %>
<%@. Import Namespace="System.Web.Mail" %>
<%@. Import NameSpace="System.Data.OleDb" %>
<%@. Import NameSpace="FeaturedProducts.Net" %>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
If Not(Page.IsPostBack)Then
Dim ManningsFeaturedItems As New FeaturedProducts
Dim MyDS as New DataSet()
'A Value of 1 Equals Office Supplies, 2 Equals School, 3 Equals Random
MyDS = ManningsFeaturedItems.CurrentProducts(1)
Repeater1.DataSource = MyDS
Repeater1.DataBind()
End If
End Sub
You'll need the System.Web.UI namespace to refer to the Page object...

You can always add the namespace to the object declarations on any doubt.
I use C# so I would say:

If (Dept = 1) should be Dept="1"

but VB can typecast pretty good so Im not sure.

Use C#

Emil
I tried adding "1" to the class and again nothing changed.
It's showing the actual error when I try and call an instance of the class.

*****************************************************************************
Line 10: Dim ManningsFeaturedItems As New FeaturedProducts
*****************************************************************************
Ok I fixed it by adding the namespace in front of the class.
Like this...
Dim ManningsFeaturedItems As New FeaturedProducts.Net.FeaturedProducts

Shouldn't I be able to leave off the NameSpace since I have imported it?

I mean I don't have to do..
Dim MyDS As New System.Data.DataSet if I have imported the (System.Data) NameSpace.
Are you sure you still want to inherit from the WebService class?

> Public Class FeaturedProducts Inherits WebService
As far as I know I have too, because I can't reference server.mappath without it.
When I try to remove it, it tells me that server (server.mappath) is not declared.
But you're trying to make a regular class, not a web service, right?

Don't forget that you can get the current context like this:

System.Web.HttpContext.Current

so you could do:

string strPath = System.Web.HttpContext.Current.Server.MapPath("myfile.txt");
How should I compile my dll, because apparently I'm not doing it properly?
I say this because I can completely remove my Import NameSpace statement and us the Dim XXX As New FeaturedProducts.Net.FeaturedProducts and everything works fine.
This is what I have for my compiling script.
******************************************************************************
vbc /t:library /r:System.dll /r:system.web.dll /r:system.web.services.dll /r:system.xml.dll /r:system.data.dll /out:.\FeaturedProducts.Net.dll AdRotator2.vb
pause
Ok, I finally worked out all the problems. I have to asume that since NameSpace(FeaturedProducts.Net) and Function(FeaturedProducts) have the same string in the name, it created a conflict. I changed the NameSpace in my class to FeaturedItems.Net and everything works fine now.

Thank you all for your help!!

Final code...


*****************************************************************************
<%@. Control Language="VB" Debug="True" %>
<%@. Import NameSpace="System.Data" %>
<%@. Import NameSpace="System.Data.OleDb" %>
<%@. Import NameSpace="System.Web.UI.WebControls" %>
<%@. Import NameSpace="FeaturedItems.Net" %>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
Dim MyFeaturedItems As New FeaturedProducts()
Dim MyDS as New DataSet
'A Value of 1 Equals Office Supplies, 2 Equals School, 3 Equals Random
MyDS = MyFeaturedItems.CurrentProducts("1")
Repeater1.DataSource = MyDS
Repeater1.DataBind()
End Sub

One more thing.
How do I go about adding caching inside my class.

I have already Imported NameSpace System.Web.Caching, and tried looking for an item in cache. It does not work though.

Here is my error.
*****************************************************************************
Compiler Error Message: BC30469: Reference to a non-shared member requires an object reference.
Can someone please help me with the caching problem? I'm so close to getting this.

Thanks,
Jackie

0 comments:

Post a Comment