Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 825 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I convert String to Int?

#1
I have a `TextBoxD1.Text` and I want to convert it to an `int` to store it in a database.

How can I do this?

Reply

#2
You need to parse the string, and you also need to ensure that it is truly in the format of an integer.

The easiest way is this:

int parsedInt = 0;
if (int.TryParse(TextBoxD1.Text, out parsedInt))
{
// Code for if the string was valid
}
else
{
// Code for if the string was invalid
}
Reply

#3
int i = Convert.ToInt32(TextBoxD1.Text);

Reply

#4
int.TryParse()

It won't throw if the text is not numeric.
Reply

#5
int myInt = int.Parse(TextBoxD1.Text)

Another way would be:

bool isConvertible = false;
int myInt = 0;

isConvertible = int.TryParse(TextBoxD1.Text, out myInt);

The difference between the two is that the first one would throw an exception if the value in your textbox can't be converted, whereas the second one would just return false.
Reply

#6
int x = 0;
int.TryParse(TextBoxD1.Text, out x);

The TryParse statement returns a boolean representing whether the parse has succeeded or not. If it succeeded, the parsed value is stored into the second parameter.

See *[Int32.TryParse Method (String, Int32)][1]* for more detailed information.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through