|
Seriously, learn to use the debugger - it will show you exactly what is happening while your code is running. It lets you look at variables to check what they contain - and compare them to what you think they should contain - execute your code line by line, and a load of other things.
It's your biggest friend in development - and the place you should be spending most of your time!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Understood sir and thank you for your frankness. I will continue following your advice
|
|
|
|
|
the goal is to add and save later I can do it once when I want to do it for a second time I have an error: unable to find table 0, I must close the form and open it again so that I can do another one ... how to solve this problem without closing my form before?
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
{
setConnection();
sql_con.Open();
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);
cmd.ExecuteNonQuery();
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0]; (Problem is here):confused::confused:
dataGridView1.DataSource = DT;
cmd.Dispose();
sql_con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det, @qute_det, @Designation, @Prix_unitaire_HT, @Prix_total_HT)";
try
{
LoadDB();
//ADDITIONNER UNE COLONNE DU DATAGRIDVIEW
for (int i = 0; i < dataGridView1.Rows.Count; i++) //Car tu fais une infériorité stricte
{
total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
}
TxtTotalCmd.Text = total_achat.ToString();//convertir total_achat en string^pour pouvoir l'afficher dans le textbox
vider();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
sql_con.Close();
}
finally
{
sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!");
}
}
}
}
|
|
|
|
|
Just what does DB have to do with anything?
DB.Fill(DS); It doesn't have any obvious relation to the Connection object, or to the Command - so why would you expect it to do anything useful?
Come on man, you need to start thinking here - instead of just copy'n'paste code from somewhere else and hoping it works!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
you may be right but i have declared its objects to display the info in the datagridview
private OleDbDataAdapter DB;
private DataSet DS = new DataSet ();
private DataTable DT = new DataTable ();
thanks for guiding me
|
|
|
|
|
Oh gawd, I only stopped banging my head on the desk yesterday ...
Stop and think.
Where are you getting the data from?
How does the data source connect to the DataSet?
How does the DataSet connect to the DataTable?
How does the DataTable connect to the DataGridView?
Any one of those links is wrong ... and you don't get data ...
Seriously man - get a book and start reading. You aren't helping yourself by trying and hoping - all you do is make yourself look ... umm ... pretty silly, he said as politely as he could manage.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thank you sir i will read and get back to you because i feel like an idiot. I will review her and let you know.
|
|
|
|
|
Don't feel like an idiot, just learn the basics - because until you understand what you are doing, you are just whistling in the dark and hoping the noise attracts solutions rather than monsters ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thank you sir because you give me courage I will review some court about it to find a solution I will come back to you even if I do not manage
|
|
|
|
|
sir you are right these lines were useless, they were useless ...
I just saw its The DataAdapter object serves as a link between a DataSet object and a data source.
A .NET data provider will use a DataAdapter object to fill data with a DataSet and then pass on the changes made to a data source.
|
|
|
|
|
Yes it does - but that doesn't mean that you create an instance and "magic happens"
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
yes sir i already did to change the datagridview
private void LoadDB()
{
setConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "SELECT * FROM Detail_temp";
DB = new OleDbDataAdapter(CommandText, sql_con);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
dataGridView1.DataSource = DT;
sql_con.Close();
}
maybe I'm wrong? but I did it to see my recordings in the datagridview
|
|
|
|
|
That's better - but in the code you showed to start with, you didn't do that, did you?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
yes sir and again thank you for your advice
|
|
|
|
|
Off topic, but why do you keep putting your messages in text tags?
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 Guys,
I'm trying to write 100 Doors Kata using TDD. I have created the logic like below
But not sure from where I should start to write the Test. I've written 2 test cases like
[TestMethod]
public void TestIfAllDoorsAreClosedAtBiginning()
{
bool[] result = _100DoorsKata.CheckDoorState(0);
for (int i = 1; i <= 100; i++)
{
Assert.IsFalse(result[i]);
}
}
[TestMethod]
public void TestIfAllDoorsAreOpenedAfterFirstRound()
{
bool[] result = _100DoorsKata.CheckDoorState(1);
for (int i = 1; i <= 100; i++)
{
Assert.IsTrue(result[i]);
}
}
And code seems to be:
public bool[] CheckDoorState(int pass)
{
bool[] doors = new bool[101];
for (int i = 1; i <= 100; i++)
{
if (pass == 0)
{
doors[i] = false;
}
else
{
doors[i] = true;
}
}
return doors;
}
Can someone please help me to complete it.
Thanks
modified 18-Mar-20 11:37am.
|
|
|
|
|
IMO it's a bit impractical to use TDD here since you should have a test case for every door, which means having 100 cases.
What you can do to reduce number of test cases is to use test oracle technique. You implement the simplest algorithm possible and compare your solution to output it renders. For example
public static void OracleImplementation(bool[] doors)
{
for (int p = 0; p < 100; p++)
{
for (int d = 0; d < 100; d++)
{
if ((d + 1) % (p + 1) == 0)
{
doors[d] = !doors[d];
}
}
}
}
[Fact]
public void Test()
{
var a = new bool[100];
OracleImplementation(a);
var b = new bool[100];
YourCleverImplementation(b);
b.Should().Equal(a);
}
Note that I've used here a rough sketch of what FluentAssertions do. It may be incomplete tho.
Now in YourCleverImplementation you can come up with more witty solution i.e. select all perfect squares from 1 to 100 and just set your array items to true at indices that are perfect square - 1.
Alternatively, you can use this approach as a test oracle and come up with one more implementation.
I'll leave the rest up to you
|
|
|
|
|
good evening gentlemen and thank you to you who brought me help but I am continuing my project and I have a problem retrieving info from one table to insert it into another.
I have three tables which are related
1.product (article_ID, design, price_U, Qty)
2.order (com_number, com_date, com_amount)
3.detail_command (num_det, num_com, ID_article, qute_det)
I can't fill the detail_commende table. It must be said that I am lost. can you put me on the track please
I may have made a serious mistake in my codes.
private void BtnValider_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Voulez vous valider la facture?", "VALIDATION DE LA FACTURE", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
try
{
setConnection();
sql_con.Open();
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery();
}
long Qte_prod, numCmd;
string ref_prod;
using (OleDbCommand cmd2 = sql_con.CreateCommand())
{
cmd2.CommandText = "SELECT MAX(num_com) AS dernier_num FROM Commandes";
object result = cmd2.ExecuteScalar();
if (result is null || Convert.IsDBNull(result))
{
numCmd = 0;
}
else
{
numCmd = Convert.ToInt64(result);
TxtNunCmd.Text = numCmd.ToString();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
ref_prod = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
Qte_prod = Convert.ToInt64(dataGridView1.Rows[i].Cells[1].Value);
using (OleDbCommand cmd3 = sql_con.CreateCommand())
{
cmd3.CommandText = "INSERT INTO * Detail_commandes (com_det, ref_det, qute_det) VALUES (@com_det,@ref_det,@qute_det)";
cmd3.Parameters.AddWithValue("@com_det", TxtNunCmd.Text);
cmd3.Parameters.AddWithValue("@ref_det", ref_prod);
cmd3.Parameters.AddWithValue("@qute_det", Qte_prod);
cmd3.ExecuteNonQuery();
}
using (OleDbCommand cmd4 = sql_con.CreateCommand())
{
setConnection();
cmd4.CommandText = "UPDATE Catalogue SET Quantite = Quantite - (@Qte_cmd) WHERE code_article = (@code_article)";
cmd4.Parameters.AddWithValue("@Qte_cmd", Qte_prod);
cmd4.Parameters.AddWithValue("@code_article", TxtRefProduit.Text);
cmd4.ExecuteNonQuery();
}
}
sql_con.Close();
SupDetail_Temp();
TxtTotalCmd.Text = "";
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
sql_con.Close();
}
}
else if (dialogResult == DialogResult.No)
{
}
}
|
|
|
|
|
First off, you cannot assume that the last INSERT you did is the last INSERT done by the system - if you have multiple users then their INSERTS can happen at any time. So instead of a separate SELECT to return the maximum value issue a combination command which does the INSERT and returns the value of the IDENTITY field:
INSERT INTO ...; SELECT SCOPE_IDENTITY(); And it will return the IDENTITY vlaue associated with your INSERT as a scalar value.
This is assuming that the commandes table you speak of is actually the detail_command you describe at the top of your question - but if it isn't, then that would explain why you are having trouble with getting it to work ...
Oh, and BTW: I think we've mentioned that you shouldn't recycle SqlConnections before - create them in a using block and they will be Disposed correctly when you are finished with them.
If you must faff with them like you are, then don't close the connection in the main code and the catch block - add a finally block to your try and close it there.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thank you for your answer but I wanted to point out that (num_com) is auto-incremente. I would like to know if that doesn't count?
|
|
|
|
|
Yes, that's the whole point. That is what an IDENTITY field in SQL Server is.
And when you use autoincremented fields you can't rely on the MAX value to be the one you inserted: you do your INSERT, then two dozen other users add their own before you issue your SELECT. YOur MAX then fetches the ID for a totally different row, and you start to get intermittent problems that are fiendishly difficult to duplicate, track down, fix, and prove.
Using SCOPE_IDENTITY returns the last autoincremented value used by that connection, and will never return values from different users.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OK well sir and I just found her in my research, are I adapting well to my code?
using (OleDbCommand cmd1 = sql_con.CreateCommand ())
{
cmd1.CommandText = "INSERT INTO Orders (amount_com) VALUES (@montant_com)";
cmd1.CommandText = "SELECT @@ IDENTITY";
int id = Convert.ToInt32 (cmd1.ExecuteScalar ());
cmd1.Parameters.AddWithValue ("@ amount_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery ();
}
|
|
|
|
|
Stop guessing. Start thinking.
Look at your code and tell me what is wrong with it.
Where have we seen that problem before?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ok my apologies, this is the error I get
System.Data.Oledb.Ole Exception (0x80040E14): Character found after the end of the Sql instruction
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com);" + "SELECT SCOPE_IDENTITY()";
int id;
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery();
id = (int)cmd1.ExecuteScalar();
TxtNunCmd.Text = id.ToString();
}
|
|
|
|
|
That's better!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|