Method runs for every instance of the method - c#

I am trying to set my ASP row and cell color based off my method in my data class. I have the color set and the method running. The method returns true or false based on the conditions below. How can I prevent the method from running multiple times?
For instance, it runs once and returns correctly then it runs again with "" in the lineNum and brandNum fields with the pack count as the same number as the first run. It processed to run once for every instance of my method call. Then when I actually call the method again it runs everything again. Why is it running more than once and how can I fix this?
output:
lineNum:123 brandNum:456 packCount:15
second run (not asked for)
lineNum:"" brandNum:"" packCount:15
Method to determine if true:
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("#GenNum6", itemNum),
new SqlParameter("#GenTxt9", brandNum)
};
try
{
reader = App_Code.DBHelper.executeQuery(dbConn,
sqlString.ToString(), parameters);
if (reader.HasRows)
{
while (reader.Read())
{
PackCount = reader["PackCount"].ToString();
}
}
reader.Close();
reader.Dispose();
dbConn.Close();
dbConn.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (dbConn != null)
{
try { dbConn.Close(); dbConn.Dispose(); }
catch { }
}
if (reader != null)
{
try { reader.Close(); reader.Dispose(); }
catch { }
}
}
if (Convert.ToInt32(PackCount) <= 12)
{
return true;
}
else
{
return false;
}
Call:
if (lineData.PacksLeft1(L1.Text,L2.Text))
{
myTable.Rows[1].Cells[0].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[1].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[2].BgColor = "#FE2E2E";
myTable.Rows[1].Cells[3].BgColor = "#FE2E2E";
}

There is no need for the finally statement as it performs the same action that is performed in the try statement. I believe that after seeing this piece, if you go back through where it is being called from, you may have it running in a try catch, doing the exact same thing. I would suggest taking the
reader.Close();
reader.Dispose();
dbConn.Close();
dbConn.Dispose();
out of the try block and leaving the finally statement. This way, no matter the outcome, these items are performed.
Finally is only used when you want the code to run something after it has preformed the try statement. Such as:
Try{
//Try this statement, if it works, run it.
}
catch{
//error occurred
}
finally{
//run this code whether the try failed or not.
}

Related

Interrupting long running method pattern

I am currently using this somewhat tedious pattern to generate error message for user running some long operation:
string _problem;
void SomeLongRunningMethod()
{
try
{
_problem = "Method1 had problem";
Method1();
_problem = "Unexpected error during doing something in Method2";
if(Method2())
{
_problem = "Method3 fails";
Method3();
}
_problem = "Not possible to obtain data";
var somedata = Method4();
}
catch(Exception)
{
MessageBox.Show("Problem with some long running method: " + _problem);
}
}
Either of methods may throw and I want to tell the user at which step failure occurs. This is done by setting _problem before running any of them.
In some cases I can use different Exception types to catch, but that doesn't works always, e.g. both Method1 and Method2 can throw InvalidOperationException().
This repeated code looks like a pattern. Though I can't recognize it. Any ideas? How to improve readability?
You could use when in the catch to differentiate between the same exception types and to check which method threw this exception:
void SomeLongRunningMethod()
{
try
{
Method1();
if (Method2())
{
Method3();
}
var somedata = Method4();
}
catch (InvalidOperationException invEx) when (invEx.TargetSite?.Name == nameof(Method1))
{
// ...
}
catch (InvalidOperationException invEx) when (invEx.TargetSite?.Name == nameof(Method2))
{
// ...
}
catch (Exception ex)
{
// ...
}
}
You could get the method that caused the exception using error.TargetSite. The only thing you need to change is your catch line: catch (Exception error)
I'd make a sequence of the things you want to do and run through them:
var methodList = new[]{
new{action = (Action)Method1, identifier = "Method1"},
new{action = (Action)Method2, identifier = "Method2"},
new{action = (Action)Method3, identifier = "Method3"},
};
string problem = null;
foreach(var info in methodList)
{
try
{
info.action();
}
catch(InvalidOperationException)
{
problem = string.Format("{0} failed", info.identifier);
break;
}
}
if(problem != null)
{
//notify
}

Test connection before get data with data reader

