incorrect string format

R

RipperT

Don't know if this group covers web apps, but here goes. In VS 2005, I am
trying to get variables to hold thier values during postback from the
server. I convert a text box's user-keyed value to an integer and assign it
to a module level variable, then convert the variable and assign it to a
hidden text box, setting it's EnableViewState to true so it returns with the
reload. Then in the form's load event, I attempt to convert the contents of
the hidden textbox back to an integer thus:

intTotalCredits = Integer.Parse(Me.txtAccumCredits.Text)

and assign it back to the variable and I get a format exception:

"Input String was not in correct format."

I've also tried this with a label.Text and the HiddenField.Value; same
exception. I noticed that the line of code works fine in a sub, but not in
the load event. What gives?

Many thanx,

Ripper

PS: I'm following instructions in a VB textbook. I'm astounded there isn't
an easier way to do this. I understand there used to be a module available
that was unassociated with the web page, what happened to that?
 
S

Scott M.

The problem is this:

intTotalCredits = Integer.Parse(Me.txtAccumCredits.Text)

What if there is no number entered into the textbox though? Are you using a
validation control on this textbox to ensure only numeric data is entered?

Use: intTotalCredits = CType(txtAccumCredits.Text, Integer) instead

Also (I can't help but to ask), What are you attempting to do here? It
seems like you are going to great lengths to to what ASP.NET already does
for you. You just want to send back down to the client the same data it
posted up to the server? EnableViewState does this for you with no code
what-so-ever.
 
R

RipperT

Many thanx for the response. Sorry I've omitted an explanation. This is a
class assignment. I am attempting to accumulate totals, as in
intTotalCredits += intCredits, so I've got to retain the variable
intTotalCredits on postback.

I will try your suggestion. I've also just noticed that I am using the
form_load event instead of the page_load event. Don't know if that makes a
difference, but will find out! I'm a fish at this.

Thanx again,

Rip
 
R

RipperT

intTotalCredits = Integer.Parse(Me.txtAccumCredits.Text) does not work:

"Conversion from type string "" to 'integer' is not valid"

As I said, if I put it in a sub, it works, but it doesn't work in the load
event. I just don't get it.
If anyone has any ideas, please help! And thanks!

Rip
 
S

Scott M.

This message means that you didn't have any text in the textbox at the time.
You must have numeric data in your textbox when you post the data back to
the server.

As for the ability to increase the data, all you need in Page_Load() is:

'Assume we already have a variable called "totalCredits"
Dim i as Integer = CType(txtAccumCredits.Text, Integer) + totalCredits
txtAccumCredits.Text = i.ToString()

That's it, but as I said, you must have integer data in the textbox when you
post back to the server. This is where a client-side validation control
would help.

Also, there is no "Form_Load()" event in ASP.NET at all, so that definitely
wouldn't work.

As for it working in a "sub", but not "Page_Load"... "Page_Load" is a sub
and it is the correct place to do this work.
 
H

HKSHK

Hello RipperT,
"Conversion from type string "" to 'integer' is not valid"

Is it possible that at load time there is an empty string in that text
field? If so, then the conversion will fail, because an empty string
cannot be converted into an integer.

Best Regards,

HKSHK
 
R

RipperT

Thanks to all for your help. Scott, I will try your suggested code, but I
want to first understand what is going on with the code I have.

intTotalCredits comes from the selected index of a combo box that the user
selects. When they click the button (I am using the value property of the
hiddenField control here, but also tracking it using a visible label, which
is not shown in code below):

Dim intIndex As Integer




With Me

intIndex = .cboCourse.SelectedIndex 'find index in array

intTotalCredits = courseDetails(intIndex).intNumberCredits 'plug #
credits into intTotalCredits

..hidTotalCredits.Value = intTotalCredits.ToString 'Assign # credits to
hidden textbox

End With

Then on reload:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If IsPostBack And hidTotalCredits.Value <> "" Then

intTotalCredits = CType(hidTotalCredits.Value, Integer)

End If

End Sub

What happens is this: on initial load the value of intTotalCredits is 0,
which is what I initialized it to when I declared it at module level. I
select an item from the cboBox, click the button and the value of
intTotalCredits comes up in my label as 0, even tho the selected index of
the cboBox was 3. Then with the next cboBox selection and button click, the
selected index gets plugged into intTotalCredits and it displays in my
label. With the above code, why does intTotalCredits not show up until the
2nd postback?

Thanx again,

Rip
 
S

Scott M.

I think you are making this much more complicated than it need be. You
won't need any hidden text fields or module level variables to make this
work

Also (just a few suggestions), you should not be naming your variables with
prefix that denotes the variable type (int) as this convention is no longer
suggested. Also, we do not have ComboBoxes in ASP.NET so the "cbo" prefix
you are using is incorrect, use "lst" for list if you must use a prefix.
Also, you are using the SelectedIndex from your listbox, but not the value
of that selected index - is that really what you intended?

You haven't provided all of your code (which you should do for best results
around here), so I have to make some assumptions about what you are
currently doing.

