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)
{
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();
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)
{
MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
}
}
}
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)
{
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)
{
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...