Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Asp.code

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1" DataTextField="socialname">
                                 <asp:ListItem text="select">Comment using
                             
                             <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:prakashConnectionString %>" SelectCommand="SELECT [socialname] FROM [socialnetwork]">
                             <asp:Label ID="Label3" runat="server" Text="Label">


C# code

C#
protected void Bindddl() {

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from socialnetwork", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DropDownList1.DataTextField = "socialname";
    DropDownList1.DataValueField = "name";
    DropDownList1.DataBind();
    con.Close();
}

protected void BindTitle()
{
    if (DropDownList1 != null)
    {
        foreach (ListItem li in DropDownList1.Items)
        {
            li.Attributes["title"] = "social/" + li.Value; // it ll set the value of items in dropdownlist as tooltip

        }
    }
}

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
    BindTitle();
    Label3.Text = DropDownList1.SelectedItem.Text;
    Label3.Text = "Commented by";
}
Posted
Updated 15-Sep-14 0:13am
v2

I think "name" is not exist in your select statement.

So, Solution will be as follow.
C#
DropDownList1.DataValueField = "socialname";
 
Share this answer
 
v2
At your sql query u are query as
SQL
SELECT [socialname] FROM [socialnetwork]


But while binding you are using value field as
DropDownList1.DataValueField = "name";

so change your code or retrieve that name column from database and you can set socialnameas text and name as value field of the dropdown.
 
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