Problem to DDL

Hi,
I see that
 
Convert.ToString(dt.Rows[0]["status_dscr"])
 
is having correct DDL value inside, but by these
 
 
               if (ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])) != null)
                    ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])).Selected = true;
                                        ...
 

Open in new window

the DDL is not showing correct value on page. why?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
first of all, need to know what's the values in ur DDL ddlStatus3 ?

"status_dscr" looks like a Status Description to me, or do you have some other fields like "status_id" instead that contains the Status Value?

if yes, try change:
Convert.ToString(dt.Rows[0]["status_dscr"])

Open in new window

to:
Convert.ToString(dt.Rows[0]["status_id"])

Open in new window

etc
HuaMin ChenProblem resolverAuthor Commented:
But

Convert.ToString(dt.Rows[0]["status_dscr"])

is having correct DDL value inside, while ddlStatus3 is also showing a list of that kind of value.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
so do you mean the DDL 's item was not selected?

can you tell us:

1. the value of Convert.ToString(dt.Rows[0]["status_dscr"]) ?

2. what's the HTML codes generated from the DDL as the output?

we wish to compare those values together...
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

HuaMin ChenProblem resolverAuthor Commented:
Convert.ToString(dt.Rows[0]["status_dscr"])

is having one value, that is currently one value of the list from DDL. These 2 lines
   
             if (ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])) != null)
                    ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])).Selected = true;
					...

Open in new window

are being fired, upon that I press Edit button. But why can't I see the DDL showing the correct value selected?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>But why can't I see the DDL showing the correct value selected?
Exactly! so that's why I try to give you advise on telling us what's the output generated from your script and then we can diagnose which part causing the "error".

so do you mean the DDL 's item was not selected?

1. the value of Convert.ToString(dt.Rows[0]["status_dscr"]) ?

2. what's the HTML codes generated from the DDL as the output?
Without providing sufficient info, it's difficult for us to give suggestions/solutions
HuaMin ChenProblem resolverAuthor Commented:
Here is how DDL is being binded
        sql = "select code,dscr from c_status where deleted = '0' and system = 'OK' order by id ";
        dt = DBUtil.GetCodeTable(conn, sql);
        ddlStatus.DataSource = dt;
        ddlStatus.DataValueField = "code";
        ddlStatus.DataTextField = "dscr";
        ddlStatus.DataBind();

Open in new window


why can't these
                ddlStatus3.SelectedIndex = -1;
                if (ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])) != null)
                    ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])).Selected = true;

Open in new window

reveal the correct value to DDL, while I check that

Convert.ToString(dt.Rows[0]["status_dscr"])

is having the right value inside.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
since you mentioned:

>> sql = "select code,dscr from c_status where deleted = '0' and system = 'OK' order by id ";

>>ddlStatus.DataValueField = "code";
>>ddlStatus.DataTextField = "dscr";

>>dt.Rows[0]["status_dscr"]

and you are searching via FindByValue method

is that mean there's a possibility dt.Rows[0]["status_dscr"] will equal to "code" in table: c_status ?

in addition, you may try change:

ddlStatus3.Items.FindByValue

Open in new window

to:
ddlStatus3.Items.FindByText

Open in new window

for testing
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
anyway, your script can be written like this instead for better practice:
ddlStatus3.SelectedIndex = -1;
                if (ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])) != null)
                    ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])).Selected = true;

Open in new window

can be write as:
if (ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])) != null) {
                    ddlStatus3.SelectedIndex = -1;
                 ddlStatus3.Items.FindByValue(Convert.ToString(dt.Rows[0]["status_dscr"])).Selected = true; 
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverAuthor Commented:
Many many thanks Ryan. It is fine for that DDL.

But it is not for another DDL below.

Here is how I bind it
 
      sql = @"select store_no as code,dscr
            from c_store_ok c
            where deleted = '0' and dscr is not null and trim(dscr)<>'' order by dscr";

        //sql = ConvStoreUtil.GetSqlStoreCode(accessRight, dept);
        dt = DBUtil.GetCodeTable(conn, sql);
        Session["dtStore"] = dt;
        ddlStore_ok.DataSource = dt;
        ddlStore_ok.DataValueField = "code";
        ddlStore_ok.DataTextField = "dscr";
        ddlStore_ok.DataBind();
		...

Open in new window

By these below
            
                ddlStore.SelectedIndex = -1;
                if (ddlStore.Items.FindByText(Convert.ToString(dt.Rows[0]["store_dscr"])) != null)
                    ddlStore.Items.FindByText(Convert.ToString(dt.Rows[0]["store_dscr"])).Selected = true;

Open in new window

I am not able to show correct value by DDL, even if I've tried FindByValue, while Convert.ToString(dt.Rows[0]["store_dscr"]) is having the correct value (equal to "dscr" that is the binding column)
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>ddlStore_ok.
>>ddlStore

1. can you tell us which one is not selecting the item?
2. can you post the codes for DataBind() of DDL ddlStore_ok?

just a side quick guess.... is that possible your DDL was being refreshed everytime after the page was posted back?

you may want to consider to put a IsPostBack check before you binding the DDL?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.