Display count of Stock in DropDownList using C# in ASP.Net

Last Reply one year ago By AnandM

Posted one year ago

 How do i count the total quantity of stock and display it on dropdown control

example below

TABLE

ID            USERNAME            CATEGORY            QUANTITY

-------------------------------------------------------------

1              MIC33                  SHOES              10

-----------------------------------------------------------

2           STEV44                  BAGS               30

----------------------------------------------------

The total quantity count should be - 40

 

 

 

if (!this.IsPostBack)
        {
            string Constr1 = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
            using (SqlConnection CONNE = new SqlConnection(Constr1))
            {
                using (SqlCommand C0MMDE = new SqlCommand("SELECT Count Quantity FROM SixInchesSolHollowMachMoldNkwereStore "))
                {
                    C0MMDE.Parameters.AddWithValue("@Quantity", this.ddlstock.SelectedValue.Trim());

                    C0MMDE.Parameters.AddWithValue("@UserName", Session["userName"]);
                    C0MMDE.CommandType = CommandType.Text;
                    C0MMDE.Connection = CONNE;
                    CONNE.Open();
                    ddlstock.DataSource = C0MMDE.ExecuteReader();
                    // ddlProductCode.DataValueField = "ProductID";
                    ddlstock.DataTextField = "Quantity";
                    // ddlProductCode.DataValueField = "UserName";

                    ddlstock.DataBind();
                    CONNE.Close();
                }
            }
            // ddlstock.Items.Insert(0, new ListItem("-- Stock--", "0"));
        }

 

Posted one year ago
micah says:
if (!this.IsPostBack)
        {
            string Constr1 = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
            using (SqlConnection CONNE = new SqlConnection(Constr1))
            {
                using (SqlCommand C0MMDE = new SqlCommand("SELECT Count Quantity FROM SixInchesSolHollowMachMoldNkwereStore "))
                {
                    C0MMDE.Parameters.AddWithValue("@Quantity", this.ddlstock.SelectedValue.Trim());
 
                    C0MMDE.Parameters.AddWithValue("@UserName", Session["userName"]);
                    C0MMDE.CommandType = CommandType.Text;
                    C0MMDE.Connection = CONNE;
                    CONNE.Open();
                    ddlstock.DataSource = C0MMDE.ExecuteReader();
                    // ddlProductCode.DataValueField = "ProductID";
                    ddlstock.DataTextField = "Quantity";
                    // ddlProductCode.DataValueField = "UserName";
 
                    ddlstock.DataBind();
                    CONNE.Close();
                }
            }
            // ddlstock.Items.Insert(0, new ListItem("-- Stock--", "0"));
        }

 replace above code with below.

if (!this.IsPostBack)
{
    string Constr1 = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
    using (SqlConnection CONNE = new SqlConnection(Constr1))
    {
        using (SqlCommand C0MMDE = new SqlCommand("SELECT Count(Quantity) Quantity FROM SixInchesSolHollowMachMoldNkwereStore "))
        {
            C0MMDE.CommandType = CommandType.Text;
            C0MMDE.Connection = CONNE;
            CONNE.Open();
            ddlstock.DataSource = C0MMDE.ExecuteScalar();
            // ddlProductCode.DataValueField = "ProductID";
            ddlstock.DataTextField = "Quantity";
            // ddlProductCode.DataValueField = "UserName";

            ddlstock.DataBind();
            CONNE.Close();
        }
    }
    // ddlstock.Items.Insert(0, new ListItem("-- Stock--", "0"));
}