Object reference not set to an instance of an object
bonjour,
soit la classe suivante :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| class connectionsql
{
SqlConnection con;
public SqlDataReader rdr ;
SqlCommand cmd;
public string req;
string strcon = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
public connectionsql()
{
con=new SqlConnection(strcon);
con.Open();
cmd = new SqlCommand();
}
public void CreateReq()
{
cmd.CommandText = req;
cmd.Connection = con;
}
public void AddParametre(string parametre, SqlDbType sqlDbType, object value)
{
cmd.Parameters.Add(parametre, sqlDbType);
cmd.Parameters[parametre].Value = value;
}
public void ExecuteNonQuery()
{
cmd.ExecuteNonQuery();
}
public void ExecuteReader()
{
rdr=cmd.ExecuteReader();
}
public SqlCommand returnCmd()
{
return cmd;
}
public void Deconnection()
{
cmd.Dispose();
rdr.Dispose();
con.Close();
con.Dispose();
}
//strcon contient la chaîne de connection
} |
et bout de code suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
private void button1_Click(object sender, EventArgs e)
{try
{
connectionsql insert = new connectionsql();
req = "Insert into tab_montant_bp (Code_A,Code_SC,montant,Exercice_BP,etat) values (@Code_A,@Code_SC,@montant,@Exercice_BP,@etat)";
insert.req = req;
insert.CreateReq();
insert.AddParametre("@Code_A", SqlDbType.Int, Convert.ToInt32(comboArticleBS.SelectedItem.ToString()));
insert.AddParametre("@Code_SC", SqlDbType.Int, Convert.ToInt32(comboSCBS.SelectedItem.ToString()));
insert.AddParametre("@montant", SqlDbType.Decimal, 0);
insert.AddParametre("@Exercice_BP", SqlDbType.Int, Convert.ToInt32(txtCurrentExercice.Text));
insert.AddParametre("@etat", SqlDbType.Bit, true);
insert.ExecuteNonQuery();
insert.Deconnection();
}
catch(Exception err)
{
MessageBox.show(err.Message);
]
} |
en executant ce bout de code en cliquant sur le bouton, ça me génère un exception du gene :
Citation:
Object reference not set to an instance of an object
cette exception est généré au niveau de la ligne de la classe : connectionsql.
quelqu'un saurait il d'ou peux venir le problème, et comment le résoudre ??
Merci.