Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
0 down vote favorite


I have a web form when i click on search button DropDownList must select the text among the Dataitems available in dropdownlist but problem with my code is that it insert the duplicate Dataitem text instead of selecting original Dataitem. Following is my code

C#
sqlChk = s.callingSP("SP_SearchCase", txtOfficeSerialNO.Text.Trim());
    if (sqlChk.Tables[0].Rows.Count > 0)
    {
        divEmpMast.Style.Add("display", "block");

        lblMsg.Visible = false;

        txtSNO.Text = sqlChk.Tables[0].Rows[0]["SNo"].ToString();

        txtSending.Text = sqlChk.Tables[0].Rows[0]["DateOfSending"].ToString();
        if (string.IsNullOrEmpty(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString()))
        {
            // Write your Custom Code
            sqlChk.Tables[0].Rows[0]["FrmWhere"] = "None";
        }
        else
           ddlSubdistrict.SelectedItem.Text=sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString();


Please help
Posted

You should do like this...
C#
if(ddlSubdistrict.Items.Contains(ddlSubdistrict.Items.FindByText(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString())))
{
    ddlSubdistrict.Items.FindByText(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString()).Selected = true;
}
 
Share this answer
 
v2
Comments
ankitsrist 8-Jan-15 3:29am    
thanks @tadit
Most welcome buddy. :)
Hi ankitsrist,

You are trying to set the text for the current selected item so if the current selected item is something different then it will change the display text for that selected item.

Rather you need to set the SelectedValue for the dropdown if you have that.

It seems you have the text value only to select the dropdown, so here is the solution for your problem :-

JavaScript
ddlSubdistrict.SelectedIndex = ddlSubdistrict.Items.IndexOf(ddlSubdistrict.Items.FindByText(sqlChk.Tables[0].Rows[0]["FrmWhere"].ToString()));


Hope this will definitely of help to you.
 
Share this answer
 
Comments
ankitsrist 8-Jan-15 3:29am    
thank u so much @srs
Hi,

You can also use a dataview for storing the table data there and then you can able to select the distinct values for the data. Please follow this link here.

Thanks
Sisir Patro
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900