Can you just post the entire page's code?
 
R

RipperT

First off, I agree: I am certain I'm making this more complicated than need
be; I just don't know any different. I'm a newbie in a college class, just
trying learn. Below is all the code. Instructor informed me that the
structure and array should work. According to my textbook: EnableViewState
maintains the contents of controls, but not variables. To maintain variable
values, I need to use the hidden field (again according to the text). I
appreciate your taking the time. Many thanx!
Option Strict On

Option Explicit On

Partial Class _Default

Inherits System.Web.UI.Page

Private Const decREG_FEE As Decimal = 25D

Private Const decONLINE_FEE As Decimal = 10D

Private Const decRESIDENT_TUITION As Decimal = 67D

Private Const decNON_DISTRICT_TUITION As Decimal = 120D

Private Const decNON_STATE_TUITION As Decimal = 180D

Private Const decINTERNATIONAL_TUITION As Decimal = 180D

Private Const decFACILITIES_FEE As Decimal = 5D

Friend decTotalTuition As Decimal = 0D

Friend decTotalFees As Decimal = 0D

Friend intTotalCredits As Integer = 0I

Friend decTotalCost As Decimal = 0D

Public Structure Course

Dim strCourseCode As String

Dim decCourseFee As Decimal

Dim intNumberCredits As Integer

End Structure

Private courseDetails(16) As Course

Protected Sub btnSelectCourse_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSelectCourse.Click

With Me

Dim intIndex As Integer = .cboCourse.SelectedIndex 'find index in array

intTotalCredits = courseDetails(intIndex).intNumberCredits 'plug # credits
into intTotalCredits

..txtAccumCredits.Text = intTotalCredits.ToString 'Assign # credits to hidden
textbox

End With

End Sub

Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnClear.Click

' =============================================

' Clear form

' =============================================

txtFirstName.Text = String.Empty

txtLastName.Text = String.Empty

txtStudentID.Text = String.Empty

txtEmailAddress.Text = String.Empty

radioResidencyStatus.SelectedIndex = 0

cboCourse.SelectedIndex = 0

txtAccumCredits.Text = ""

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If IsPostBack And txtAccumCredits.Text <> "" Then

Dim i As Integer = CType(txtAccumCredits.Text, Integer) + intTotalCredits

txtAccumCredits.Text = i.ToString

'lblShow.Text = CStr(i)

End If

courseDetails(0).strCourseCode = "CITA110"

courseDetails(0).decCourseFee = 12D

courseDetails(0).intNumberCredits = 3I

courseDetails(1).strCourseCode = "CITA110"

courseDetails(1).decCourseFee = 12D

courseDetails(1).intNumberCredits = 3I

courseDetails(2).strCourseCode = "CITA115"

courseDetails(2).decCourseFee = 45D

courseDetails(2).intNumberCredits = 3I

courseDetails(3).strCourseCode = "CITA115"

courseDetails(3).decCourseFee = 45D

courseDetails(3).intNumberCredits = 3I

courseDetails(4).strCourseCode = "CITA126"

courseDetails(4).decCourseFee = 45D

