Problems with passing values Asp.net C#

Asked

Viewed 102 times

0

I’m having a problem when I’m going to assign the return of query for a variable of the type object she passes as null. I have other identical methods, and they work well, just the one that’s not passing the right value. I could see that the DbValue takes the right value, but at the time of assigning passes null. My code is like this:

public string getEmailUsuario(string loginRede)
        {
            object email = "";
            string Sql;

             Sql = "SELECT EMAIL FROM VIEW_ADUSERS_LAZ_BR WHERE LOGIN_REDE = @LOGIN";

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnIntranet"].ConnectionString;
            SqlCommand cmd = new SqlCommand(Sql, conn);

            cmd.Parameters.AddWithValue("@LOGIN", loginRede);

            try
            {
                conn.Open();

                email = cmd.ExecuteScalar(); <--- Aqui dá o erro. Passa "null"

                if (email != DBNull.Value)
                {
                    return email.ToString();
                }

                return "Sem email";

            }
            catch (Exception e)
            {
                _MSGERROR = e.Message.ToString();
                return "Sem email";
            }
            finally { conn.Close(); }
        }
  • Query the Database and see if returned any value. Most likely not.

  • I’ve done @Marconi and come back good ..

  • its field LOGIN_REDE is varchar?

  • It’s a View query, it’s like Nvarchar.

1 answer

2


Deducing that your column LOGIN_REDE is varchar you should include the quotes in @LOGIN to make the comparison between varchars.

When you move the Query to the bank it will be executed like this:

SELECT EMAIL FROM VIEW_ADUSERS_LAZ_BR WHERE LOGIN_REDE = seulogin

Instead of:

"SELECT EMAIL FROM VIEW_ADUSERS_LAZ_BR WHERE LOGIN_REDE = @LOGIN"

Use:

"SELECT EMAIL FROM VIEW_ADUSERS_LAZ_BR WHERE LOGIN_REDE = '@LOGIN'"
  • All right, I’ll try here, thanks

  • @Andreeh Deu certo?

  • didn’t work out. I saw that I was going through wrong thing, fixed but still doesn’t work, now everything is fine and nothing ..

  • Now I got @Marconi, but now it’s time to send the email.

  • @Andreeh open another question with the discussion of the same.

  • i opened. http://answall.com/questions/67426/problema-ao-mail-usando-smtpclient-c/67427#67427

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.