Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I need to convert a varchar (value in a textbox, which I thought is supposed to be a string but it keeps telling me it's a varchar) to a decimal. I am not sure how to go about it. The most obvious way to do this (I would have thought) I put below. However, that doesn't work. I get the error: "Input string was not in correct format." Does Convert.ToDecimal only work with strings?

 

Dim surveyNum As Decimal= Convert.ToDecimal(txtSurveyNum.Text)

 

I have also had someone tell me to try this but it won't work:

Dim surveyNum As Decimal = Decimal.Parse(txtSurveyNum.Text, Globalization.NumberStyles.Number)

 

I am running Visual Studio .net 2003, and programming a VB asp.net web app.

 

I am fairly new to VB so I could be missing something very simple. Any help would be greatly appreciated, I am really stuck on this. Thnx

Posted
When are you getting this error? in the IDe, at runtime? I put the code you have in a sample form and did not get any errors.
"Nobody knows what I do until I stop doing it."
Posted

The program compiles fine, I get the error during runtime. I have the webpage set up so that when I hit the submit button it goes to a cmdSubmit_click sub. When I click the submit button, I get an Error page with the "Input String was not in correct format" error.

I am trying to add the decimal value to a database (which takes regular decimal variables just fine) but when I try to take the value from a textbox, turn it into a decimal and INSERT it into the database I get this error. I don't understand why it would insert a regular variable, type decimal, and not one that is converted to type decimal. That is why I figured the convert was not working. Here is the code in my cmdSubmit_click sub:

 

Dim surveyNum As Decimal = Decimal.Parse(txtSurveyNum.Text, Globalization.NumberStyles.Number)

myCmd.CommandText = "INSERT INTO survey(ID) VALUES('" & surveyNum & "')"

myConn.Open()

Try

myCmd.ExecuteNonQuery()

lblMessage.Text = "Record successfully updated"

Catch

lblMessage.Text = "Query error: " & Err.Description

End Try

myConn.Close()

Posted

The error occurs with the line:

Dim surveyNum As Decimal = Decimal.Parse(txtSurveyNum.Text, Globalization.NumberStyles.Number)

 

and says: Input string was not in a correct format.

  • Administrators
Posted

If the error occurs on the line you stated above

Dim surveyNum As Decimal = Decimal.Parse(txtSurveyNum.Text, Globalization.NumberStyles.Number)

then it is failing before the insert statement is being run (also you don't have to put single quotes around numbers during an insert). In the debugger what does it display as the value for txtSurveyNum.Text - is it actually displaying what you enter in the webpage?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Why not convert the text into a integer.

 

I.E.

Dim intNumber As Integer
intNumber = System.Convert.ToInt32(txtSurveyNum.Text)

 

Hope this helps

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Posted
I figured out my problem, it was actually quite stupid of me. I didn't include the code in the page load : If (Not Page.IsPostBack) Then ... End If. So no matter what I put in the textbox, it was always empty when I tried to convert it. Everything works fine now with this line.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...