Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem to retrieve an info in a sql request / problème pour recéper une info dans une requête sql

problem to retrieve an info in a sql request / problème pour recéper une info dans une requête sql

Scheduled Pinned Locked Moved C#
databasehelp
45 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard DeemingR Richard Deeming

    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:

    // 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:

    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_co

    A Offline
    A Offline
    ago2486
    wrote on last edited by
    #33

    Thank you very much to you I try it and I come back to you

    1 Reply Last reply
    0
    • A ago2486

      answer 4

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #34

      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!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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!

        A Offline
        A Offline
        ago2486
        wrote on last edited by
        #35

        Can you explain it to me please? I'm just a beginner who learns through some tutorials

        OriginalGriffO 1 Reply Last reply
        0
        • A ago2486

          Can you explain it to me please? I'm just a beginner who learns through some tutorials

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #36

          Ok. Suppose we do this:

          List myList = new List();
          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 myList = new List();
          myList.Add("One");
          myList.Add("Two");
          myList.Add("Three");
          myList.Add("Four");
          myList.Add("Five");
          myList = new List();
          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!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          A 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Ok. Suppose we do this:

            List myList = new List();
            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 myList = new List();
            myList.Add("One");
            myList.Add("Two");
            myList.Add("Three");
            myList.Add("Four");
            myList.Add("Five");
            myList = new List();
            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!

            A Offline
            A Offline
            ago2486
            wrote on last edited by
            #37

            you are right sir I doubted but after having compiled I realized that the variable s is empty.

            OriginalGriffO 1 Reply Last reply
            0
            • A ago2486

              you are right sir I doubted but after having compiled I realized that the variable s is empty.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #38

              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!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                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!

                A Offline
                A Offline
                ago2486
                wrote on last edited by
                #39

                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.

                OriginalGriffO 1 Reply Last reply
                0
                • A ago2486

                  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.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #40

                  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!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  A 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    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!

                    A Offline
                    A Offline
                    ago2486
                    wrote on last edited by
                    #41

                    it does not matter sir I understand you but nevertheless if I have a problem I will post it for a possible help

                    1 Reply Last reply
                    0
                    • A ago2486

                      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();
                      //ExecuteQuery(txtQuery);
                      }
                      }
                      catch(OleDbException ex)
                      {
                      MessageBox.Show(ex.Source);
                      }

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #42

                      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. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      A 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        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. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        A Offline
                        A Offline
                        ago2486
                        wrote on last edited by
                        #43

                        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?

                        L 1 Reply Last reply
                        0
                        • A ago2486

                          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?

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #44

                          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. :)

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          A 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            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. :)

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            A Offline
                            A Offline
                            ago2486
                            wrote on last edited by
                            #45

                            thank you sir i will do it now

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups