|
Yes - and that's because the new keyword creates a new, empty instance of the List and throws away the old one when it is assigned to the variable.
And your code does exactly the same thing:
...
sql_cmd.Parameters.AddWithValue("@Prix_total_HT", total);
sql_cmd = new OleDbCommand(txtQuery, sql_con);
sql_cmd.ExecuteNonQuery(); You load up the parameters into an OleDbCommand instance, and then throw it all away to create a new, empty instance and store it in the same variable.
To be honest, if you are "just a beginner who learns through some tutorials" then you are doing it all wrong, particularly if these are YouTube tutorials - I've yet to see one that is of any real use whatsoever. Instead, look for a good book on the subject - Apress, Wrox, Addison Wesley, Microsoft Press - they all do excellent beginner volumes though I don't know if any of them are available in French. If you can, look for a copy of "Pro C# 8.0" (APress, I believe), or "C# in a nutshell" (O'Reilly?) - I learnt from those one many, many years ago when .NET was at V2!
Books introduce the material in a structured way, building on what has been taught before - and aren't written just to get views and subscribers...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
really thank you for your advice i will inquire to see if the book is available in french. I did a bit of programming at school but not the csharp, and since it is a langauge that I am passionate about, I decided to take it easy but I really want to know the role of each method used. And for its I thank you again because I followed your advice. But with your permission I would like to send you some project that I have realized thanks to courses on Csharp to just give me your opinion on the presentation of my codes and if there is an improvement to be made.
|
|
|
|
|
Don't send it, I won't look at it.
I don't have time to be a mentor to anyone - and I get a couple of request for it a month, and certainly couldn't do it for everyone who asks: so I don't do it at all to be as fair as I can to everybody.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
it does not matter sir I understand you but nevertheless if I have a problem I will post it for a possible help
|
|
|
|
|
ago2486 wrote: 'No value given for one or more of the required parameters.' That is not "some error", it tells you exactly what the problem is. You need to make sure you are submitting a value for all parameters.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Look at his code and see what he does immediately after adding the parameters and their values...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: Look at his code Ya, I know. I was more referring to the fact that three separate people asked what the error was and the OP just kept saying "some error."
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Hi,
please stop doing
catch(OleDbException ex)
{
MessageBox.Show(ex.Source);
}
instead do
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
It will (1) be able to catch more problems, and (2) will provide much more information when something goes wrong (including the exact line number when running in Visual Studio); you may not understand all of its output right away, but that typically is the info one needs to easily pinpoint what went wrong.
|
|
|
|
|
Hello sir and thank you for the info, I think I saw it when I was doing my research. And it was written only to see the problems with connection to the database. So I would like to know if we could use it only at the connection of the database or not?
|
|
|
|
|
All exceptions derive from the Exception class, so catching Exception will catch "everything".
Whatever your code is about, it is most often wise to put it inside a try-catch block that catches
and displays all information about all exceptions.
And when your code misbehaves and you don't yet have a try-catch, adding one is the first thing you should do.
modified 18-Mar-20 11:15am.
|
|
|
|
|
thank you sir i will do it now
|
|
|
|
|
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.
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.
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
|
|
|
|
|
Hello sir and thank you for your help. I did as you advised me but I have a microsoft jet engine error. I will see another tutorial on this subject if I will find a solution ...
|
|
|
|
|
If you want someone to help you fix an error, you need to give us the full details of the error.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
ok thank you sir. But there is not much detail except that when I submit the button to add I receive the message microsoft jet engine.
private void BtnAjouter_Click(object sender, EventArgs e)
{
int total;
decimal total_achat;
if (TxtDesignation.Text == "" || TxtPrixUnitaire.Text == "" || TxtQteCmd.Text == "" || TxtQteStock.Text == "" || TxtRefProduit.Text == "")
{
MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (Int32.TryParse(TxtQteCmd.Text, out int value) && TxtRefProduit.Text != "")
{
int a = 0;
int b = 0;
int c;
int.TryParse(TxtQteCmd.Text.Trim(), out a);
int.TryParse(TxtQteStock.Text.Trim(), out b);
int.TryParse(TxtPrixUnitaire.Text.Trim(), out c);
if (a <= b)
{
total = a * c;
total_achat = 0;
try
{
{
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();
}
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Source);
}
try
{
LoadDB();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
}
TxtTotalCmd.Text = total_achat.ToString();
TxtDesignation.Text = "";
TxtQteCmd.Text = "";
TxtPrixUnitaire.Text = "";
TxtRefProduit.Text = "";
TxtQteCmd.Text = "";
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Source);
sql_con.Close();
}
finally
{
sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!");
}
}
}
}
|
|
|
|
|
That's not the full error message.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I followed your advice, sir, regarding possible SQL injection. I may be wrong but I made the parameter request as you told me, but if he has other errors, can you help me please
|
|
|
|
|
Sorry, I posted that before I noticed that you'd commented-out the vulnerable code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thank you sir i was afraid i didn't do what you told me.
|
|
|
|
|
private void BtnAjouter_Click(object sender, EventArgs e)
{
int total;
decimal total_achat;
if (TxtDesignation.Text == "" || TxtPrixUnitaire.Text == "" || TxtQteCmd.Text == "" || TxtQteStock.Text == "" || TxtRefProduit.Text == "")
{
MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (Int32.TryParse(TxtQteCmd.Text, out int value) && TxtRefProduit.Text != "")
{
int a = 0;
int b = 0;
int c;
int.TryParse(TxtQteCmd.Text.Trim(), out a);
int.TryParse(TxtQteStock.Text.Trim(), out b);
int.TryParse(TxtPrixUnitaire.Text.Trim(), out c);
if (a <= b)
{
total = a * c;
total_achat = 0;
try
{
{
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();
}
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Source);
}
try
{
LoadDB();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
}
TxtTotalCmd.Text = total_achat.ToString();
TxtDesignation.Text = "";
TxtQteCmd.Text = "";
TxtPrixUnitaire.Text = "";
TxtRefProduit.Text = "";
TxtQteCmd.Text = "";
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Source);
sql_con.Close();
}
finally
{
sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!");
}
}
}
}
|
|
|
|
|
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:
{
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();
}
- Creates a string variable to hold the query;
- Adds 5 parameters to the
sql_cmd variable; - Throws the
sql_cmd variable away and sets it to a new OleDbCommand instance; - 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:
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
|
|
|
|
|
Thank you very much to you I try it and I come back to you
|
|
|
|
|
and here is my connection
private void setConnection()
{
try
{
string connetionString = null;
connetionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\DB_CaisseEnregistreuse.mdb;Persist Security Info=True;Jet OLEDB:Database Password=B@sta08091987";
sql_con = new OleDbConnection(connetionString);
}
catch (Exception ex)
{
MessageBox.Show("Erreur de connexion à la base donnée" + ex.Message);
}
}
|
|
|
|
|
But what is the error message?
|
|
|
|
|
System.Data.OleDb.OleDbException: 'No value given for one or more of the required parameters.'
|
|
|
|
|