Set DropDownList selected Text using C# in ASP.Net

Last Reply 2 days ago By itsme

Posted 4 days ago

When user will login first time then user will fill his profile details and when he login next time then all the filled details will be shown to the user by fetching from database and user can update particular value.. but value of state and city is being fetched in code behind but it is not setting the value in dropdownlist. why?

help me please.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mycon.Open();
                cmd.Connection = mycon;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select Name,email from Common_regn2 where email='" + Session["mail"].ToString() + "'";
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        cname.Text = dr["Name"].ToString();
                        cname.ReadOnly = true;
                        txt_mail.Text = dr["email"].ToString();
                        txt_mail.ReadOnly = true;
                    }                       
                }               
                Session["name"] = cname.Text.ToString();
            }

            if (!IsPostBack)
            {
                mycon.Close();
                mycon.Open();
                cmd.Connection = mycon;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select City,State,phone,Contact_person,Address,Company_website from Company_profile where email='" + Session["mail"].ToString() + "'";
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        State.SelectedItem.Value = dr["State"].ToString();
                        city.SelectedItem.Text = dr["City"].ToString();
                        txt_no.Text = dr["phone"].ToString();
                        txt_Contact.Text = dr["Contact_person"].ToString();
                        txt_address.Text = dr["Address"].ToString();
                        weblink.Text = dr["Company_website"].ToString();
                        btnsave.Visible = false;
                        btnUpdate.Visible = true;
                    }
                }
            }
        }

 

        protected void State_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["sid"] = State.SelectedValue.ToString();
        }

 

 

Posted 4 days ago

Hi itsme,

Refer below code.

Code

Selected Text

if (State.Items.FindByText(dr["State"].ToString()) != null)
{
    State.Items.FindByText(dr["State"].ToString()).Selected = true;
}

Selected Value

if (State.Items.FindByValue(dr["State"].ToString()) != null)
{
    State.Items.FindByValue(dr["State"].ToString()).Selected = true;
}

 



Posted 2 days ago

I actually found a way to do so.

                                        <tr>
                                            <td>
                                                <b>State:</b>
                                            </td>
                                            <asp:Panel runat="server" ID="pnllbls" Visible="true">
                                                <td>
                                                <asp:TextBox ID="txtstate" runat="server" MaxLength="200" Width="250px" ReadOnly="true"></asp:TextBox>
                                                </td>
                                            </asp:Panel>
                                            
                                            
                                            <asp:Panel runat="server" ID="pnlstate" Visible="false">
                                              <td>  
                                                    <asp:DropDownList ID="State" runat="server" class="form-control input-lg" DataSourceID="SqlDataSource1" DataTextField="State"
                                                        DataValueField="SId" OnSelectedIndexChanged="State_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
                                                        <asp:ListItem Value="0">--Select--</asp:ListItem>
                                                    </asp:DropDownList><br />
                                                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                                        SelectCommand="SELECT DISTINCT SId,State FROM [State]"></asp:SqlDataSource>
                                                </td>
                                            </asp:Panel>
                                            
                                        </tr>
                                        <tr>
                                            <td>
                                                <b>City:</b>
                                            </td>
                                            <asp:Panel runat="server" ID="pnllblc" Visible="true">
                                             <td>
                                                <asp:TextBox ID="txtcity" runat="server" MaxLength="200" Width="250px" ReadOnly="true"></asp:TextBox>
                                            </td>
                                            </asp:Panel>
                                            <asp:Panel runat="server" ID="pnlcity" Visible="false">
                                                <td>
                                                    <asp:DropDownList ID="city" runat="server" class="form-control input-lg" DataSourceID="SqlDataSource2"
                                                        DataTextField="City" DataValueField="Id" AutoPostBack="true" AppendDataBoundItems="false">
                                                        <asp:ListItem Value="0">--Select City--</asp:ListItem>
                                                    </asp:DropDownList>
                                                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                                        SelectCommand="SELECT DISTINCT [Id], [City] FROM [City] WHERE ([SId] = @SId)">
                                                        <SelectParameters>
                                                            <asp:SessionParameter Name="SId" SessionField="sid" Type="Int32" />
                                                        </SelectParameters>
                                                    </asp:SqlDataSource>
                                                </td>
                                            </asp:Panel>                                          
                                        </tr>
                                        <tr>                                          
                                            <asp:Panel runat="server" ID="pnlbtn">
                                                <td colspan="2" align="center">
                                                    <asp:Button ID="btnchange" runat="server" Text="Edit" CssClass="btn btn-info" OnClick="btnstate_Click" Width="50px" Height="30px" />
                                                </td>
                                            </asp:Panel>
                                        </tr>
                                        <tr>

 

            if (!IsPostBack)
            {
                mycon.Close();
                mycon.Open();
                cmd.Connection = mycon;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select City,State,phone,Contact_person,Address,Company_website from Company_profile where email='" + Session["mail"].ToString() + "'";
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {                        
                        txtstate.Text = dr["State"].ToString();
                        txtcity.Text = dr["City"].ToString();
                        txt_no.Text = dr["phone"].ToString();
                        txt_Contact.Text = dr["Contact_person"].ToString();
                        txt_address.Text = dr["Address"].ToString();
                        weblink.Text = dr["Company_website"].ToString();
                        btnsave.Visible = false;
                        btnUpdate.Visible = true;
                    }
                }
            }

 

        protected void btnstate_Click(object sender, EventArgs e)
        {
            pnlcity.Visible = true;
            pnlstate.Visible = true;
            pnllbls.Visible = false;
            pnllblc.Visible = false;
            pnlbtn.Visible = false;
        }