courseDetails(4).intNumberCredits = 3I

courseDetails(5).strCourseCode = "CITA126"

courseDetails(5).decCourseFee = 45D

courseDetails(5).intNumberCredits = 3I

courseDetails(6).strCourseCode = "CITA210"

courseDetails(6).decCourseFee = 12D

courseDetails(6).intNumberCredits = 3I

courseDetails(7).strCourseCode = "CITA210"

courseDetails(7).decCourseFee = 12D

courseDetails(7).intNumberCredits = 3I

courseDetails(8).strCourseCode = "CITP110"

courseDetails(8).decCourseFee = 15D

courseDetails(8).intNumberCredits = 4I

courseDetails(9).strCourseCode = "CITP110"

courseDetails(9).decCourseFee = 15D

courseDetails(9).intNumberCredits = 4I

courseDetails(10).strCourseCode = "CITP150"

courseDetails(10).decCourseFee = 20D

courseDetails(10).intNumberCredits = 4I

courseDetails(11).strCourseCode = "CITP150"

courseDetails(11).decCourseFee = 20D

courseDetails(11).intNumberCredits = 4I

courseDetails(12).strCourseCode = "CITP190"

courseDetails(12).decCourseFee = 15D

courseDetails(12).intNumberCredits = 3I

courseDetails(13).strCourseCode = "CITP190"

courseDetails(13).decCourseFee = 15D

courseDetails(13).intNumberCredits = 3I

courseDetails(14).strCourseCode = "CITP200"

courseDetails(14).decCourseFee = 12D

courseDetails(14).intNumberCredits = 3I

courseDetails(15).strCourseCode = "CITD120"

courseDetails(15).decCourseFee = 10D

courseDetails(15).intNumberCredits = 2I

courseDetails(16).strCourseCode = "CITD120"

courseDetails(16).decCourseFee = 10D

courseDetails(16).intNumberCredits = 2I

End Sub



End Class
 
S

Scott M.

Ok Rip, here are my thoughts on this......

I believe the root of your problem stems from the Page_Load routine inside
your If PostBack statement. You were checking txtAccumCredits.Text to see
if it was less than "" (txtAccumCredits.Text < ""), rather than checking to
see if it was equal to an empty string (txtAccumCredits.Text <> ""). You
can't check a string to see if it is less than an empty string. This would
be the cause for your invalid cast exception messages.

It also seems that you are changing the value of txtAccumCredits.Text in
contradictory ways. You are setting a value for this in the Page_Load when
Postback occurs and there is already a value for txtAccumCredits.Text and
you are then continuing to the button_click event handler where the data you
just wrote into the textbox is changed to the # of credits for the selected
course, not the accumulated credit total. This is something you'll need to
work out since I'm not sure what answer you actually intend to get.

Also, if you look at the code below, I have taken the liberty of modifying
it a little and made comments explaining what I've modified and why.

Option Strict On

'This VS .NET setting should already be on by default.
'So, if you are using VS.NET (VS 2005), you don't need this
Option Explicit On

Partial Class _Default
Inherits System.Web.UI.Page

'I've removed the data type prefixes from all the constant
'and variable names as this is no longer considered to be
'a best practice (hasn't been for 5 years now).

Private Const REG_FEE As Decimal = 25D
Private Const ONLINE_FEE As Decimal = 10D
Private Const RESIDENT_TUITION As Decimal = 67D
Private Const NON_DISTRICT_TUITION As Decimal = 120D
Private Const NON_STATE_TUITION As Decimal = 180D
Private Const INTERNATIONAL_TUITION As Decimal = 180D
Private Const FACILITIES_FEE As Decimal = 5D

Friend totalTuition As Decimal = 0D
Friend totalFees As Decimal = 0D
Friend totalCredits As Integer = 0I
Friend totalCost As Decimal = 0D

Public Structure Course
Dim courseCode As String
Dim courseFee As Decimal
Dim numberCredits As Integer
End Structure

Private courseDetails(16) As Course

Protected Sub btnSelectCourse_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSelectCourse.Click

