|
-
Jan 3rd, 2010, 02:07 AM
#1
Thread Starter
Member
[RESOLVED] Calling a Sub in a Different aspx.vb File
I need to call a sub which must be contained within a separate web page, but even though it is declared as public, when I call it like this:
Code:
Cart.addMovie(ByVal title As String, ByVal format As String, _
ByVal copies As Integer, ByVal price As Double)
I'm getting this error:
Code:
Error 1 Reference to a non-shared member requires an object reference. C:\Documents and Settings\John Wesley Cooper\My Documents\College\cis259\MovieStore\MovieStore\Default.aspx.vb 48 9 MovieStore
...so what must I do to call this procedure as I need to?
Last edited by jwesleycooper; Jan 11th, 2010 at 01:03 AM.
-
Jan 3rd, 2010, 02:51 AM
#2
Re: Calling a Sub in a Different aspx.vb File
i don't think your web app should even compile when you try to call like this...
you want to call it not declare it
Code:
Cart.addMovie(title,format, _
copies, price)
make sure that : title, format, copies, price has values...
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Jan 3rd, 2010, 06:53 AM
#3
Re: Calling a Sub in a Different aspx.vb File
Hey,
Where is Cart instantiated?
I would question why you need to use this approach. If the method that is on the other page is common to both, then I would recommend that you extract that method and put in it's own class, and put it in the App_Code folder, or perhaps even it's own class library.
Gary
-
Jan 3rd, 2010, 08:22 AM
#4
Re: Calling a Sub in a Different aspx.vb File
Calling a method in another page requires you to instantiate that other page, which is a waste of resources. When you move addMovie to a common class, you can make it static/shared if you like.
Declare a class called CartHelper and put your method in there. If your addMovie method talks to any UI elements then you'll need to separate that logic out as well, make it a standalone function that does something and returns something without touching any controls.
-
Jan 3rd, 2010, 09:04 AM
#5
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
What I need to do is use values that are selected/specified on the 1st page as input for variables on the second... so what would be the best course of action in this case, and how exactly can I do such?
-
Jan 3rd, 2010, 09:26 AM
#6
Re: Calling a Sub in a Different aspx.vb File
Pass it to the second page. A common way to do this is the querystring. On Page1, when the first 'value' is selected,
Response.Redirect("Page2.aspx?category=Books")
On Page2, you read the category using Request.QueryString("category")
-
Jan 3rd, 2010, 01:57 PM
#7
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
The values need to all be sent at the same time, so would this work from within my cartButton_Click Sub? Additionally, one of the values I need must be taken from my database via the Cart (2nd) Form and placed into a contructor as a parameter there... So what must I do to accomplish all of this?
-
Jan 3rd, 2010, 06:19 PM
#8
Re: Calling a Sub in a Different aspx.vb File
You can pass many values in a querystring like Page2.aspx?category=Books&var2=aaa&var3=bbb etc
You retreve them by name on the 2nd page Request.QueryString("category") Request.QueryString("var2") etc..
When you get the value from the database is up to you. You could do it on the 2nd page before calling the cart method....
-
Jan 3rd, 2010, 09:12 PM
#9
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
I just implemented it as you all indicated, but the passed values are not loading as I need them to, though they are placed into the request url... So here's my code so far:
1st page:
Code:
Private Sub cartButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cartButton.Click
Response.Redirect("Cart.aspx?title=" & titleDropDownList.SelectedItem.Text _
& "&format=" & formatDropDownList.SelectedItem.Text _
& "&copies=" & quantityTextBox.Text)
End Sub
and the 2nd:
Code:
Public Partial Class Cart
Inherits System.Web.UI.Page
Private inCart As ArrayList = New ArrayList()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.ToString.Contains("?") Then
Dim selectedItem As CartItem = New CartItem(Request.QueryString("title"), _
Request.QueryString("format"), _
Integer.Parse(Request.QueryString _
("copies")))
If inCart.Count > 1 Then
Dim x As Integer = 0
Dim identical As Boolean = False
Do
If inCart.Item(x).IsIdentical(selectedItem) Then
identical = True
x = inCart.Count
Else
x = x + 1
End If
Loop Until x = inCart.Count
If Not identical Then
inCart.Add(selectedItem)
End If
Else
inCart.Add(selectedItem)
End If
End If
For Each i As CartItem In inCart
cartCheckBoxList.Items.Add(i.Copies & " copies of " & i.Title & " in " _
& i.Format & " format at $" & i.PricePerCopy _
& " each")
Next
End Sub
Private Sub calcTotal()
Dim numberMovies As Integer = 0
Dim totalPrice As Double = 0.0
If inCart.Count > 1 Then
For c As Integer = 0 To inCart.Count - 1
numberMovies = numberMovies + inCart.Item(c).Copies()
totalPrice = totalPrice + inCart.Item(c).Copies() _
* inCart.Item(c).PricePerCopy()
Next
Else
numberMovies = inCart.Item(0).Copies()
totalPrice = numberMovies * inCart.Item(0).PricePerCopy()
End If
totalLabel.Text = "Total Movies: " & numberMovies & ControlChars.Tab _
& "Total Price: $" & totalPrice
End Sub
Public Class CartItem
Private _title As String
Private _format As String
Private _copies As Integer
Private _pricePerCopy As Double
Public Sub New(ByVal title As String, ByVal format As String, _
ByVal copies As Integer)
_title = title
_format = format
_copies = copies
If format.Equals("VHS") Then
_pricePerCopy = 9.99
Else
_pricePerCopy = 19.99
End If
End Sub
Public ReadOnly Property Title() As String
Get
Return _title
End Get
End Property
Public ReadOnly Property Format() As String
Get
Return _format
End Get
End Property
Public ReadOnly Property Copies() As Integer
Get
Return _copies
End Get
End Property
Public ReadOnly Property PricePerCopy() As Double
Get
Return _pricePerCopy
End Get
End Property
Public Function IsIdentical(ByVal movie As CartItem) As Boolean
If Title = movie.Title AndAlso Format = movie.Format Then
Return True
Else
Return False
End If
End Function
End Class
End Class
So what's wrong here?
-
Jan 4th, 2010, 09:17 AM
#10
Re: Calling a Sub in a Different aspx.vb File
Hey,
Have you tried stepping into the code? Have you set any breakpoints?
Out of interest, why did you choose to do this:
Code:
If Request.ToString.Contains("?") Then
Did you see that somewhere?
Gary
-
Jan 4th, 2010, 08:12 PM
#11
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
 Originally Posted by gep13
