Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

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?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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
Avatar of Peter Chan

ASKER

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.
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...
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?
>>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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
>>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?