how to update my dropdownlist by value?

ibne ali 1 Reputation point
2022-08-02T09:11:35.207+00:00

<asp:DropDownList ID="ddlWeekOffType" class="form-control" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlWeekOffType_SelectedIndexChanged">
<asp:ListItem Text="-Select-" Value="0"></asp:ListItem>
<asp:ListItem Text="Constant" Value="C"></asp:ListItem>
<asp:ListItem Text="Variable" Value="V"></asp:ListItem>
<asp:ListItem Text="Constant Variable" Value="CV"></asp:ListItem>
</asp:DropDownList>

This is my dropdownlist ..Fact is i am store data value by help of Text property .Here i am selecting a text property is
Constant
like this .
obj.WeekOffType = Convert.ToString(ddlWeekOffType.SelectedItem.Value);

if (ddlWeekOffType.SelectedItem.Value == "C")
then insert data in sql is C .In storing data situation working good..

updating dropdownlist is fetch data from sql is perfectly. is retrieve is C is working fine.
obj.WeekOffType = Convert.ToString(dt.Rows[0]["WeekOffType"]);

but problem is that i can't retrieve Constant in dropdownlist.

ddlWeekOffType.Items.FindByValue(obj.WeekOffType.Trim().ToString()).Selected = true;

Any slove this issue so please ....

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,237 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,231 Reputation points Microsoft Vendor
    2022-08-03T02:21:50.587+00:00

    Hi @ibne ali ,
    According to your description, I think you are getting Text instead of Value from database.
    You can try FindByText instead of FindByValue.
    You have provided less information, maybe you can provide full code that reproduces the problem.

    obj.WeekOffType = Convert.ToString(dt.Rows[0]["WeekOffType"]);  
    ddlWeekOffType.Items.FindByText(obj.WeekOffType.Trim().ToString()).Selected = true;  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments