-1

I am previously only familiar with Linq and the like for data access. I am working on something now that requires me to use actual SQL commands on the back end to return a single value. My code compiles and runs, however it is returning null for a value that I know should be returning something besides an empty string...

Is my structure off on this? Or is something else missing?

Below is my code:

  internal string GetSexDescription(string sex, int id_merchant)
    {
        string newSex = "";

        var builder = new ConnectionStringHelper();
        var connString = builder.getCasinoDBString(id_merchant);

        using (SqlConnection conn = new SqlConnection(connString))
        {
            string sql = "SELECT Description FROM person_gender_lookup WHERE ID = @sex";

            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;

                newSex = cmd.ExecuteScalar().ToString();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return newSex;
        }
    }

Here is a picture of the result set of the table:

enter image description here

Jacked_Nerd
  • 209
  • 3
  • 12

4 Answers4

2

Open the connection.

internal string GetSexDescription(string sex, int id_merchant)
{
   string newSex = "";

   var builder = new ConnectionStringHelper();
   var connString = builder.getCasinoDBString(id_merchant);

   using (SqlConnection conn = new SqlConnection(connString))
   {
      conn.Open(); //<- This line here.
      string sql = "SELECT Description FROM person_gender_lookup WHERE ID = @sex";

      SqlCommand cmd = new SqlCommand(sql, conn);
      try
      {
         cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;

         newSex = cmd.ExecuteScalar().ToString();
      }
      catch(Exception ex)
      {
         Console.WriteLine(ex.Message);
      }

      return newSex;
   }
}

cmd.ExecuteScalar() is probably throwing an InvalidOperationException because you haven't opened the connection. The exception is being caught, outputted to the console, then the initial value of newSex is begin returned since the call to ExecuteScalar threw.

Joshua Robinson
  • 3,399
  • 7
  • 22
1

ID is a int or varchar? If is int use:

cmd.Parameters.Add("@sex", SqlDbType.Int).Value = sex;

instead of:

cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;

P.S. Query parameters and parameter add into cmd.Parameters is case sensitive.

Write

@sex

instead of

@Sex
L.Zoffoli
  • 129
  • 3
  • 10
  • ID in the table is an int... however how the rest of the project is set up, it was wrongfully passing a string for ID instead of an int. If I were to re-structure, I would have to undo a lot of things already in motion unfortunately.. – Jacked_Nerd Jun 11 '19 at 16:25
  • You also miss conn.Open(); – L.Zoffoli Jun 11 '19 at 16:36
0

Figured it out. Had to open the cmd and close it AFTER I set the newSex variable to the value being pulled.

   internal string GetSexDescription(string sex, int id_merchant)
    {
        string newSex = "";

        var builder = new ConnectionStringHelper();
        var connString = builder.getCasinoDBString(id_merchant);

        DataSet ds = new DataSet();

        using (SqlDataAdapter adapter = new SqlDataAdapter())
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string sql = "SELECT Description FROM person_gender_lookup WHERE ID = @Sex";

                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    adapter.SelectCommand = cmd;
                    cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = sex;
                    adapter.Fill(ds);

                    newSex = cmd.ExecuteScalar().ToString();

                    conn.Close();

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                return newSex;
            }
        }

    }
Jacked_Nerd
  • 209
  • 3
  • 12
-1

Try this:

internal string GetSexDescription(string sex, int id_merchant)
{
    string newSex = "";

    var builder = new ConnectionStringHelper();
    var connString = builder.getCasinoDBString(id_merchant);

    using (SqlConnection conn = new SqlConnection(connString))
    {
        string sql = "SELECT Description FROM person_gender_lookup WHERE ID" +  sex;;

        SqlCommand cmd = new SqlCommand(sql, conn);
        try
        {


            newSex = cmd.ExecuteScalar().ToString();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return newSex;
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
John Meek
  • 173
  • 1
  • 9