locked
Get zero value while retrieving value from Textbox RRS feed

  • Question

  • Hi,

    I have two functions below...And  while debugging i see that i do not get any value in

    int textBox8Value = getIntegerFromTextbox(textBox8);

    I get "0" in textBoxValue while debugging...but textBox8 has a value

    so thats why i get zero in tPrice1.Text

    tPrice1.Text = (textBox8Value * textBoxDiscountValue * textBoxQtyValue).ToString();




    private void getTradePrice() 
            { 
     
                string s = "SELECT DISTINCT Products.FactorC FROM Packing INNER JOIN Products ON Packing.PackingID=Products.Packing WHERE Products.ProductName='" + comboBox3.Text + "' And Packing.PackingName='" + comboBox1.Text + "'"; 
                database = new OleDbConnection(connectionString); 
                database.Open(); 
                 
                OleDbCommand cmd = new OleDbCommand(s, database); 
                cmd.CommandText = s
                int value = (int)cmd.ExecuteScalar(); 
                RetrievePrice(); 
                int textBox8Value = getIntegerFromTextbox(textBox8); 
                int textBoxQtyValue = getIntegerFromTextbox(SplyQty); 
                int textBoxDiscountValue = getIntegerFromTextbox(disc); 
                textBoxDiscountValue=(((textBox8Value*value)/100)*textBoxDiscountValue)-textBox8Value; 
     
                if (SplyType.Text == "C" && SplyQty.Text!=null) 
                { 
                    tPrice1.Text = (value * textBox8Value * textBoxDiscountValue * textBoxQtyValue).ToString(); 
     
                } 
                else if (SplyType.Text == "" && SplyQty.Text!=null) 
                { 
                    tPrice1.Text = (textBox8Value * textBoxDiscountValue * textBoxQtyValue).ToString(); 
                } 
     
     
    private void RetrievePrice() 
            { 
                string sql3 = "SELECT DISTINCT Products.CPrice FROM Packing INNER JOIN Products ON Packing.PackingID=Products.Packing WHERE Products.ProductName='" + comboBox3.Text + "' And Packing.PackingName='" + comboBox1.Text + "'"; 
     
                database = new OleDbConnection(connectionString); 
                OleDbCommand cmd = new OleDbCommand(sql3, database); 
                cmd.CommandText = sql3
                database.Open(); 
     
                Object ReturnValue = cmd.ExecuteScalar(); 
                if (ReturnValue != DBNull.Value) 
                { 
     
                    textBox8.Text = cmd.ExecuteScalar().ToString(); 
                } 
                else 
                { textBox8.Text = ""; } 
                //database.Close(); 
     
            } 


    please help

    Haroon
    Sunday, March 8, 2009 10:54 PM

Answers

  • Try:

            private int getIntegerFromTextbox(TextBox tb)  
            {  
                decimal tempValue = 0;  
                decimal.TryParse(tb.Text, out tempValue);  
                return (int)decimal.Round(tempValue);  
            }  
     

    This will round up or down as necessary. (In a VB6 way... 1.5 => 2 and 2.5 => 2). Or you can just "return (int)tempValue;" to just trim the digits after the decimal.
    • Marked as answer by shah123 Monday, March 9, 2009 12:43 PM
    Monday, March 9, 2009 12:29 PM
  • Hi,

        You got this error because you first call your own built-up method i.e. getIntegerFromTextBox();

    Don't use this. As this method tries to save the convert the decimal value to integer and then the assigned datatype(double), where compiler tries to cast it according to it. Better use only one like like this:

    double textBox8Value = Convert.ToDouble(textBox8.Text);

    Now you won't get any compiler error


    Regards

    Midnight Programmer
    • Marked as answer by shah123 Monday, March 9, 2009 12:43 PM
    Monday, March 9, 2009 12:33 PM

All replies

  • Hi shah123.

    This looks like a problem with getIntegerFromTextbox() or with the datatypes you are using. Can you post the code for getIntegerFromTextbox()?

    What is the value in textBox8?

    There's also some duplicate code in RetrievePrice() that doesn't need to be there. "sql3" is given to the command twice. You don't need to call "cmd.CommandText = sql3" as this has already been performed in the constructor. ExecuteScalar() is also called twice, and will run the query twice. Much better just to cast the result you've already got:

    Object ReturnValue = cmd.ExecuteScalar();  
    if (ReturnValue != DBNull.Value)  
    {  
        textBox8.Text = ReturnValue.ToString();  
    }  
    else  
    {  
        textBox8.Text = "";  
    Monday, March 9, 2009 9:41 AM
  • Hi Shah123,

        I don't know what code you are using in the below method:

                    int textBox8Value = getIntegerFromTextbox(textBox8);

    The getIntegerFromTextbox() method.....

    Try using this way:

        int textBox8Value = Convert.ToInt32(textBox8.Text);

    I hope this solves the prob...

    Regards,

    Midnight Programmer
    Monday, March 9, 2009 10:09 AM
  • Hi,
     
    int textBox8Value = Convert.ToInt32(textBox8.Text);

    gives error "Input String is not in correct format...

     

    The value in textBox8.text is ="20.40" while debugging..


    And below is the getInteger function...

    int getIntegerFromTextbox(TextBox tb)

    {

    int tempValue;

    int.TryParse(tb.Text, out tempValue);

    return tempValue;

    }

     


    Haroon
    Monday, March 9, 2009 12:09 PM
  • ' 20.40' is float value


    Try this now

    double TempValue = Convert.ToDouble(tbox8.Text);


    regards,
    Midnight Programmer
    Monday, March 9, 2009 12:16 PM
  • What you are trying to do is storing a decimal value to a datatype which accepts only int value or numbers without decimal values.

    Float Datatype:- Use when you ONLY want to save or assign decimal value.

    Double Datatype:- Use when you are un-predictable of what kind of value you will get. This datatype can store values of both integer and float type.


    regards,
    Midnight Programmer
    Monday, March 9, 2009 12:18 PM
  • Hi,

    I get following error on line:

    double TempValue = Convert.ToDouble(tbox8.Text); // error

    "Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'."

    regards,

    private void getTradePrice() 
            { 
     
                string s = "SELECT DISTINCT Products.FactorC FROM Packing INNER JOIN Products ON Packing.PackingID=Products.Packing WHERE Products.ProductName='" + comboBox3.Text + "' And Packing.PackingName='" + comboBox1.Text + "'"; 
                database = new OleDbConnection(connectionString); 
                database.Open(); 
     
                OleDbCommand cmd = new OleDbCommand(s, database); 
                cmd.CommandText = s
                int value = (int)cmd.ExecuteScalar(); 
                RetrievePrice(); // retrieve price of textBox8
                 
                double textBox8Value = getIntegerFromTextbox(textBox8); 
                textBox8Value = Convert.ToDouble(textBox8);  //error here
                 
                double textBoxQtyValue = getIntegerFromTextbox(SplyQty); 
                textBoxQtyValue = Convert.ToDouble(SplyQty); 
     
                double textBoxDiscountValue = getIntegerFromTextbox(disc); 
                textBoxDiscountValue = Convert.ToDouble(disc); 
     
                textBoxDiscountValue = (((textBox8Value * value) / 100) * textBoxDiscountValue) - textBox8Value; 
     
                if (SplyType.Text == "C" && SplyQty.Text != null) 
                { 
                    tPrice1.Text = (value * textBox8Value * textBoxDiscountValue * textBoxQtyValue).ToString(); 
     
                } 
                else if (SplyType.Text == "" && SplyQty.Text != null) 
                { 
                    tPrice1.Text = (textBox8Value * textBoxDiscountValue * textBoxQtyValue).ToString(); 
                } 
                 
            } 
            int getIntegerFromTextbox(TextBox tb) 
            { 
                int tempValue; 
                int.TryParse(tb.Text, out tempValue); 
                return tempValue; 
            } 




    Haroon
    Monday, March 9, 2009 12:24 PM
  • Try:

            private int getIntegerFromTextbox(TextBox tb)  
            {  
                decimal tempValue = 0;  
                decimal.TryParse(tb.Text, out tempValue);  
                return (int)decimal.Round(tempValue);  
            }  
     

    This will round up or down as necessary. (In a VB6 way... 1.5 => 2 and 2.5 => 2). Or you can just "return (int)tempValue;" to just trim the digits after the decimal.
    • Marked as answer by shah123 Monday, March 9, 2009 12:43 PM
    Monday, March 9, 2009 12:29 PM
  • Hi,

        You got this error because you first call your own built-up method i.e. getIntegerFromTextBox();

    Don't use this. As this method tries to save the convert the decimal value to integer and then the assigned datatype(double), where compiler tries to cast it according to it. Better use only one like like this:

    double textBox8Value = Convert.ToDouble(textBox8.Text);

    Now you won't get any compiler error


    Regards

    Midnight Programmer
    • Marked as answer by shah123 Monday, March 9, 2009 12:43 PM
    Monday, March 9, 2009 12:33 PM
  • It worked...Thanks a lot....I really appreciate...
    Haroon
    • Proposed as answer by Sadeqzadeh Saturday, September 11, 2010 5:58 PM
    Monday, March 9, 2009 12:43 PM