如何根据id在数据库中插入数据,但在组合框上显示名称? [英] How do i insert data on database based on id but showing the name on combobox?

查看:81
本文介绍了如何根据id在数据库中插入数据,但在组合框上显示名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在很困惑,我想不出办法解决这个问题...



以此为例:



我有2列,一个有ID_empresa,另一个有Name_empresa





我想要我的组合框显示name_empresa并在另一个表上插入id_empresa,其中包含id_empresa的列。



我会给予任何帮助,这让我痛苦不堪屁股! :/



我的代码:

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:

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







编辑:



我做了一些研究,解决了1个问题,又来了另一个,这是错误的打印屏幕......

[ ^ ]





我的Sql查询:








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





我的尝试:



我试过在互联网上找到一个例子,但现在它说id_veiculo(这是车牌没有默认值...



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...

推荐答案

您是否阅读了错误消息?您尝试插入的行中的列数多于您的行。



您的插入指定了10列,但之后您只提供了9个值。
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.


这篇关于如何根据id在数据库中插入数据,但在组合框上显示名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