Home:ALL Converter>Populate dropdownlist from database with multiparameter

Populate dropdownlist from database with multiparameter

Ask Time:2015-05-11T15:09:28         Author:nf91

Json Formatter

I am trying to populate a dropdownlist from database, but the parameter (Div) has multiple values. I'm trying to use foreach loop but its not working. Is it actually possible or is there another solution for it? Maybe I should put it in the table and populate based from it? Would really appreciate any ideas and suggestion.

here's my code so far:

//strDiv is generated from another table in database, has multiple value and 
for each Div, it will have many product model

DCmdInquiry.CommandText = "Select  SG.DivCode from Staff as ST, StaffGroup as SG " +
                                  " where UserName = @UName and ST.SGroupID = SG.SGroupID";

        SqlParameter UName = new SqlParameter("@UName", SqlDbType.VarChar);
        UName.Value = strTmpName;
        DCmdInquiry.Parameters.Add(UName);

        DRInquiry = DCmdInquiry.ExecuteReader();

        while (DRInquiry.Read())
        {
            strDiv = DRInquiry["DivCode"].ToString().Split(',');
        }

        ddlModelName.AppendDataBoundItems = true;

        SqlConnection DbConn = new SqlConnection();
        SqlCommand DCmdInquiry2 = new SqlCommand();

        DbConn.ConnectionString = ConfigurationManager.ConnectionStrings["LDSTestConnectionString"].ConnectionString;

        DCmdInquiry2.Connection = DbConn;
        DbConn.Open();

        DCmdInquiry2.CommandText = "Select ModelName from ProductModel where Div= @Div";

        foreach (string x in strDiv)
        {
            SqlParameter Div = new SqlParameter("Div", SqlDbType.VarChar);
            Div.Value = x;
            DCmdInquiry2.Parameters.Add(Div);

            ddlModelName.DataSource = DCmdInquiry2.ExecuteReader();
            ddlModelName.DataTextField = "ModelName";
            ddlModelName.DataValueField = "ModelName";
            ddlModelName.DataBind();
        }

Edit- My updated code, as suggested by Tim Schmelter :

foreach (string x in strDiv)
{
   Div.Value = x;
   DCmdInquiry2.Parameters.Add(Div);

   ddlModelName.Items.Add((string)DCmdInquiry2.ExecuteScalar());
   DCmdInquiry2.Parameters.Clear(); 
}
ddlModelName.DataBind();

Thank you.

Update:

After taking into account of Jon P's opinion and doing a lot of trial and error, I have come out with this SQL statement:

select distinct PD.DivCode, PM.ModelName, ST.UserName from ProdDiv as PD
Join Staff as ST on PD.ProdGroup = ST.SGroupID
Join ProductModel as PM on PD.DivCode = PM.Div
where ST.Username = '@UName'

that joins 3 tables to get the result. It works but I am not sure if this is the best way to do it.

I still can't figure out the way to populate dropdownlist using multiparameter that yields more than one result. Tim Schmelter's way of doing it works for only one row (because it's using ExecuteScalar) and I need to yield all rows available.

Since I'm running out of time, I'll just stick with the above SQL. Perhaps its the only "correct" way to do it.

Author:nf91,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30161550/populate-dropdownlist-from-database-with-multiparameter
yy