...Out of interest, why did you choose to do this:
Code:
If Request.ToString.Contains("?") Then
Did you see that somewhere?
Gary
That's where the problem was! It seems that the .ToString method didn't return the actual contents of the Request, thus not adding anything to the cart! I was adding that line in case this page were somehow called without any queriable input, so to prevent any exceptions from being raised via null input... or could that even cause a problem in these circumstances? If so, how can I set this up to not add a null CartItem to my ArrayList whenever the Cart page is called without passed values?
Oh yes, and how can I prevent the previous CartItem(s) from being removed when the user navigates away from the cart page?
Last edited by jwesleycooper; Jan 4th, 2010 at 08:39 PM.
-
Jan 5th, 2010, 03:41 AM
#12
Re: Calling a Sub in a Different aspx.vb File
Ah, hold on, looking at that again, that would never work. You would need to use another member of the Request Class, i.e. the Url Property if you wanted to use that approach:
http://msdn.microsoft.com/en-us/libr...t_members.aspx
The correct approach to this though would be to check for the QueryString parameters that you need to continue, if there isn't one that you need to continue, throw a new ArgumentException.
Gary
-
Jan 6th, 2010, 11:39 PM
#13
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
...but how do I keep my inCart ArrayList variable from being cleared when the user leaves the page?
-
Jan 7th, 2010, 02:22 AM
#14
Fanatic Member
Re: Calling a Sub in a Different aspx.vb File
you can either store its value(s) into database or Session object. In that way your arraylist will keep having the previous items. I suggest database (table shoppingcart) and using cookie to identify the shopcart ID.
-
Jan 7th, 2010, 02:45 AM
#15
Re: Calling a Sub in a Different aspx.vb File
selanec is pretty much spot on.
The only other thing that I would mention is the use of the ArrayList. I think you would be better off altering the code to use a generic list, rather than limiting yourself to this.
Gary
-
Jan 7th, 2010, 10:26 PM
#16
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
I was considering that at first, but I don't know how to add records to a data table via vb code, no one in the Database Development forum will tell me how on earth to do this, and the textbook I've got doesn't even mention it! So could somebody please tell me how this is done?!?
-
Jan 8th, 2010, 03:45 AM
#17
Re: Calling a Sub in a Different aspx.vb File
Hey,
I am not clear on exactly what you are referring to. Can you elaborate? Or perhaps show the code that you are now using?
Gary
-
Jan 8th, 2010, 10:11 AM
#18
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
What I need to be able to do is add the data selected/inputted by the user on the first page into a new record in a pre-constructed cart table of my database... but I haven't a clue how to do this.
-
Jan 8th, 2010, 10:16 AM
#19
Re: Calling a Sub in a Different aspx.vb File
Hey,
So what you need to learn about is ADO.Net. There are a number of links in my signature that will help you learn up on this, but the basic steps are as follows:
1) Create a connection to your database
2) Create a command object with the necessary query that you need to insert/update/read/delete the information in the database
3) Create any parameters that are needed and add them to the command
4) Execute the command
5) Iterate the results if required.
Gary
-
Jan 8th, 2010, 04:46 PM
#20
Re: Calling a Sub in a Different aspx.vb File
Read an ADO.NET tutorial. You want to pay specific attention to calling a stored proc from code (which can do the insert for you)
-
Jan 8th, 2010, 11:54 PM
#21
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
Three dumb questions I need answered ASAP:
- How can I set-up my CartTable to automatically generate an ID field whenever the rest of a record inserted?
- How do I implement structured exception handling for Edit and Insert commands automatically generated for a DetailsView Component?
- How can I utilize ReportViewer controls to create a separate page that displays the entire store inventory?
-
Jan 9th, 2010, 02:45 AM
#22
Fanatic Member
Re: Calling a Sub in a Different aspx.vb File
I will answer just the 1st question.
Usually you generate cartid by yourself. Then when you deal with the table you check the cookie and if it not exists you generate new cartID:
Code:
Dim cartId As String = Guid.NewGuid.ToString
The field datatype that you store this value in, is char(36)
-
Jan 9th, 2010, 09:17 AM
#23
Re: Calling a Sub in a Different aspx.vb File
Hey,
1) Either the above, or you can configure the database to do this for you. Either with a GUID, as described, or have it as an AutoIncrementing field.
2) Don't. I would strongly urge you to take more control over the commands that are being generated for you. That way you have more flexibility, and know exactly what is going on in your code.
3) http://msdn.microsoft.com/en-us/libr...7(SQL.80).aspx
Gary
-
Jan 9th, 2010, 11:26 AM
#24
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
How do I make the field autoincrement?
Additionally, though I understand that I'd be better off defining the commands myself, I really need to get this project done right away; so I need to know how I can modify just the exception handling this time around.
Last edited by jwesleycooper; Jan 9th, 2010 at 11:34 AM.
-
Jan 9th, 2010, 11:38 AM
#25
Re: Calling a Sub in a Different aspx.vb File
Hey,
What database are you using? SQL Server? MySQL?
Each database is different.
You can do it through a SQL Query as defined here:
http://www.w3schools.com/Sql/sql_autoincrement.asp
But it's easier to do this in a database IDE, such as SQL Server Management Studio.
That's just it though, doing it using wizards, datasources, etc, means that you are limited in what you can actually do. For instance, the only thing you might be able to do is this:
http://www.15seconds.com/issue/030102.htm
However, if you implement it in code, you have far more control over the exception handling.
Gary
-
Jan 9th, 2010, 03:06 PM
#26
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
What I need to do is not only prevent the user from entering a null value, or one that cannot be contained by the datatype used, but also prevent passable yet incorrect values from being placed into my SQL Server Database. For instance, the format feild should only contain either a "VHS" or a "DVD" value... but a user could still enter "VH", or "vhs", which would cause problems for processing later on. How can I define and throw an exception for this kind of issue?
-
Jan 9th, 2010, 03:44 PM
#27
Fanatic Member
Re: Calling a Sub in a Different aspx.vb File
I would do this on the client-side using the built-in Validator controls. Of course you can always make an extra checking on the server-side but usually you do it on the client-side.
you just specify which TextBox control to be validated and add some regular expression for the pattern you want it matches and voila. That's pretty simple model to be implemented.
-
Jan 9th, 2010, 04:49 PM
#28
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
So how do I incorporate Validator controls into my DetailsView?
-
Jan 10th, 2010, 02:38 PM
#29
Re: Calling a Sub in a Different aspx.vb File
Hey,
It is not just on the client side that you need to do this, you need to do this on the server side as well. However, the ASP.Net Validation controls do both for you 
You would need to create a TemplateField for each of the values that you want to validate, then add the necessary validation control:
http://www.asp.net/(S(pdfrohu0ajmwt4...ial-19-vb.aspx
Hope that helps!
Gary
-
Jan 10th, 2010, 07:23 PM
#30
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
How can I make my validator check that the input for and SKU contains 6 numeric characters, that the Format is either "VHS" or "DVD", that the rating is one of several possible values, and that the runtime is entered as 2 numbers, a colon, and two more numbers? The compare validator only seems to check against a single static value, and I have no idea how to implement a custom validator...
-
Jan 10th, 2010, 11:18 PM
#31
Thread Starter
Member
Re: Calling a Sub in a Different aspx.vb File
Figured it out from studying the MSDN Page on CustomValidators! 
Actually, I think I've resolved all of my issues now! Thanks a million gep13, you're a life-saver!
Last edited by jwesleycooper; Jan 10th, 2010 at 11:31 PM.
-
Jan 11th, 2010, 03:11 AM
#32
Re: [RESOLVED] Calling a Sub in a Different aspx.vb File
Hey,
Ah, yeah, I should have mentioned that the CustomValidator might be the way to go. Did you implement both the client and server side check?
Gary
-
Jan 13th, 2010, 05:23 AM
#33
Re: Calling a Sub in a Different aspx.vb File
 Originally Posted by jwesleycooper
