Click here to Skip to main content
15,882,329 members
Home / Discussions / C#
   

C#

 
GeneralRe: recording problem Pin
ago248617-Mar-20 2:07
ago248617-Mar-20 2:07 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 2:13
mveOriginalGriff17-Mar-20 2:13 
GeneralRe: recording problem Pin
ago248617-Mar-20 2:21
ago248617-Mar-20 2:21 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 2:34
mveOriginalGriff17-Mar-20 2:34 
GeneralRe: recording problem Pin
ago248617-Mar-20 2:40
ago248617-Mar-20 2:40 
GeneralRe: recording problem Pin
ago248617-Mar-20 2:43
ago248617-Mar-20 2:43 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 2:51
mveOriginalGriff17-Mar-20 2:51 
GeneralRe: recording problem Pin
ago248617-Mar-20 3:02
ago248617-Mar-20 3:02 
GeneralRe: recording problem Pin
OriginalGriff17-Mar-20 3:17
mveOriginalGriff17-Mar-20 3:17 
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 
As Griff already pointed out, your first command needs to use a parameter. You should also wrap the OleDbCommand object in a using block, and get rid of the ExecuteQuery method.
C#
using (var cmd = sql_con.CreateCommand())
{
    cmd.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
    cmd.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
    cmd.ExecuteNonQuery();
}
OleDbParameterCollection.AddWithValue(String, Object) Method (System.Data.OleDb) | Microsoft Docs[^]

The second command does not need any parameters. But you do need to execute the command and read the returned value.
C#
long numCmd;
using (var cmd = sql_con.CreateConnection())
{
    cmd.CommandText = "SELECT MAX(num_com) AS dernier_num FROM Commandes";
    
    object result = cmd.ExecuteScalar();
    if (result is null || Convert.IsDBNull(result))
    {
        numCmd = 0L;
    }
    else
    {
        numCmd = Convert.ToInt64(result);
    }
}
OleDbCommand.ExecuteScalar Method (System.Data.OleDb) | Microsoft Docs[^]



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

Generalproblem registering in the access database Pin
ago248617-Mar-20 0:58
ago248617-Mar-20 0:58 

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.