Bonsoir,
j'ai un formulaire qui contient 2 listes Box et 2 TextBox,je doit afficher dans ce formulaire des données à partir d'une base de données,voici mon code,mais si vous pouvez l'améliorer car je pense que je répète toujours les mêmes instructions:
public Form3(Form10 f10)
{
// TODO: Complete member initialization
InitializeComponent();
this.form10 = f10;
if (form10.fichierDataGridView1.RowCount > 0) // controle si le dataGrid n'est pas vide
{
sel1 = (string)form10.fichierDataGridView1.CurrentRow.Cells[0].Value;
sel2 = (string)form10.fichierDataGridView1.CurrentRow.Cells[1].Value;
textBox1.Text = sel1;
textBox2.Text = sel2;
SqlConnection connection1 = new SqlConnection(connectionString);
SqlConnection connection2 = new SqlConnection(connectionString);
SqlConnection connection3 = new SqlConnection(connectionString);
SqlConnection connection4 = new SqlConnection(connectionString);
connection1.Open();
req1 = "select capital from fichier where code='"+textBox1.Text+"';"; //numero_cpte,intitulé_cpte from compte where numero_cpte=cast(" + a + " as int); "; //OR intitulé_cpte like '%" + b + "%' OR type_cpte='" + c + "' OR index_full_text_cpte like'%" + d + "%';";
SqlCommand sql1 = new SqlCommand(req1, connection1);
SqlDataReader dre = sql1.ExecuteReader();
if (dre.Read())
{
textBox4.Text = dre.GetValue(0).ToString();
}
connection1.Close();
connection2.Open();
req2 = "select form_juri from fichier where code='" + textBox1.Text + "';";
SqlCommand sql2 = new SqlCommand(req2, connection2);
SqlDataReader dre2 = sql2.ExecuteReader();
if (dre2.Read())
{
listBox1.Text = dre2.GetValue(0).ToString();
}
connection2.Close();
connection3.Open();
req3 = "select activi from fichier where code='" + textBox1.Text + "';";
SqlCommand sql3 = new SqlCommand(req3, connection3);
SqlDataReader dre3 = sql3.ExecuteReader();
if (dre3.Read())
{
textBox3.Text = dre3.GetValue(0).ToString();
}
connection3.Close();
connection4.Open();
req4 = "select filiale from fichier where code='" + textBox1.Text + "';";
SqlCommand sql4 = new SqlCommand(req4, connection4);
SqlDataReader dre4 = sql4.ExecuteReader();
if (dre4.Read())
{
listBox2.Text = dre4.GetValue(0).ToString();
}
connection4.Close();
//int o = sql1.ExecuteNonQuery();
//MessageBox.Show(o + " succès");
textBox1.Enabled = false;
}
}
merci pour l'aide :)