I have come up with the following "pattern" to get data from a database.
For sure, I wanted to check if connection could be established, so I need to use a try-catch block.
It works fine but I am curious if there is a better/faster/safer way.
public List<T> GetAll(string sql)
{
var list = new List<T>();
var db = new SqlDatabase(Registry.Instance.CurrentConnectionString);
var cmd = new SqlCommand(sql);
IDataReader reader = null;
using (cmd)
{
try
{
reader = db.ExecuteReader(cmd);
}
catch (SqlException ex)
{
ShowExceptionError(ex);
return list;
}
}
using (reader)
{
while (reader.Read())
list.Add(GetMethod(reader));
reader.Close();
}
return list;
}
I know that the best way is to declare the dataReader in the using block, but since I need the reference outside the try-catch block and I don't refer to it outside the using block, I think it's fine.
This is purely subjective, but I'd suggest instead of using the "using" blocks, use the "try/catch/finally" block. Something like this:
public List<T> GetAll(string sql)
{
var list = new List<T>();
SqlDatabase db = null;
SqlCommand cmd = null;
IDataReader reader = null;
try
{
db = new SqlDatabase(Registry.Instance.CurrentConnectionString);
cmd = new SqlCommand(sql);
reader = db.ExecuteReader(cmd);
while (reader.Read())
{
list.Add(GetMethod(reader));
}
}
catch (SqlException ex)
{
// Deal with sql exceptions
ShowExceptionError(ex);
return list;
}
catch (Exception ex)
{
// Deal with all other exceptions
ShowExceptionError(ex);
return list;
}
finally
{
if (db != null)
{
db.Dispose();
}
if (cmd != null)
{
cmd.Dispose();
}
if (reader != null)
{
reader.Dispose();
}
}
return list;
}
The idea here is to initialize everything inside the try block, so that if anything fails, one of the catch statements will handle it. I always use an additional catch(Exception e) block which handles anything that I didn't predict. The finally block will happen in both cases whether the code successfully executes or if there's an exception.
Also, there should be no need for closing the reader because the Dispose method will handle it. The code above is not tested and some parts might fail. The problem in question might be if one of the objects does not implement IDisposable interface, you can't use it in the using block and you can't dispose them. In that case just remove it from the finally block.

Best way to throw exceptions on query attempt c#

