Convert Decimal to String???

this.txtCommission.Text = (decimal)_SalesPersonToEdit["Default Commission"];

How do i convert this string to a Decimal, I have tried using ToString() but that does not work and I also tried to use Convert.ToDecimal(string) = object....and that didnt work eithier any help on this situation! thanks!




Answer this question

Convert Decimal to String???

  • Strak


    private void LoadSalesRowObjectToForm(System.Data.DataRow SalesPersonToEdit)
    {
    try

    {
    _SalesPersonToEdit = SalesPersonToEdit;
    int i;
    for(i=2; i<_SalesPersonToEdit.Table.Columns.Count; i++)
    {
    if( i < _SalesPersonToEdit.Table.Columns.Count-1)
    {
    if(_SalesPersonToEdit.IsNull(i))
    _SalesPersonToEditIdea = "";
    }
    else

    {
    if(_SalesPersonToEdit.IsNull(i))
    _SalesPersonToEditIdea =
    false;
    }
    }
    this.cboTitle.Text = (string)_SalesPersonToEdit["Title"];
    this.txtCity.Text = (string)_SalesPersonToEdit["City"];
    this.txtFirstName.Text = (string)_SalesPersonToEdit["First Name"];
    this.txtLastName.Text = (string)_SalesPersonToEdit["Last Name"];
    this.cboState.Text = (string)_SalesPersonToEdit["State"];
    this.txtTelephone.Text = (string)_SalesPersonToEdit["CoPhone"];
    this.txtPagerNumber.Text = (string)_SalesPersonToEdit["Pager"];
    this.txtMobileNumber.Text = (string)_SalesPersonToEdit["Mobile Phone"];
    this.txtEmail.Text = (string)_SalesPersonToEdit["E-Mail"];
    this.txtExtension.Text = (string)_SalesPersonToEdit["CoExtension"];
    this.txtFaxNumber.Text = (string)_SalesPersonToEdit["Fax"];
    //this.txtCommission.Text = (decimal)_SalesPersonToEdit["Default Commission"];

    this.txtNotes.Text = (string)_SalesPersonToEdit["Notes"];




  • Whoisit

    *confused*

    can you show the entire code block you are using As well as this, what do you mean by "assign that text back to my row of data in my Sql Table" you can't read and write at the same time if you are using a SqlDataReader...a SqlDataReader is only a fast forward only reader



  • dvl_vn

    right thats what i thought but when i add the ToString() at the end like you have above i still get the same error.....Can not convert string to decimal

  • Mario N

    well everything there you are putting it into a textbox therefore everything needs to be done into a string which you pretty much have done. I don't see anywhere where you are needing to convert a value to a decimal. Anything that needs to be placed into a string type needs that object to be converted to string using the .ToString() at the end:

    this.txtCommission.Text = _SalesPersonToEdit["Default Commission"].ToString();



  • Xelestial

    Oh that line of code does not work...I am trying to assign that text back to my row of data in my Sql Table which is of type decimal...And i get the error can not convert string to decimal....with that line of code.... I thought that was specific enough....thanks for the help!

  • bonni

    hehe! No worries ;-)

  • thekaran

    what "doesnt work" you need to be specific :-)

    to convert a string to a decimal:

    decimal theDecimal = Convert.ToString(theString);

    however you need to be sure in some way that the value is a "qualified" decimal value in other words, integer values only generally



  • Spulit

    but the line i posted - no where does it convert to decimal. It may well be producing an error from some other place.

    you could also try:

    this.txtCommission.Text = Convert.ToString(_SalesPersonToEdit["Default Commission"]);



  • Ultrawhack

    I dont know why the light bulbs are showing but anyway i have a dataset and loop through the row and assign the values back to my form but....but i just need to know how i can convert that string to a decimal.....

  • Olley

    Actually i didnt need "(decimal)" in my code and it worked i guess when using that row of a datset i dont need to declare its type to bind it to the form.. but i did add the ToString() method though it looked like this

    this.txtCommission.Text = _SalesPersonToEdit["Default Commission"].ToString(); thanks 4 the help!



  • Bill Henning

    hmmm that would be strange...cause when I comment out that line the error goes away which leads me to think that it is this line of code...but i am going to try the above statement...

  • Convert Decimal to String???