I've trying to do some kind of mysql command that could check the value from a datagridview selected row and then update it if it was equal to 1. So far i got this piece of code, but i can't figure it out how to setup the sql sintax correctly.
namespace WindowsFormsApplication1
{
public partial class Emp_gerir_val : Form
{
public Emp_gerir_val()
{
InitializeComponent();
}
private void chkVerArq_CheckedChanged(object sender, EventArgs e)
{
MySqlDataAdapter sda = new MySqlDataAdapter();
MySqlConnection Con = new MySqlConnection("server=localhost;user id=root; password = 12345; persistsecurityinfo=True;database=portaria");
DataTable dt = new DataTable();
if (chkVerArq.Checked == true)
{
MySqlCommand Command = new MySqlCommand("SELECT nome_empresa, arquivo from empresas WHERE arquivo= 1;", Con);
sda.SelectCommand = Command;
sda.Fill(dt);
DgvGerirEmp.DataSource = dt;
GerirEmpArq.Hide();
GerirEmpDesArq.Show();
}
else
{
MySqlCommand Command = new MySqlCommand("SELECT nome_empresa, arquivo from empresas WHERE arquivo= 0;", Con);
sda.SelectCommand = Command;
sda.Fill(dt);
DgvGerirEmp.DataSource = dt;
GerirEmpArq.Show();
GerirEmpDesArq.Hide();
}
}
public void Emp_gerir_val_Load(object sender, EventArgs e)
{
MySqlDataAdapter sda = new MySqlDataAdapter();
MySqlConnection Con = new MySqlConnection("server=localhost;user id=root; password = 12345; persistsecurityinfo=True;database=portaria");
MySqlCommand Command = new MySqlCommand("SELECT nome_empresa, arquivo from empresas WHERE arquivo= 1;", Con);
DataTable dt = new DataTable();
sda.SelectCommand = Command;
sda.Fill(dt);
GerirEmpArq.Show();
try
{
Con.Open();
DataView dv = new DataView(dt);
DgvGerirEmp.DataSource = dv.Table;
DgvGerirEmp.AutoGenerateColumns = true;
Con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GerirEmpArq_Click(object sender, EventArgs e)
{
MySqlDataAdapter sda = new MySqlDataAdapter();
MySqlConnection Con = new MySqlConnection("server=localhost;user id=root; password = 12345; persistsecurityinfo=True;database=portaria");
DataTable dt = new DataTable();
MySqlCommand Command = new MySqlCommand("Update empresas set arquivo = 1 where nome_empresa = '" + this.NomeEmpTxt.Text + "' ;", Con);
}
private void DgvGerirEmp_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.DgvGerirEmp.Rows[e.RowIndex];
NomeEmpTxt.Text = row.Cells[0].Value.ToString();
}
}
private void GerirEmpDesArq_Click(object sender, EventArgs e)
{
}
}
}
Table:
+---------------+--------------+----------+
| nome_empresa | id_empresa | arquivo |
|Test Company 0 | 1 | 0 |
|Test Company 1 | 2 | 1 |
|Test Company 2 | 3 | 1 |
|Test Company 3 | 4 | 0 |
|Test Company 4 | 5 | 0 |
As you can see i got on the column Arquivo some ids that got 0 and other got 1, that means "is the id archived?" 1 for yes and 0 for no. With that in mind i want some kind of help on this stuff. For example, i got this row selected(the one underlined) and i want to change the "arquivo value which is 0 to 1, but how can i do it on a UPDATE sintax? That's my question.