Click here to Skip to main content
15,891,184 members
Home / Discussions / C#
   

C#

 
GeneralRe: recording problem Pin
ago248617-Mar-20 3:24
ago248617-Mar-20 3:24 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 3:38
mveOriginalGriff17-Mar-20 3:38 
GeneralRe: recording problem Pin
ago248617-Mar-20 3:52
ago248617-Mar-20 3:52 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 4:08
mveOriginalGriff17-Mar-20 4:08 
GeneralRe: recording problem Pin
ago248617-Mar-20 4:28
ago248617-Mar-20 4:28 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 4:31
mveOriginalGriff17-Mar-20 4:31 
GeneralRe: recording problem Pin
ago248617-Mar-20 4:39
ago248617-Mar-20 4:39 
AnswerRe: recording problem Pin
ZurdoDev17-Mar-20 2:39
professionalZurdoDev17-Mar-20 2:39 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 2:52
mveOriginalGriff17-Mar-20 2:52 
GeneralRe: recording problem Pin
ZurdoDev17-Mar-20 2:59
professionalZurdoDev17-Mar-20 2:59 
GeneralRe: recording problem Pin
Luc Pattyn17-Mar-20 12:08
sitebuilderLuc Pattyn17-Mar-20 12:08 
GeneralRe: recording problem Pin
ago248617-Mar-20 22:04
ago248617-Mar-20 22:04 
GeneralRe: recording problem Pin
Luc Pattyn17-Mar-20 22:18
sitebuilderLuc Pattyn17-Mar-20 22:18 
GeneralRe: recording problem Pin
ago248617-Mar-20 23:20
ago248617-Mar-20 23:20 
AnswerRe: problem to retrieve an info in a sql request / problème pour recéper une info dans une requête sql Pin
Richard Deeming16-Mar-20 9:28
mveRichard Deeming16-Mar-20 9:28 
Generalproblem registering in the access database Pin
ago248617-Mar-20 0:58
ago248617-Mar-20 0:58 
GeneralRe: problem registering in the access database Pin
Richard Deeming17-Mar-20 1:01
mveRichard Deeming17-Mar-20 1:01 
GeneralRe: problem registering in the access database Pin
ago248617-Mar-20 1:08
ago248617-Mar-20 1:08 
GeneralRe: problem registering in the access database Pin
Richard Deeming17-Mar-20 1:10
mveRichard Deeming17-Mar-20 1:10 
GeneralRe: problem registering in the access database Pin
ago248617-Mar-20 1:17
ago248617-Mar-20 1:17 
GeneralRe: problem registering in the access database Pin
Richard Deeming17-Mar-20 1:35
mveRichard Deeming17-Mar-20 1:35 
GeneralRe: problem registering in the access database Pin
ago248617-Mar-20 2:00
ago248617-Mar-20 2:00 
GeneralRe: problem registering in the access database Pin
ago248617-Mar-20 1:18
ago248617-Mar-20 1:18 
GeneralRe: problem registering in the access database Pin
Richard Deeming17-Mar-20 2:57
mveRichard Deeming17-Mar-20 2:57 
Griff has already spotted the problem in the thread above, but in case it's not obvious, here's what your code is doing:
Quote:
C#
// using (sql_cmd = sql_con.CreateCommand())
{
    string txtQuery = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
    sql_cmd.Parameters.AddWithValue("@ref_det", TxtRefProduit.Text);
    sql_cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
    sql_cmd.Parameters.AddWithValue("@Designation", TxtDesignation.Text);
    sql_cmd.Parameters.AddWithValue("@Prix_unitaire_HT", TxtPrixUnitaire.Text);
    sql_cmd.Parameters.AddWithValue("@Prix_total_HT", total);
    sql_cmd = new OleDbCommand(txtQuery, sql_con);
    sql_cmd.ExecuteNonQuery();
    //ExecuteQuery(txtQuery);
}
  1. Creates a string variable to hold the query;
  2. Adds 5 parameters to the sql_cmd variable;
  3. Throws the sql_cmd variable away and sets it to a new OleDbCommand instance;
  4. Attempts to execute the sql_cmd without adding any parameters to it;

This is yet another reason not to store the OleDbConnection and OleDbCommand objects in class-level fields. The first bit of your code is manipulating a command from a previous method. If the sql_cmd field hasn't been initialized, you may even get a NullReferenceException.

Change your code to create and use a new OleDbCommand instance, wrapped in a using block:
C#
using (OleDbCommand cmd = sql_con.CreateCommand())
{
    cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
    
    cmd.Parameters.AddWithValue("@ref_det", TxtRefProduit.Text);
    cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
    cmd.Parameters.AddWithValue("@Designation", TxtDesignation.Text);
    cmd.Parameters.AddWithValue("@Prix_unitaire_HT", TxtPrixUnitaire.Text);
    cmd.Parameters.AddWithValue("@Prix_total_HT", total);
    
    sql_con.Open();
    cmd.ExecuteNonQuery();
}
Ideally, you should change your code to create the OleDbConnection as a local variable wrapped in a using block too, and delete the sql_con and sql_cmd fields.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: problem registering in the access database Pin
ago248617-Mar-20 3:14
ago248617-Mar-20 3:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.