Hi I'm developing a winform application using C# and the entity framework (linq to entities).
Suppose the following escenario:
In a method of some class, I set and object of values with form values
private void agrega_cliente_Click(object sender, EventArgs e)
{
cliente = new _Cliente();
try
{
cliente.nombres = nom_cliente.Text;
cliente.apellidoP = apellidoP_cliente.Text;
cliente.apellidoM = apellidoM_cliente.Text;
cliente.fechaNacimiento = fechaNacimientoPicker.Value.Date;
if (operaciones.AgregaCliente(cliente, referencias))
{
MessageBox.Show("Cliente Agregado");
this.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Note that the assigment and calling to method "AgregaCliente" is between a try and catch, so if an exception is triggered a MessageBox will show it.
Then in other class, I have AgregaCliente method which insert values in a database.
public bool AgregaCliente(_Cliente cliente, ArrayList refes)
{
try
{
Cliente cli = new Cliente()
{
Nombres = cliente.nombres,
ApellidoP = cliente.apellidoP,
ApellidoM = cliente.apellidoM,
FechaNac = cliente.fechaNacimiento
};
if (NombreExiste(cli))
context.clientes.AddObject(cli);
else
throw new System.ArgumentException("El usuario ya existe");
if (refes.Count != 0)
{
foreach (_Referencia elem in refes)
context.referencias_personales.AddObject(AgregaReferencia(elem));
}
context.SaveChanges();
}
catch (Exception ex)
{
return false;
}
return true;
}
Inside this method there is a call to "NombreExiste()" which checks that the user isn't already inserted, if the user exists an exception is thrown.
So the problem here is that if an exception is thrown in "AgregaCliente" method, I want this exception to be catched by "agrega_cliente_Click()" method, so the user knowns what originated the problem. I hope you understand what I'm trying to do.
Thanks
Simply get rid of your try/catch inside the AgregaCliente() method and the exception will automatically bubble-up.
public bool AgregaCliente(_Cliente cliente, ArrayList refes)
{
Cliente cli = new Cliente()
{
Nombres = cliente.nombres,
ApellidoP = cliente.apellidoP,
ApellidoM = cliente.apellidoM,
FechaNac = cliente.fechaNacimiento
};
if (NombreExiste(cli))
context.clientes.AddObject(cli);
else
throw new System.ArgumentException("El usuario ya existe");
if (refes.Count != 0)
{
foreach (_Referencia elem in refes)
context.referencias_personales.AddObject(AgregaReferencia(elem));
}
context.SaveChanges();
return true;
}
The problem is that your AgregaCliente() method is catching all exceptions and simply swallowing them. Instead of catching all exceptions via:
catch (Exception ex)
{
return false;
}
You should only catch specific Exceptions you can handle and let the others pass up the call chain. However, you should know that throwing exceptions is very "expensive" for a program. C# does a lot of work behind the scenes when an exception is thrown. A better solution may be to use a return code to indicate to callers of the AgregaCliente() method the status. For example:
public enum AgregaClienteStatus
{
Success = 0;
ClientAlreadyExists = 1;
Other = ??; // Any other status numbers you want
}
public AgregaClienteStatus AgregaCliente(_Cliente cliente, ArrayList refes)
{
Cliente cli = new Cliente()
{
Nombres = cliente.nombres,
ApellidoP = cliente.apellidoP,
ApellidoM = cliente.apellidoM,
FechaNac = cliente.fechaNacimiento
};
if (NombreExiste(cli))
context.clientes.AddObject(cli);
else
return AgregaClienteStatus.ClientAlreadyExists
if (refes.Count != 0)
{
foreach (_Referencia elem in refes)
context.referencias_personales.AddObject(AgregaReferencia(elem));
}
context.SaveChanges();
return AgregaClientStatus.Success;
}
Of course, this functionality could also be achieved using constant integers if you don't like enums.
You can then use that return status to indicate information to the user without the expense of an exception:
var result = AgregaClient(cliente, refes);
switch (result)
{
case AgregaClientStatus.Success:
// Perform success logic
break;
case AgregaClientStatus.ClientAlreadyExists:
MessageBox.Show("Client already exists");
break;
// OTHER SPECIAL CASES
default:
break;
}
}

How to Deal with Asynchronous SQL Queries from Non-UI Threads

All, I have successfully used ADO.NET to make use of asynchronous SQL queries similar to the example below. In the example shown the method ExecNonQuery is being invoked from the UI thread. This works well, but I wondered how I would handle the callback if I were to call ExecNonQuery from a non-UI thread?
Note. Clearly, in such a case I would amend ExecNonQuery, so that such things as this.toolStripStatusLabel1.Text were dealt with accordingly, or removed.
public bool ExecNonQuery(string strCmd, string strUserMsg = "")
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strCmd;
cmd.CommandTimeout = 0;
bIsExecuting = true;
AsyncCallback callback = new AsyncCallback(HandleCallback);
cmd.BeginExecuteNonQuery(callback, cmd);
return true;
}
catch (Exception Ex)
{
bIsExecuting = false;
this.toolStripStatusLabel1.Text = String.Format("Ready (last error: {0})", Ex.Message);
if (conn != null)
conn.Close();
}
return false;
}
private delegate void DisplayInfoDelegate(string Text);
private void HandleCallback(IAsyncResult result)
{
try
{
// Retrieve the original command object, passed
// to this procedure in the AsyncState property
// of the IAsyncResult parameter.
SqlCommand command = (SqlCommand)result.AsyncState;
int rowCount = command.EndExecuteNonQuery(result);
string rowText = " rows affected.";
if (rowCount == 1)
rowText = " row affected.";
rowText = rowCount + rowText;
// Call the procedure from the form's thread.
DisplayInfoDelegate del = new DisplayInfoDelegate(DisplayResults);
this.Invoke(del, rowText);
}
catch (Exception ex)
{
// Because you are now running code in a separate thread,
// if you do not handle the exception here, none of your other
// code catches the exception.
// You can create the delegate instance as you
// invoke it, like this:
this.Invoke(new DisplayInfoDelegate(DisplayResults),
String.Format("Ready(last error: {0}", ex.Message));
}
finally
{
bIsExecuting = false;
if (conn != null)
conn.Close();
}
}
private void DisplayResults(string Text)
{
this.toolStripStatusLabel1.Text = Text;
this.toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
this.toolStripProgressBar1.Value = 100;
}
Thanks for you time.
It makes no difference to your callback which thread runs ExecNonQuery - HandleCallback will still be run on a thread pool thread.
You have already spotted the change you need to make: don't access UI controls directly in ExecNonQuery if it is not being run on the UI thread.
Nick

C# try-catch-else

