0

Hello i have the following cs code

 SqlConnection con = new SqlConnection();
        SqlCommand com = new SqlCommand();
        String comando = "";
        con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["INBIOTECAConnectionString"].ToString());
        SqlDataReader leer = null;
        String diaUltimo = "";
        String fecha = DateTime.Now.ToString("dd/MM/yyyy");

        try
        {
            con.Open();
            comando = " SELECT TOP 1 hora FROM SI_RegEntrada  WHERE nombre = '" + name + "' and dia = '"+fecha+"' ORDER BY id DESC;";
            com = new SqlCommand(comando, con);
            leer = com.ExecuteReader();
            while (leer.Read())
            {
                diaUltimo = leer[0].ToString();
            }

            if (diaUltimo.Equals(""))
            {
                return false;
            }
            else
            {
                return true;
            }



        }
        catch (Exception ex)
        {
            return true;
        }
        finally
        {
            con.Close();
            con.Dispose();
            com.Dispose();
            com = null;
            con = null;
        }

It's supposed to be pretty simple, it's gonna check if there's an entry on the SI_RegEntrada table today (fecha) from any user (name).

SELECT TOP 1 hora FROM SI_RegEntrada  WHERE nombre = '" + name + "' and dia = '"+fecha+"' ORDER BY id DESC;

ok, so far so good, i proceed to read the results on the var diaUltimo.

On my local server, if there are no results (meaning that today, that user, hasn't interact with the system), diaUltimo will get the value of

 diaUltimo = ""

so I check for that in the if/else condition a return true or false. In the live server, it always falls on the catch clause, and return always true.

I was thinking that, maybe diaUltimo will get some kind of null value, or some sql error.

I cannot debug or interact with the live server.

8
  • 1
    Are you sure it is failing at the if/else or the catch? Commented Jul 6, 2017 at 20:59
  • well your query will return NULL, not an empty string if a user hasn't had any interaction, if that helps Commented Jul 6, 2017 at 21:02
  • in the catch, i tried to change the return of the catch to false, and it did it every time, so is 100% sure its falling in the catch Commented Jul 6, 2017 at 21:02
  • So what is the Exception type and Message? Commented Jul 6, 2017 at 21:03
  • Its very important to know which line is raising exception. Even though you cannot debug but can you put some sort of logging to find at what line exception was raised. Probably connection is failing. Commented Jul 6, 2017 at 21:14

3 Answers 3

2

Change the lines

if (diaUltimo.Equals(""))
{
  return false;
}
else
{
 return true;
}

to

if(!String.IsNullOrEmpty(diaUltimo))
{
 return true;
}
else
{
 return false;
}

It will work double duty in checking that you assigned value is neither null or empty

Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking something like that, even if in my local server, it works well, for now I'm going to try with something simpler that I had not thought of: if (leer.HasRows) { return true; } else { return false; }
Like I said @ErickReyes, your SQL returns NULL not an EMPTY string and NULL != anything including NULL. +1 Travis.
0

There is very high chance that con.Open() is raising exception. Nothing else looks problematic in your code.

Comments

0

First, temporarily replace the string contents of comando to be a SQL command which is not dependent on your table or rows...something like:

static string commando = "Select to_char(sysdate) from dual";

This will do 2 things: it will test whether your connection object and SQLExecute both work as expected AND it will test the default format mask on date fields in your live database.

If you do not get any results then I would begin diagnosing the exception by not returning 'true' but instead raising an internal exception and passing the ex object contents out for your to inspect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.