C# -- convert STRING to DECIMAL

finance_teacher
finance_teacher used Ask the Experts™
on
The below "romid = (decimal)" fails
with a cannot convert STRING to DECIMAL.

How can I solve ?
------------------------------------------------------------------------------------
        private void fillBy_HistoryToolStripButton_Click_1(object sender, EventArgs e)
        {
            try
            {
                Nullable<Decimal> id = null;
                Nullable<Decimal> romid = null;

                if (!string.IsNullOrEmpty(vIMIDToolStripTextBox.Text))
                {
                    try
                    {
                        DataRow[] nDR1 = this.gmL_ITEMMSTRTableAdapter1.GetDataBy_vStockNumber(vIMIDToolStripTextBox.Text).Select();
                        id = (decimal)nDR1[0]["ID"];

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Invalid Stock Number");
                        return;
                    }
                }

                if (!string.IsNullOrEmpty(this.vROMIDToolStripTextBox.Text))
                {
                    try
                    {
                        romid = (decimal)this.vROMIDToolStripTextBox.Text;

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ID must be Numberic");
                        return;
                    }
                }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Try:
try
{
    if (!decimal.TryParse(this.vROMIDToolStripTextBox.Text, out romid))
    {
        MessageBox.Show("ID must be Numberic");
        return;
    }

}
catch (Exception ex)
{
    MessageBox.Show("ID must be Numberic");
    return;
}

Open in new window


// change this line:
romid = (decimal)this.vROMIDToolStripTextBox.Text;
// to:
romid = Decimal.Parse(this.vROMIDToolStripTextBox.Text);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial