|
yes yes sir I am still looking for, I will find it, I assure you sir
|
|
|
|
|
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
OleDbCommand cmr = sql_con.CreateCommand();
cmr.CommandText = "SELECT @@IDENTITY AS LastId";
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
id = (int)cmr.ExecuteScalar();
TxtNunCmd.Text = id.ToString();
cmd1.ExecuteNonQuery();
}
but i can't get the id
|
|
|
|
|
Stop guessing, and start thinking.
When is the ID actually created? What line of code causes that to happen?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello sir and all my excuses for yesterday I had a connection problem that's why I disconnected but by dint of fighting all night I could find the solution regaré and tell me if the code is clean. I must say I am glad I did ...
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();
OleDbCommand cmr = sql_con.CreateCommand();
cmr.CommandText = "SELECT @@IDENTITY AS LastId";
int LastId = Convert.ToInt32(cmr.ExecuteScalar());
TxtNunCmd.Text = LastId.ToString();
}
|
|
|
|
|
Yes, that's fine - you don't need the "AS LastID" because ExecuteScalar doesn't return labels, but it doesn't matter if you add it.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi guys i really need help. Any one Know how to set and marked ontime,late,and absence in sqldatabas help plss
|
|
|
|
|
Yes. I do, and so do most other people here.
Glad we could resolve that for you.
Next time, if you want to ask a question, try to make it rather more specific about your actual problem, and tell us what you have tried, where you are stuck, and what help you need. Remember that we can't see your screen, access your HDD, or read your mind - and that we aren't here to do your homework for you!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Bonsoir les amis veuillez m’excuser mais un problème pour recéper une info dans une requete sql.
Good evening friends please excuse me but a problem to receive information in a sql request.
try
{
setConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "INSERT INTO Commandes (montant_com) VALUES ('" + TxtTotalCmd.Text + "')";
ExecuteQuery(CommandText);
long numCmd;
string CommandText1 = "SELECT MAX(num_com) AS dernier_num FROM Commandes";
sql_cmd = new OleDbCommand(CommandText1, sql_con);
numCmd = sql_cmd.Parameters.Add("dernier_num"); (j'ai une erreur à ce niveau)
}
[edit]Google Translate added - OriginalGriff[/edit]
|
|
|
|
|
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood' The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable; Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x'; A perfectly valid SELECT
DROP TABLE MyTable; A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
And when you've fixed that throughout you app, start looking at the problem you have noticed.
And that is probably in your ExecuteQuery method: if you are creating an SQL Reader ther, then you can't issue any more commands on that connection until the Reader is closed...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
merci pour votre réponse je vais revoir mon code et je vous reviens
|
|
|
|
|
You're welcome - but please use English in future; this is an English language site and we only normally accept questions in that language. Google Translate does a pretty good job and it saves everybody else from having to do it for you!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Et surtout merci pour vos conseils
|
|
|
|
|
hello sir all my apologies for the delay ... i was putting my code clean as you advised me. I have an error like microsoft jet engine
try
{
setConnection();
sql_con.Open();
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();
}
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Source);
}
|
|
|
|
|
Quote: I have an error like microsoft jet engine
Is pretty meaningless on it's own ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You are right sir but frankly I don't know how to explain it to you. but once I submit the add button after filling in all the fields as it should be this is the message I receive. And nothing in the access database.
|
|
|
|
|
What is the message you recieve?
What line does it happen on?
What does the debugger show you is going on?
So far, all I have is "I have an error like microsoft jet engine" which tells me nothing at all!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ok, thank you sir. I removed my block of code in the try and here is the error: at the level of sql_cmd.ExecuteNonQuery ();
System.Data.OleDb.OleDbException: 'No value given for one or more of the required parameters.'
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;
setConnection();
sql_con.Open();
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();
}
|
|
|
|
|
Right: and what do you think that message means?
Hint: What do you do immediately after you add the parameter values to sql_cmd ? (This is not a trick question)
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
for me this message means that one of the values entered in the texbox is not loaded in the database suddenly it sees no data in the fields. I have the impression that my fields remain empty after adding
|
|
|
|
|
I even tried this if, I have the same error
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)";
OleDbParameter refprod = sql_cmd.Parameters.AddWithValue("@ref_det", OleDbType.VarChar);
refprod.Value = TxtRefProduit.Text;
OleDbParameter qtecmd = sql_cmd.Parameters.AddWithValue("@qute_det", OleDbType.VarChar);
qtecmd.Value = TxtQteCmd.Text;
OleDbParameter design = sql_cmd.Parameters.AddWithValue("@Designation", OleDbType.VarChar);
design.Value = TxtDesignation.Text;
OleDbParameter prixU = sql_cmd.Parameters.AddWithValue("@Prix_unitaire_HT", OleDbType.Integer);
prixU.Value = TxtPrixUnitaire.Text;
OleDbParameter prixT = sql_cmd.Parameters.AddWithValue("@Prix_total_HT", OleDbType.Integer);
prixT.Value = total;
sql_cmd = new OleDbCommand(txtQuery, sql_con);
sql_cmd.ExecuteNonQuery();
}
|
|
|
|
|
Please, look closely at your code.
What does this line do exactly:
sql_cmd = new OleDbCommand(txtQuery, sql_con);
Not a trick question: Here it is as multiple choice:
1) Nothing
2) Throw away the existing content of the variable and replace it with a new, empty one.
3) Catch fire and die.
4) Create a new instance and assign all the old data to it.
Extra hint: It's less than (3) and greater than (1).
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Wrong.
Quote: Extra hint: It's less than (3) and greater than (1).
Think: What does the new keyword actually do?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Can you explain it to me please? I'm just a beginner who learns through some tutorials
|
|
|
|
|
Ok.
Suppose we do this:
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
myList.Add("Five");
foreach (string s in myList)
{
Console.WriteLine(s);
} You would expect it do print five lines, and indeed it will:
One
Two
Three
Four
Five
But what if I do this:
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
myList.Add("Five");
myList = new List<string>();
foreach (string s in myList)
{
Console.WriteLine(s);
} How many lines of print do you expect to get now? (Run the code if you have to, I don't mind.)
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|