Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have a c# winforms app with makes some calculations based on a formula.
the formula runs OK but depending on the value of an sql table the formula can change. I was thinking of appying `IF´s`, but if i do i click the button and the formula does nothing.
I removed a large quantity of code to facilitade viewing.

What I have tried:

C#
private void button1_Click(object sender, EventArgs e) //formula
                {
                    decimal ValueFromDB;
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand("select prumos from dbo.modelos where id = '" + prumos + "'", con))
                    {
                        cmd.Parameters.AddWithValue("@ID", prumos);
                        ValueFromDB = decimal.Parse(cmd.ExecuteScalar().ToString());
                        con.Close();
                    }
                    if (larg.Text == string.Empty)
                    {
                        MessageBox.Show("Missing Value", "Warning");
                        return;
    //SEVERAL OTHER IF´s to prevent empty textboxes
                    }        
            else if (prumos == "1")
            {
                return;
            }    
                    decimal uw;                    
                    decimal ata;                 
                    decimal vi1;
    ...
                    decimal wmk4;
                    decimal wmk5;
                    decimal wmk6;
                    
        
                    vi1 = decimal.Parse(textBox1.Text);
                    wmk3 = decimal.Parse(textBox12.Text);
                    wmk4 = decimal.Parse(textBox22.Text);
    ...
                    wmk5 = decimal.Parse(textBox17.Text);
                    wmk6 = decimal.Parse(textBox15.Text);
    ...                     
                    uw = (adp * ufa + adv * ug + perv * wmk) / ac;
                    answ.Text = (Math.Truncate(uw * 100) / 100).ToString();
     {
            else if (prumos == "2")
            {
                return;
            }


any ideas why the formula wont load after i apply this rule?

Thanks in advance.
Posted
Updated 18-Nov-16 1:40am

Well...you don't change the value of "prumos" in that code - or even show where it's set originally.
You do read a value from SQL, but you store that in ValueFromDB which you never reference again. I'd suspect that you really need to look at that instead as part of your if rather than promus

And don't do SQL like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
Marc-IT 18-Nov-16 7:32am    
Thanks for the reply.
Can you make an example of how would you select the value.
You are right, ValueFromDB is the value i need, i posted an older version of the code. i tried else if (prumos == "ValueFromDB")but it still wont work
OriginalGriff 18-Nov-16 7:46am    
No, ValueFromDb contains the value you read from the database - promus tells the DB which specific row contains the value to return.
Once it's there, it will hold a decimal number (because you declared it as a decimal variable). So you can compare it with other decimal values:
if (ValueFromDB == 1)
{
...
}
else if (ValueFromDB == 2)
{
...
}
C#
 if (larg.Text == string.Empty)
  {
    MessageBox.Show("Missing Value", "Warning");
    return;
    //SEVERAL OTHER IF´s to prevent empty textboxes
  } 
// In both cases it will return  
else if (prumos.Equals("1") || prumos.Equals("2"))
 {
   return;
 }
// Put your calculation in last Condition 
else
{
				
decimal uw;                    
decimal ata;                 
decimal vi1;
decimal wmk4;
decimal wmk5;
decimal wmk6;
vi1 = decimal.Parse(textBox1.Text);
wmk3 = decimal.Parse(textBox12.Text);
wmk4 = decimal.Parse(textBox22.Text);
wmk5 = decimal.Parse(textBox17.Text);
wmk6 = decimal.Parse(textBox15.Text);
uw = (adp * ufa + adv * ug + perv * wmk) / ac;
answ.Text = (Math.Truncate(uw * 100) / 100).ToString();
}		
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900