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. Execute code only if query successful

Execute code only if query successful

Scheduled Pinned Locked Moved C#
databasehelpquestion
8 Posts 7 Posters 1 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.
  • J Offline
    J Offline
    Joe Stansfield
    wrote on last edited by
    #1

    Hi Guys, I'm having trouble with an exception error after executing a query to return one record from my dataset. If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'. Could someone please explain what this means and where I need to look to fix it and avoid an exception? Thanks, Joe

    N P R P M 5 Replies Last reply
    0
    • J Joe Stansfield

      Hi Guys, I'm having trouble with an exception error after executing a query to return one record from my dataset. If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'. Could someone please explain what this means and where I need to look to fix it and avoid an exception? Thanks, Joe

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Joe Stansfield wrote:

      If the query returns a result that doesn't exist within the database

      What? With so little information it is impossible to tell you where to look. Perhaps if you posted a snippet of the relevant code we may be able to help.


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • J Joe Stansfield

        Hi Guys, I'm having trouble with an exception error after executing a query to return one record from my dataset. If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'. Could someone please explain what this means and where I need to look to fix it and avoid an exception? Thanks, Joe

        P Offline
        P Offline
        Paw Jershauge
        wrote on last edited by
        #3

        Have you checked the innerException of the Exception('Exception has been thrown by the target of an invocation') ??? whats it saying ???

        With great code, comes great complexity, so keep it simple stupid...:-\ :-\

        1 Reply Last reply
        0
        • J Joe Stansfield

          Hi Guys, I'm having trouble with an exception error after executing a query to return one record from my dataset. If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'. Could someone please explain what this means and where I need to look to fix it and avoid an exception? Thanks, Joe

          R Offline
          R Offline
          RobCroll
          wrote on last edited by
          #4

          More info is required because there are many reasons this exception can be thrown. Is it a stored procedure that you are calling? Does it always work the first time? Stuff like that

          Architecture is extensible, code is minimal.

          1 Reply Last reply
          0
          • J Joe Stansfield

            Hi Guys, I'm having trouble with an exception error after executing a query to return one record from my dataset. If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'. Could someone please explain what this means and where I need to look to fix it and avoid an exception? Thanks, Joe

            P Offline
            P Offline
            Pravin Patil Mumbai
            wrote on last edited by
            #5

            Provide some code.........

            1 Reply Last reply
            0
            • J Joe Stansfield

              Hi Guys, I'm having trouble with an exception error after executing a query to return one record from my dataset. If the query returns a result that doesn't exist within the database, I am getting a 'Exception has been thrown by the target of an invocation'. Could someone please explain what this means and where I need to look to fix it and avoid an exception? Thanks, Joe

              M Offline
              M Offline
              Michael Kingsford Gray
              wrote on last edited by
              #6

              Before executing the query, try checking that the .Count of said query is positive. This might solve your problem.

              J 1 Reply Last reply
              0
              • M Michael Kingsford Gray

                Before executing the query, try checking that the .Count of said query is positive. This might solve your problem.

                J Offline
                J Offline
                Joe Stansfield
                wrote on last edited by
                #7

                Sorry about the delay in getting back to you all - have been out of the office and based in Australia. Code that executes with the query is as follows:

                        try
                        {
                            //Run query to open file
                            this.file\_InfoTableAdapter.fFile\_File\_Open(this.salesDataSet.File\_Info, ((int)(System.Convert.ChangeType(fFile\_Open\_File\_Text\_Box.Text, typeof(int)))));
                
                            //Sort sales dgv to sort by date
                            this.saleInformationDataGridView.Sort(this.saleInformationDataGridView.Columns\["dataGridViewTextBoxColumn9"\], ListSortDirection.Descending);
                
                            //Set photo list box to display correct photo list
                            photoListBox.Items.Clear();
                            photoListBox.Items.Add(file\_NameTextBox.Text + "\_a.jpg");
                            photoListBox.Items.Add(file\_NameTextBox.Text + "\_b.jpg");
                            photoListBox.Items.Add(file\_NameTextBox.Text + "\_c.jpg");
                            photoListBox.Items.Add(file\_NameTextBox.Text + "\_d.jpg");
                
                            //Display first file photo if one exists
                            string photoDisplay = "P:\\\\" + localityTextBox.Text + "\\\\" + file\_NameTextBox.Text + "\_a.jpg";
                            this.photoBox.ImageLocation = photoDisplay;
                
                            //Set property sale detail text boxes back to read only
                            land\_use\_codeTextBox.ReadOnly = true;
                            land\_areaTextBox.ReadOnly = true;
                            construction\_yearTextBox.ReadOnly = true;
                            room\_countTextBox.ReadOnly = true;
                            wall\_construction\_codeTextBox.ReadOnly = true;
                            roof\_construction\_codeTextBox.ReadOnly = true;
                            building\_areaTextBox.ReadOnly = true;
                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                

                I have limited knowledge about avoiding exceptions, but it is my understanding that all the code underneath the running of the query is being run even if the query returns no result? Leading to the exception? But to be honest, I really have no idea... I do know that there is a lot I could improve in the code, but it is a very much "Learn as I go" approach... Which of course isn't the best, but I can only learn and get better! Joe

                M 1 Reply Last reply
                0
                • J Joe Stansfield

                  Sorry about the delay in getting back to you all - have been out of the office and based in Australia. Code that executes with the query is as follows:

                          try
                          {
                              //Run query to open file
                              this.file\_InfoTableAdapter.fFile\_File\_Open(this.salesDataSet.File\_Info, ((int)(System.Convert.ChangeType(fFile\_Open\_File\_Text\_Box.Text, typeof(int)))));
                  
                              //Sort sales dgv to sort by date
                              this.saleInformationDataGridView.Sort(this.saleInformationDataGridView.Columns\["dataGridViewTextBoxColumn9"\], ListSortDirection.Descending);
                  
                              //Set photo list box to display correct photo list
                              photoListBox.Items.Clear();
                              photoListBox.Items.Add(file\_NameTextBox.Text + "\_a.jpg");
                              photoListBox.Items.Add(file\_NameTextBox.Text + "\_b.jpg");
                              photoListBox.Items.Add(file\_NameTextBox.Text + "\_c.jpg");
                              photoListBox.Items.Add(file\_NameTextBox.Text + "\_d.jpg");
                  
                              //Display first file photo if one exists
                              string photoDisplay = "P:\\\\" + localityTextBox.Text + "\\\\" + file\_NameTextBox.Text + "\_a.jpg";
                              this.photoBox.ImageLocation = photoDisplay;
                  
                              //Set property sale detail text boxes back to read only
                              land\_use\_codeTextBox.ReadOnly = true;
                              land\_areaTextBox.ReadOnly = true;
                              construction\_yearTextBox.ReadOnly = true;
                              room\_countTextBox.ReadOnly = true;
                              wall\_construction\_codeTextBox.ReadOnly = true;
                              roof\_construction\_codeTextBox.ReadOnly = true;
                              building\_areaTextBox.ReadOnly = true;
                          }
                          catch (System.Exception ex)
                          {
                              System.Windows.Forms.MessageBox.Show(ex.Message);
                          }
                  

                  I have limited knowledge about avoiding exceptions, but it is my understanding that all the code underneath the running of the query is being run even if the query returns no result? Leading to the exception? But to be honest, I really have no idea... I do know that there is a lot I could improve in the code, but it is a very much "Learn as I go" approach... Which of course isn't the best, but I can only learn and get better! Joe

                  M Offline
                  M Offline
                  meaningoflights
                  wrote on last edited by
                  #8

                  Joe follow Paw Jershauge's advice.. look at the inner exception

                  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