Three dumb questions I need answered ASAP:
- How can I set-up my CartTable to automatically generate an ID field whenever the rest of a record inserted?
- How do I implement structured exception handling for Edit and Insert commands automatically generated for a DetailsView Component?
- How can I utilize ReportViewer controls to create a separate page that displays the entire store inventory?
1 - If it's a database table make it autoincrement. If it's a datatable (in code), use a new Guid if you need a unique ID. What you do depends on how you'll be using it.
2 - Try Catch around it?
-
Jan 13th, 2010, 05:24 AM
#34
Re: Calling a Sub in a Different aspx.vb File
 Originally Posted by jwesleycooper
What I need to do is not only prevent the user from entering a null value, or one that cannot be contained by the datatype used, but also prevent passable yet incorrect values from being placed into my SQL Server Database. For instance, the format feild should only contain either a "VHS" or a "DVD" value... but a user could still enter "VH", or "vhs", which would cause problems for processing later on. How can I define and throw an exception for this kind of issue?
Then don't give them the choice. Use a dropdownlist so that they can pick from a list of predetermined values, that's the purpose of DDLs - to get the user to pick from a specific list rather than typing it in freely. If you use a textbox and custom validation, there'll always be a way around it unless you validate client side as well, but then it still sucks because
1) It's not intuitive to the user
2) Duplicating effort
-
Jan 13th, 2010, 05:25 AM
#35
Re: Calling a Sub in a Different aspx.vb File
 Originally Posted by jwesleycooper
How can I make my validator check that the input for and SKU contains 6 numeric characters, that the Format is either "VHS" or "DVD", that the rating is one of several possible values, and that the runtime is entered as 2 numbers, a colon, and two more numbers? The compare validator only seems to check against a single static value, and I have no idea how to implement a custom validator... 
And this would be a Regular Expression Validator.
-
Jan 13th, 2010, 05:25 AM
#36
Re: [RESOLVED] Calling a Sub in a Different aspx.vb File
Ah, good point, never occurred to me to suggest a DDL. Thinking about it, this would make much more sense.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|