Execute code only if query successful
-
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
-
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
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
-
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
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...:-\ :-\
-
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
-
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
Provide some code.........
-
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
Before executing the query, try checking that the .Count of said query is positive. This might solve your problem.
-
Before executing the query, try checking that the .Count of said query is positive. This might solve your problem.
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
-
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
Joe follow Paw Jershauge's advice.. look at the inner exception