Answered by:
pass variable to external function

Question
-
User139366633 posted
Hello,
I have been trying to correctly pass the value of a textbox as a variable to a function that reside in my App_Code folder when triggered by a click event. I can't seem to get it right.
Here is the code behind for the aspx page with the textbox and the click event: (Error is on 4th line)
Public Sub btnCouponCode_Click1(ByVal sender As Object, ByVal e As System.EventArgs, Optional ByVal CouponCode As Char) Handles btnCouponCode.Click lblSubtotal.Text = Calculations.CalcSubTotalPrice().ToString("C") lblShipping.Text = Calculations.CalcSAndH().ToString("C") lblCouponDiscount.Text = Calculations.CalcCouponDiscount().ToString("C") lblTotalPrice.Text = Calculations.CalcTotalPrice().ToString("C") End Sub
here is the function's code: (5th line in second function shows error)
Public Shared Function CalcCouponDiscount(ByVal CouponCode As Char, ByRef e As System.EventArgs) As Decimal Dim lblCouponError As New Label Dim couponDiscount As Decimal Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=C:\Users\Perry\Documents\Visual Studio 2008\WebSites\OnlineStore\App_Data\SmallCompanyDB.mdf;Integrated Security=true;User Instance=true") conn.Open() Dim cmd As New SqlCommand("SELECT * FROM CouponTable WHERE (CouponCode=@CouponCode)", conn) cmd.CommandType = Data.CommandType.Text cmd.Parameters.Add("CouponCode", Data.SqlDbType.VarChar).Value = CouponCode Dim dr As SqlDataReader = cmd.ExecuteReader() dr.Read() If dr.HasRows Then couponDiscount = FormatCurrency(dr.Item(2), 2) Return couponDiscount Else lblCouponError.Text = "coupon Invalid" End If conn.Dispose() conn.Close() End Function Public Shared Function CalcTotalPrice() As Decimal Dim totalprice As Decimal totalprice = CalcSubTotalPrice() totalprice = totalprice + CalcSAndH() totalprice = totalprice - CalcCouponDiscount() Return totalprice End Function
All error relate to argument not being specified.
Thanks I'm getting a headache.
Saturday, December 12, 2009 6:03 PM
Answers
-
User2069888697 posted
If i am reading it right your funtion requires values.
Public Shared Function CalcCouponDiscount(ByVal CouponCode As Char, ByRef e As System.EventArgs) As Decimal
your request should be something like
lblCouponDiscount.Text = Calculations.CalcCouponDiscount("abc",e).ToString("C")
but from the looks of it you don't need the ByRef.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 12, 2009 7:37 PM -
User2069888697 posted
You need to figure out what the value you are assigning is, is it an object,integer,array???
I do not use char very often mostly string and some types can easily be converted prior to using them
cint("1") converts string to integer, cstr, cchar, cdate, cobj
You just need to find out what your trying to send and see if it will convert.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 12, 2009 10:30 PM -
User139366633 posted
Thanks you to all who replied. It was pointed out to me in a different thread that I needed to add a length attribute for data type to my parameter string. Example is below:
cmd.Parameters.Add(
New SqlParameter("CouponCode", Data.SqlDbType.VarChar, 6, Data.ParameterDirection.Input)).Value = CouponCode.ToString
The addition of this little, but not insignificant, attribute was what causing my error. I did alot of browsing to try and find the problem, and almost evey post blambed this type of behavior of incorrect data typ formats between the database and either the control or parameter that is using/supplying the variable.
Here is the thread where it was discovered:
http://forums.asp.net/t/1503430.aspx
Hope this helps.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, December 13, 2009 10:25 AM
All replies
-
User2069888697 posted
If i am reading it right your funtion requires values.
Public Shared Function CalcCouponDiscount(ByVal CouponCode As Char, ByRef e As System.EventArgs) As Decimal
your request should be something like
lblCouponDiscount.Text = Calculations.CalcCouponDiscount("abc",e).ToString("C")
but from the looks of it you don't need the ByRef.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 12, 2009 7:37 PM -
User139366633 posted
That corrected the problem right off. Now I am getting cast/covert errors. Since the compiler is telling me this: Conversion from type 'combinedcart_aspx' to type 'Char' is not valid, where the aspx page is the page generating the control, I assume that the syntax is still not correct.
No specific lines are being blamed for the error. I checked and the SQL column, ByVal statement, and sqlparameters all are now char or equivalent.
Any ideas?
Thanks,
Saturday, December 12, 2009 8:16 PM -
User2069888697 posted
You need to figure out what the value you are assigning is, is it an object,integer,array???
I do not use char very often mostly string and some types can easily be converted prior to using them
cint("1") converts string to integer, cstr, cchar, cdate, cobj
You just need to find out what your trying to send and see if it will convert.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 12, 2009 10:30 PM -
User139366633 posted
Thanks you to all who replied. It was pointed out to me in a different thread that I needed to add a length attribute for data type to my parameter string. Example is below:
cmd.Parameters.Add(
New SqlParameter("CouponCode", Data.SqlDbType.VarChar, 6, Data.ParameterDirection.Input)).Value = CouponCode.ToString
The addition of this little, but not insignificant, attribute was what causing my error. I did alot of browsing to try and find the problem, and almost evey post blambed this type of behavior of incorrect data typ formats between the database and either the control or parameter that is using/supplying the variable.
Here is the thread where it was discovered:
http://forums.asp.net/t/1503430.aspx
Hope this helps.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, December 13, 2009 10:25 AM