0

I am trying to get value from database to be display in label. First i have to get the value of the dropdownlist and retrieved from database based on it. After that, I need to get the titlePromo column into my Label.

Currently i have the code out but i am not sure if it is the right one. There is no error but it displayed the membershipType column instead of the titlePromo.

  ID  titlePromo   membershipType         defaults  
  --  ----------   --------------         ------    
  1    Promo 1     Membership Promotion      Y      
  2    Promo 2     Membership Renewal        Y       
  3    Promo 3     Membership Grad           Y       
  4    Promo 4     Membership Promotion      N       
  5    Promo 5     Membership Promotion      N      
  6    Promo 6     Membership Grad           N   

My codes that i have done so far:

string strConnectionString = ConfigurationManager.ConnectionStrings["FYPDB"].ConnectionString;

            SqlConnection myConnect = new SqlConnection(strConnectionString);


            string strCommandText2 = "select * FROM FYPDB.dbo.Promotions where membershipType = '%' + @membership + '%' AND defaults = 'Y'";

                string ddlmembership = ((DropDownList)dvInsertPromotion.FindControl("ddlAddMembershiplist")).SelectedItem.ToString();

                cmd.Parameters.Add("@membership", SqlDbType.NVarChar);

                cmd.Parameters["@membership"].Value = ddlmembership;

 DataSet da2 = dal.retrieveTitle(ddlmembership);
                    SqlCommand cmd2 = new SqlCommand(strCommandText2, myConnect);

                    ((Label)pnlDefaultPopup.FindControl("Label13")).Visible = true;
                    ((Label)pnlDefaultPopup.FindControl("Label13")).Text = da2.Tables[0].Rows[0]["titlePromo"].ToString();

html:

.cs

  public DataSet retrieveTitle(String membership)
        {
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@membership", SqlDbType.NVarChar),

            };
            parameters[0].Value = membership;

            DataSet ds = new DataSet();
            ds = commons.ExecuteDataSet("Select * FROM Promotions WHERE (membershipType = '" + membership + "') AND defaults = 'Y' ");

            return ds;

        }
4
  • Your code seems fine to me. Commented May 13, 2016 at 12:50
  • yeah but it's not showing out the result i want. it's showing another column instead Commented May 13, 2016 at 12:57
  • You are selecting the right column how it can show the other column ? You may be binding the grid after that. Commented May 13, 2016 at 12:59
  • If you using that code at page load then make sure that you have used Page.IsPostback property properly. Commented May 13, 2016 at 13:46

1 Answer 1

1

Before giving you my suggestion I would like to make some remarks to your existing code:

  • you should select only the titlePromo in your query, as you only need one field, and not the entire row (therefore you wouldn't need a dataset in the first place)
  • the naming of your function is not according to its scope, at it does not retrieve the title, but an entire entry in the promotions table.
  • in this structure "membershipType = '%' + @membership + '%'" the syntax is not correct. The wildcards are used together with the "like" keyword

Bellow, you can find my code sample of how would I implement it if I were you:

static void Main(string[] args)
    {
        using (SqlConnection PubsConn = new SqlConnection(yourConnectionString))
        {
            //code to retrieve membership   
            var membership = "Membership Promotion";
            var title = retrieveTitle(PubsConn, membership);
           //code to set up label   
        }      
    }

    public static string retrieveTitle(SqlConnection conn, String membership)
    {
        conn.Open();

        var title = string.Empty;
        string strCommandText = "select top 1 titlePromo FROM Promotions where membershipType = @membership AND defaults = 'Y'";
        SqlCommand commmand = new SqlCommand(strCommandText, conn);
        commmand.Parameters.AddWithValue("@membership", membership);

        try
        {
            using (SqlDataReader reader = commmand.ExecuteReader())
            {
                if (reader != null && reader.Read())
                {
                    title = Convert.ToString(reader["titlePromo"]);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error while retrieving table: " + ex.Message);
        }

        conn.Close();
        return title;
    }

If you want to use wildcards and 'like', you can do it like this:

string strCommandText = "select top 1 titlePromo FROM membershipTest where membershipType like @membership AND defaults = 'Y'";
SqlCommand commmand = new SqlCommand(strCommandText, conn);
commmand.Parameters.AddWithValue("@membership", "%" + membership + "%");
Sign up to request clarification or add additional context in comments.

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.