Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm so confuded right now that i can't think a way to solve this thing...

Take this for example:

I have 2 columns, one with ID_empresa and one with Name_empresa


I want my combobox to show the name_empresa and insert id_empresa on another table, that has the column for id_empresa.

I would apreciate any help, this is giving me a pain in the ass! :/

My code:
C#
namespace Portaria
{
    public partial class DarEntrada : Form
    {
        MySqlConnection con = new MySqlConnection(@"server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True");

        MySqlCommand cmd = new MySqlCommand();
        MySqlDataReader dr;
        public DarEntrada()
        {
            InitializeComponent();
        }
        
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void DarEntrada_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'portariaDataSet.empresas' table. You can move, or remove it, as needed.
            this.empresasTableAdapter.Fill(this.portariaDataSet.empresas);

            dateTimePicker1.Enabled = false;
            txtmatpers.Visible = false;
            txtsector.Enabled = false;
            button1.Click += new EventHandler(button1_Click);
            FillDropDownList();
        }

        public void FillDropDownList()
        {
            string SQL = "SELECT id_empresa, nome_empresa FROM empresas ORDER BY nome_empresa";

            DataTable dt = new DataTable();

            // Set the connection string in the Solutions Explorer/Properties/Settings object (double-click)
            using (var cn = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
            {
                using (var cmd = new MySqlCommand(SQL, cn))
                {
                    cn.Open();

                    try
                    {
                        dt.Load(cmd.ExecuteReader());
                    }
                    catch (MySqlException e)
                    {
                        // Do some logging or something. 
                        MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
                    }
                }
            }

            // UPDATED - The .ValueMember and .DisplayMember properties 
            // refer to the string name of the field (oops!):
            comboBox1.DataSource = dt;
            comboBox1.ValueMember = "id_empresa";
            comboBox1.DisplayMember = "nome_empresa";
        }

        public void SaveComboBoxContent()
        {
            string SQL = "INSERT INTO entradas (id_empresa) VALUES (@id_empresa)";

            using (var cn = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
            {
                using (var cmd = new MySqlCommand(SQL, cn))
                {
                    cmd.Parameters.AddWithValue("@id_empresa", comboBox1.SelectedValue);
                    cn.Open();

                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MySqlException e)
                    {
                        // Do some logging or something. 
                        MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
                    }
                }
            }
        }
        private void chkmatpers_CheckedChanged(object sender, EventArgs e)
        {
            
            
            try
            {
                if (chkmatpers.Checked == true)
                {
                    msktxtmat.Mask = null;   
                }else
                {
                    msktxtmat.Mask = "00-00-AA"; 
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void chkvariasemp_CheckedChanged(object sender, EventArgs e)
        {

            try
            {
                if (chkvariasemp.Checked == true)
                {
                    
                }
                else
                {
                    
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void chksector_CheckedChanged(object sender, EventArgs e)
        {
            txtsector.Enabled = false;
            try
            {
                if(chksector.Checked == true)
                {
                    txtsector.Enabled = true;
                }
                else
                {
                    txtsector.Enabled = false;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void chkhoraentrada_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = false;
            try
            {
                if(chkhoraentrada.Checked == true)
                {
                    dateTimePicker1.Enabled = true;
                }
                else
                {
                    dateTimePicker1.Enabled = false;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveComboBoxContent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



EDIT:

I have done some research and with 1 problem solved, comes another one, here is a printscreen of the error...
[^]


My Sql Query:

string SQL = "INSERT INTO entradas (id_veiculo,id_empresa,nome_condutor,empresa_visitante,empresa_visitar,visitado,ncartao,data,hora,obs) VALUES (@msktxtmat,@txtempvis,@txtnomecondutor,@txtempvisitar,@txtpessoavisitar,@txtncartao,@data,@hora,@txtobs)";

            using (var cn = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
            {
                using (var cmd = new MySqlCommand(SQL, cn))
                {
                    cmd.Parameters.AddWithValue("@msktxtmat", msktxtmat.Text);
                    cmd.Parameters.AddWithValue("@txtnomecondutor", txtnomecondutor.Text);
                    cmd.Parameters.AddWithValue("@txtempvis",txtempvis.Text);
                    cmd.Parameters.AddWithValue("@txtobs", txtobs.Text);
                    cmd.Parameters.AddWithValue("@txtncartao",txtncartao.Text);
                    cmd.Parameters.AddWithValue("@txtpessoavisitar", txtpessoavisitar.Text);
                    cmd.Parameters.AddWithValue("@empvisitar", comboBox1.SelectedValue);
                    cmd.Parameters.AddWithValue("@data", DateTime.Now.ToString("yyyy-MM-dd"));
                    cmd.Parameters.AddWithValue("@hora", DateTime.Now.ToString("hh:mm:ss"));
                    cn.Open();

                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MySqlException e)
                    {
                        // Do some logging or something. 
                        MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
                    }
                }


What I have tried:

I've tryied an example found on the internet, but now it says that the id_veiculo(which is the license plate doesn't have a default value...
Posted
Updated 29-Feb-16 7:34am
v3
Comments
[no name] 29-Feb-16 13:04pm    
Make sure that you have value in msktxtmat.Text for parameter @msktxtmat
[no name] 1-Mar-16 0:59am    
Your query contains only 9 values where as it should have 10 values to be inserted. Please change your query and try again.
Scribling Doodle 1-Mar-16 4:07am    
yeah, figured it out, sometimes we dont see that the error is in front of out eyes :p

1 solution

Did you read the error message? There are more columns than in your row that you are trying to insert.

Your insert specified 10 columns but then you only provide 9 values.
 
Share this answer
 
Comments
Scribling Doodle 1-Mar-16 3:59am    
Yeah, i've figured it out, but now it comes with another type of mysql error, not the same since this one is fixed, but now it shows me that "column count doesn't match value count at row 1"
ZurdoDev 1-Mar-16 7:20am    
Yes, I just answered that one.

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