'find index in array
'Note the change of name for the drop down list
'since there aren't any combo boxes in ASP.NET
Dim index As Integer = lstCourse.SelectedIndex

'plug # credits into totalCredits
totalCredits = courseDetails(index).numberCredits

'Assign # credits to hidden textbox
txtAccumCredits.Text = totalCredits.ToString
End Sub


Protected Sub btnClear_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnClear.Click

' =============================================
' Clear form
' =============================================

txtFirstName.Text = String.Empty
txtLastName.Text = String.Empty
txtStudentID.Text = String.Empty
txtEmailAddress.Text = String.Empty
radioResidencyStatus.SelectedIndex = 0
cboCourse.SelectedIndex = 0
txtAccumCredits.Text = ""
End Sub

Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
Handles Me.Load

'You can't do math comparisons with string data
'as in your original line of code below.
'If IsPostBack And txtAccumCredits.Text < "" Then

'The line should be...
If IsPostBack AndAlso txtAccumCredits.Text <> "" Then
Dim i As Integer = CType(txtAccumCredits.Text, Integer) + totalCredits
txtAccumCredits.Text += i.ToString
End If

courseDetails(0).courseCode = "CITA110"
courseDetails(0).courseFee = 12D
courseDetails(0).numberCredits = 3I
courseDetails(1).courseCode = "CITA110"
courseDetails(1).courseFee = 12D
courseDetails(1).numberCredits = 3I
courseDetails(2).courseCode = "CITA115"
courseDetails(2).courseFee = 45D
courseDetails(2).numberCredits = 3I
courseDetails(3).courseCode = "CITA115"
courseDetails(3).courseFee = 45D
courseDetails(3).numberCredits = 3I
courseDetails(4).courseCode = "CITA126"
courseDetails(4).courseFee = 45D
courseDetails(4).numberCredits = 3I
courseDetails(5).courseCode = "CITA126"
courseDetails(5).courseFee = 45D
courseDetails(5).numberCredits = 3I
courseDetails(6).courseCode = "CITA210"
courseDetails(6).courseFee = 12D
courseDetails(6).numberCredits = 3I
courseDetails(7).courseCode = "CITA210"
courseDetails(7).courseFee = 12D
courseDetails(7).numberCredits = 3I
courseDetails(8).courseCode = "CITP110"
courseDetails(8).courseFee = 15D
courseDetails(8).numberCredits = 4I
courseDetails(9).courseCode = "CITP110"
courseDetails(9).courseFee = 15D
courseDetails(9).numberCredits = 4I
courseDetails(10).courseCode = "CITP150"
courseDetails(10).courseFee = 20D
courseDetails(10).numberCredits = 4I
courseDetails(11).courseCode = "CITP150"
courseDetails(11).courseFee = 20D
courseDetails(11).numberCredits = 4I
courseDetails(12).courseCode = "CITP190"
courseDetails(12).courseFee = 15D
courseDetails(12).numberCredits = 3I
courseDetails(13).courseCode = "CITP190"
courseDetails(13).courseFee = 15D
courseDetails(13).numberCredits = 3I
courseDetails(14).courseCode = "CITP200"
courseDetails(14).courseFee = 12D
courseDetails(14).numberCredits = 3I
courseDetails(15).courseCode = "CITD120"
courseDetails(15).courseFee = 10D
courseDetails(15).numberCredits = 2I
courseDetails(16).courseCode = "CITD120"
courseDetails(16).courseFee = 10D
courseDetails(16).numberCredits = 2I
End Sub

End Class
 
R

RipperT

I'm convinced that, as you've written it (which is essentially the same
thing I started out with), the statements inside the If IsPostBack are not
firing. I know the 3 statements that capture the number of credits, convert
them to text, and display them in the hidden text box are working, because
the textbox is not hidden right now; I can see the figure in the text box.
Every subsequent selection of a course simply displays the number of credits
for that course. There is no accumulation. If I change the <> to =, the
statements inside the If fire, and of course, throw an exception because the
text box is empty. I'm befuddled. I'm going to zip the whole thing and send
it to my instructor. Thanx again for taking time to assist.

Rip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top