Vaibhav Jain
Vaibhav Jain

Reputation: 34407

Need help in typecasting string in to int

I am using Convert.ToInt32(ddlBuyer1Country.SelectedValue); to typecast a string returned by selectedvalue. But this is giving me 0 instead of 3. I have selected value as 3 in this case.

Upvotes: 0

Views: 98

Answers (2)

derek
derek

Reputation: 4886

Try: Int.TryParse(ddlBuyer1Country.SelectedValue, out myInt);

But double check to make sure that you are not re-binding the dropdown on postback, that could reset your selected value to 0. Make sure the dropdown bind only occurs in a:

if(!Page.IsPostBack)

Upvotes: 2

David Basarab
David Basarab

Reputation: 73301

Convert will return a 0, if the value is null.

Are you sure SelectedValue is not null before calling Convert?

How are you using the ddlBuyer1Country?

Upvotes: 0

Related Questions