Jaguar3311 Posted December 7, 2004 Posted December 7, 2004 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 Quote
Rick_Fla Posted December 7, 2004 Posted December 7, 2004 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. Quote "Nobody knows what I do until I stop doing it."
Jaguar3311 Posted December 7, 2004 Author Posted December 7, 2004 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() Quote
Administrators PlausiblyDamp Posted December 7, 2004 Administrators Posted December 7, 2004 If you step through the code in a debugger which line actually throws the error? also what is the value of txtSurveyNum.Text when the error occurs? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jaguar3311 Posted December 7, 2004 Author Posted December 7, 2004 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. Quote
Administrators PlausiblyDamp Posted December 7, 2004 Administrators Posted December 7, 2004 and the value of txtSurveyNum.Text was..... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Administrators PlausiblyDamp Posted December 7, 2004 Administrators Posted December 7, 2004 and that is definately what the debugger says as well? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jaguar3311 Posted December 7, 2004 Author Posted December 7, 2004 I found someone who had a similar problem, and included the link. Because I'm a bit of a newbie I'm not sure what the guy is talking about in the last post which has the solution. http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=671814 Where do I move the insert? Is there a general event handler other than just the control event handlers? thnx for your help Quote
Administrators PlausiblyDamp Posted December 8, 2004 Administrators Posted December 8, 2004 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
SonicBoomAu Posted December 8, 2004 Posted December 8, 2004 Why not convert the text into a integer. I.E. Dim intNumber As Integer intNumber = System.Convert.ToInt32(txtSurveyNum.Text) Hope this helps Quote 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
Jaguar3311 Posted December 8, 2004 Author Posted December 8, 2004 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.