locked
Convert Decimal to String???

    Question

  • 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!

    Monday, November 20, 2006 12:11 AM

Answers

  • 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();

    Monday, November 20, 2006 1:10 AM
  • 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...
    Monday, November 20, 2006 1:41 AM

All replies

  • 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

    Monday, November 20, 2006 12:44 AM
  • 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!
    Monday, November 20, 2006 12:59 AM
  • *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

    Monday, November 20, 2006 1:01 AM

  • 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"];


     

    Monday, November 20, 2006 1:08 AM
  • 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.....
    Monday, November 20, 2006 1:09 AM
  • 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();

    Monday, November 20, 2006 1:10 AM
  • 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
    Monday, November 20, 2006 1:13 AM
  • 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"]);

    Monday, November 20, 2006 1:19 AM
  • 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...
    Monday, November 20, 2006 1:41 AM
  • 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!

    Monday, November 20, 2006 1:44 AM
  • hehe! No worries ;-)
    Monday, November 20, 2006 1:58 AM