In addition to the post from @OriginalGriff above consider also the following ...
Use a switch statement rather than that long line of
if-else
and avoid repeating lines that are used in all cases. E.g.
public void EmpresasCondition(MySqlCommand cmd)
{
ParEmpresas(cmd);
cmd.Parameters.AddWithValue("@idempresa", comboBox1.SelectedIndex);
switch (comboBox1.SelectedIndex)
{
case 0:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Dura");
break;
case 1:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Caetano Coatings");
break;
case 2:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Lusilectra");
break;
case 3:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Jac");
break;
case 4:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Toyota Caetano");
break;
case 5:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Formação");
break;
case 6:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Visitantes");
break;
case 7:
cmd.Parameters.AddWithValue("@empvisitar", Text = "Parinama");
break;
}
}
Note - you will need something to handle SelectedIndex = -1 (nothing selected) and any possibility of an Index > 7 being selected.
Another approach would be to have all of that text in an collection which you can reference using the SelectedIndex
var textValues = new []
{
"Dura",
"Caetano Coatings",
"Lusilectra",
"Jac",
"Toyota Caetano",
"Formação",
"Visitantes",
"Parinama"
};
cmd.Parameters.AddWithValue("@empvisitar", textValues[comboBox1.SelectedIndex]);
Or, as I suspect, if that text is actually what is being displayed in the ComboBox then
cmd.Parameters.AddWithValue("@empvisitar", comboBox1.SelectedValue);
would probably do. Or you might want to do some research on the differences between DisplayMember and ValueMember of a ComboBox.
Some other points:
In the example above I have demonstrated how you should be passing
cmd
down to your subroutines
Don't use
chkhoraentrada.Checked == true
chkmatpers.Checked == false
These are Boolean values so the true / false is not needed. Use
chkhoraentrada.Checked
!chkmatpers.Checked
Finally, be aware that even if you fix the problem with
cmd
there will be routes through your code that do not assign values to the parameters
@matricula
or
@data