String to integer Conversion
-
Hai to all, Till now i have used the function for converting the string values to integer as Integer.parse(txtnumber.text) and also used Int32.parse(txtnumber.text) And now it is giving an error like "Input string was not in a correct Format". What might be the reason for it is working for some forms and not for others.CAn anyone tell me in what cases it will give this type of error??
kissy
-
Hai to all, Till now i have used the function for converting the string values to integer as Integer.parse(txtnumber.text) and also used Int32.parse(txtnumber.text) And now it is giving an error like "Input string was not in a correct Format". What might be the reason for it is working for some forms and not for others.CAn anyone tell me in what cases it will give this type of error??
kissy
it gives the error only when txtnumber.text contains characters other than digit. before converting string value to integer u need to validate the input string.... by say regular exp... Regex reNo = new Regex(@"^[0-9]{*}$"); if (reNo.Match(txtnumber.text).Success == false) { //display err. msg. "invalid data" } else { //Convert to integer Int32.parse(txtnumber.text) } ///this way it will not give run time err.
-
Hai to all, Till now i have used the function for converting the string values to integer as Integer.parse(txtnumber.text) and also used Int32.parse(txtnumber.text) And now it is giving an error like "Input string was not in a correct Format". What might be the reason for it is working for some forms and not for others.CAn anyone tell me in what cases it will give this type of error??
kissy
if the text of your textbox(txtnumber) has an empty string it would give the above error, because becomes unable to parse it into integer.
-
it gives the error only when txtnumber.text contains characters other than digit. before converting string value to integer u need to validate the input string.... by say regular exp... Regex reNo = new Regex(@"^[0-9]{*}$"); if (reNo.Match(txtnumber.text).Success == false) { //display err. msg. "invalid data" } else { //Convert to integer Int32.parse(txtnumber.text) } ///this way it will not give run time err.
DEar Vaibhav, Thanks for ur reply,Ur suggestion is good,but even when i am entering digits then only it is giving error. I have more than 10 textboxes for entering digits into textboxes and need to convert them into integer values for the database in my webform. But it is not possible for giving RegExp for all these textboxes in a single form??Isn't it?? I am using VB in my aps.net. What to do??? Actully here i have kept all the textboxes in a table,Is it makes any difference???If the textbox is not in the table(in open) then it is not giveng any error??? -- modified at 2:12 Monday 26th March, 2007
kissy
-
DEar Vaibhav, Thanks for ur reply,Ur suggestion is good,but even when i am entering digits then only it is giving error. I have more than 10 textboxes for entering digits into textboxes and need to convert them into integer values for the database in my webform. But it is not possible for giving RegExp for all these textboxes in a single form??Isn't it?? I am using VB in my aps.net. What to do??? Actully here i have kept all the textboxes in a table,Is it makes any difference???If the textbox is not in the table(in open) then it is not giveng any error??? -- modified at 2:12 Monday 26th March, 2007
kissy
Then in this case u can use Regular exp. validators for each textbox.. and also pls update the reg. exp. to "^[0-9]*$".. and about placing things in table would not make any difference...
-
if the text of your textbox(txtnumber) has an empty string it would give the above error, because becomes unable to parse it into integer.
-
Hai, Thaks for ur reply.But my doubt is Even i am entering some value into thetextbox it is giving blank when i am parsing the data. How it is happening.Means it is nothin is coming into the textbox while conversion. Can u give me some idea.
kissy
can u pls show me ur code... so that i can help u out.
-
can u pls show me ur code... so that i can help u out.
Hai, Try Dim str As String = txtseedno.Text Dim dtrs As Integer = Convert.ToInt32(str) Response.Write(dtrs) Response.End() Catch ex As Exception Response.Write(ex.Message) End Try Also i have tried with integer.parse(str) But it is giving error because it is having blank value.I have taken this textbox in a table. and simply i am showin on button click event
kissy
-
Hai, Try Dim str As String = txtseedno.Text Dim dtrs As Integer = Convert.ToInt32(str) Response.Write(dtrs) Response.End() Catch ex As Exception Response.Write(ex.Message) End Try Also i have tried with integer.parse(str) But it is giving error because it is having blank value.I have taken this textbox in a table. and simply i am showin on button click event
kissy
hey pls send me ur email id ...so that i can send u sample project..which works..k
-
Hai, Try Dim str As String = txtseedno.Text Dim dtrs As Integer = Convert.ToInt32(str) Response.Write(dtrs) Response.End() Catch ex As Exception Response.Write(ex.Message) End Try Also i have tried with integer.parse(str) But it is giving error because it is having blank value.I have taken this textbox in a table. and simply i am showin on button click event
kissy
Try this, Dim str As String=txtseedno.Text Dim dtrs As Integer=0 If str.Length > 0 Then dtrs=Integer.Parse(str) End If response.write(dtrs) but, your txtseedno (textbox) should be validate thorough regular expression validator.