Problème de connexion de base de données avec mon interface

sanaeMaroc Messages postés 1 Date d'inscription lundi 2 janvier 2017 Statut Membre Dernière intervention 2 janvier 2017 - Modifié par jordane45 le 2/01/2017 à 01:49
cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 - 14 janv. 2017 à 18:28
Bonjour,
S'ils vous plait pouvez-vous m'aider de résoudre ce problème qui m'a cassé la tête cet aprés-midi l'erreu s'affiche comme suit:une exception non gérée du type 'System.Data.SqlClient.SqlException' s'est produite dans Sysytem.Data..dll.
Mon code est le suivant :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace yarab
{
    public partial class Form1 : Form
    {

// declaratin des connexion 
        SqlConnection con = new SqlConnection(@"Data Source=SANAE;Initial Catalog=test;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;





public Form1()
        {
            InitializeComponent();
        }

private void Form1_Load(object sender, EventArgs e)
        {

}

private void button1_Click(object sender, EventArgs e)
        {
            con.Open();

cmd = new SqlCommand("insert into cl values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            dr.Close();
          MessageBox.Show("insertion valide");
        }
    }
}

EDIT : Ajout des balises de code

Merci d'avance

1 réponse

cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 33
14 janv. 2017 à 18:28
Bonsoir
je suppose que l'erreur se produit dans "cmd.ExecuteNonQuery();"

peux tu afficher ta requête ?
en changeant le code:
string query = "insert into cl values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')";
try
{
  con.Open();
  try
  { 
    cmd = new SqlCommand( query, con);
  }
  catch ()
  {
    MessageBox.Show (query);
  }
  MessageBox.Show("insertion valide");
}
catch (Exception ex)
{
  MessageBox.Show (ex.ToString());
}
finaly
{
 con.Close();
}

note que ton instruction "dr.Close();" va également produire une exception vu que "dr" n'est pas initialisé.

0
Rejoignez-nous