One thing that has bugged me with exception handling coming from Python to C# is that in C# there doesn't appear to be any way of specifying an else clause. For example, in Python I could write something like this (Note, this is just an example. I'm not asking what is the best way to read a file):
try
{
reader = new StreamReader(path);
}
catch (Exception)
{
// Uh oh something went wrong with opening the file for reading
}
else
{
string line = reader.ReadLine();
char character = line[30];
}
From what I have seen in most C# code people would just write the following:
try
{
reader = new StreamReader(path);
string line = reader.ReadLine();
char character = line[30];
}
catch (Exception)
{
// Uh oh something went wrong, but where?
}
The trouble with this is that I don't want to catch out of range exception coming from the fact that the first line in the file may not contain more than 30 characters. I only want to catch exceptions relating to the reading of the file stream. Is there any similar construct I can use in C# to achieve the same thing?
Catch a specific class of exceptions
try
{
reader = new StreamReader(path);
string line = reader.ReadLine();
char character = line[30];
}
catch (IOException ex)
{
// Uh oh something went wrong with I/O
}
catch (Exception ex)
{
// Uh oh something else went wrong
throw; // unless you're very sure what you're doing here.
}
The second catch is optional, of course. And since you don't know what happened, swallowing this most general exception is very dangerous.
You could write it like:
bool success = false;
try {
reader = new StreamReader(path);
success = true;
}
catch(Exception) {
// Uh oh something went wrong with opening the file for reading
}
finally {
if(success) {
string line = reader.ReadLine();
char character = line[30];
}
}
You can do this:
try
{
reader = new StreamReader(path);
}
catch (Exception)
{
// Uh oh something went wrong with opening the file for reading
}
string line = reader.ReadLine();
char character = line[30];
But of course, you will have to set reader into a correct state or return out of the method.
Catch more specific exceptions.
try {
reader = new StreamReader(path);
string line = reader.ReadLine();
char character = line[30];
}
catch(FileNotFoundException e) {
// thrown by StreamReader constructor
}
catch(DirectoryNotFoundException e) {
// thrown by StreamReader constructor
}
catch(IOException e) {
// some other fatal IO error occured
}
Further, in general, handle the most specific exception possible and avoid handling the base System.Exception.
You can nest your try statements, too
Exceptions are used differently in .NET; they are for exceptional conditions only.
In fact, you should not catch an exception unless you know what it means, and can actually do something about it.
You can have multiple catch clauses, each specific to the type of exception you wish to catch. So, if you only want to catch IOExceptions, then you could change your catch clause to this:
try
{
reader = new StreamReader(path);
string line = reader.ReadLine();
char character = line[30];
}
catch (IOException)
{
}
Anything other than an IOException would then propagate up the call stack. If you want to also handle other exceptions, then you can add multiple exception clauses, but you must ensure they are added in most specific to most generic order. For example:
try
{
reader = new StreamReader(path);
string line = reader.ReadLine();
char character = line[30];
}
catch (IOException)
{
}
catch (Exception)
{
}
More idiomatically, you would employ the using statement to separate the file-open operation from the work done on the data it contains (and include automatic clean-up on exit)
try {
using (reader = new StreamReader(path))
{
DoSomethingWith(reader);
}
}
catch(IOException ex)
{
// Log ex here
}
It is also best to avoid catching every possible exception -- like the ones telling you that the runtime is about to expire.
Is there any similar construct I can use in C#
to acheive the same thing?
No.
Wrap your index accessor with an "if" statement which is the best solution in your case in case of performance and readability.
if (line.length > 30) {
char character = line [30];
}
After seeing the other suggested solutions, here is my approach:
try {
reader = new StreamReader(path);
}
catch(Exception ex) {
// Uh oh something went wrong with opening the file stream
MyOpeningFileStreamException newEx = new MyOpeningFileStreamException();
newEx.InnerException = ex;
throw(newEx);
}
string line = reader.ReadLine();
char character = line[30];
Of course, doing this makes sense only if you are interested in any exceptions thrown by opening the file stream (as an example here) apart from all other exceptions in the application. At some higher level of the application, you then get to handle your MyOpeningFileStreamException as you see fit.
Because of unchecked exceptions, you can never be 100% certain that catching only IOException out of the entire code block will be enough -- the StreamReader can decide to throw some other type of exception too, now or in the future.
You can do something similar like this:
bool passed = true;
try
{
reader = new StreamReader(path);
}
catch (Exception)
{
passed = false;
}
if (passed)
{
// code that executes if the try catch block didnt catch any exception
}
I have taken the liberty to transform your code a bit to demonstrate a few important points.
The using construct is used to open the file. If an exception is thrown you will have to remember to close the file even if you don't catch the exception. This can be done using a try { } catch () { } finally { } construct, but the using directive is much better for this. It guarantees that when the scope of the using block ends the variable created inside will be disposed. For a file it means it will be closed.
By studying the documentation for the StreamReader constructor and ReadLine method you can see which exceptions you may expect to be thrown. You can then catch those you finde appropriate. Note that the documented list of exceptions not always is complete.
// May throw FileNotFoundException, DirectoryNotFoundException,
// IOException and more.
try {
using (StreamReader streamReader = new StreamReader(path)) {
try {
String line;
// May throw IOException.
while ((line = streamReader.ReadLine()) != null) {
// May throw IndexOutOfRangeException.
Char c = line[30];
Console.WriteLine(c);
}
}
catch (IOException ex) {
Console.WriteLine("Error reading file: " + ex.Message);
}
}
}
catch (FileNotFoundException ex) {
Console.WriteLine("File does not exists: " + ex.Message);
}
catch (DirectoryNotFoundException ex) {
Console.WriteLine("Invalid path: " + ex.Message);
}
catch (IOException ex) {
Console.WriteLine("Error reading file: " + ex.Message);
}
Sounds like you want to do the second thing only if the first thing succeeded. And maybe catching different classes of exception is not appropriate, for example if both statements could throw the same class of exception.
try
{
reader1 = new StreamReader(path1);
// if we got this far, path 1 succeded, so try path2
try
{
reader2 = new StreamReader(path2);
}
catch (OIException ex)
{
// Uh oh something went wrong with opening the file2 for reading
// Nevertheless, have a look at file1. Its fine!
}
}
catch (OIException ex)
{
// Uh oh something went wrong with opening the file1 for reading.
// So I didn't even try to open file2
}
There might not be any native support for try { ... } catch { ... } else { ... } in C#, but if you are willing to shoulder the overhead of using a workaround, then the example shown below might be appealing:
using System;
public class Test
{
public static void Main()
{
Example("ksEE5A.exe");
}
public static char Example(string path) {
var reader = default(System.IO.StreamReader);
var line = default(string);
var character = default(char);
TryElse(
delegate {
Console.WriteLine("Trying to open StreamReader ...");
reader = new System.IO.StreamReader(path);
},
delegate {
Console.WriteLine("Success!");
line = reader.ReadLine();
character = line[30];
},
null,
new Case(typeof(NullReferenceException), error => {
Console.WriteLine("Something was null and should not have been.");
Console.WriteLine("The line variable could not cause this error.");
}),
new Case(typeof(System.IO.FileNotFoundException), error => {
Console.WriteLine("File could not be found:");
Console.WriteLine(path);
}),
new Case(typeof(Exception), error => {
Console.WriteLine("There was an error:");
Console.WriteLine(error);
}));
return character;
}
public static void TryElse(Action pyTry, Action pyElse, Action pyFinally, params Case[] pyExcept) {
if (pyElse != null && pyExcept.Length < 1) {
throw new ArgumentException(#"there must be exception handlers if else is specified", nameof(pyExcept));
}
var doElse = false;
var savedError = default(Exception);
try {
try {
pyTry();
doElse = true;
} catch (Exception error) {
savedError = error;
foreach (var handler in pyExcept) {
if (handler.IsMatch(error)) {
handler.Process(error);
savedError = null;
break;
}
}
}
if (doElse) {
pyElse();
}
} catch (Exception error) {
savedError = error;
}
pyFinally?.Invoke();
if (savedError != null) {
throw savedError;
}
}
}
public class Case {
private Type ExceptionType { get; }
public Action<Exception> Process { get; }
private Func<Exception, bool> When { get; }
public Case(Type exceptionType, Action<Exception> handler, Func<Exception, bool> when = null) {
if (!typeof(Exception).IsAssignableFrom(exceptionType)) {
throw new ArgumentException(#"exceptionType must be a type of exception", nameof(exceptionType));
}
this.ExceptionType = exceptionType;
this.Process = handler;
this.When = when;
}
public bool IsMatch(Exception error) {
return this.ExceptionType.IsInstanceOfType(error) && (this.When?.Invoke(error) ?? true);
}
}
If you happen to be in a loop, then you can put a continue statement in the catch blocks. This will cause the remaining code of that block to be skipped.
If you are not in a loop, then there is no need to catch the exception at this level. Let it propagate up the call stack to a catch block that knows what to do with it. You do this by eliminating the entire try/catch framework at the current level.
I like try/except/else in Python too, and maybe they will get added to C# some day (just like multiple return values were). But if you think about exceptions a little differently, else blocks are not strictly necessary.

Categories