locked
Dropdownlist Boolean Yes No Text RRS feed

  • Question

  • User1038842493 posted

    Hi ,

    I have a sql table graduated data type bit. Also dropdownlist data source linq to sql. I want to be if dropdownlist text true text=Yes else text false text=No . 

    DataClasses1DataContext test = new DataClasses1DataContext();

    var graduatedload = from student in test.students

    select student.graduate;

    Thanks in advance

    Friday, February 3, 2017 10:15 PM

All replies

  • User-1509636757 posted

    If there will be fix two ListItem only then I suggest to write these ListItem in Design code, this will save you a database trip:

    <asp:DropDownList ID="ddlList" runat="server" OnDataBound="ddlList_DataBound">
        <asp:ListItem Text="Yes" Value="true" />
        <asp:ListItem Text="No" Value="false" />
    </asp:DropDownList>

    However, if this needs to be done anyway, then I suggest you can write below code after DropDownList is binding with values (or in DropDownList OnDataBound event):

    protected void ddlList_DataBound(object sender, EventArgs e)
    {
        ddlList.Items.Cast<ListItem>().ToList().ForEach(item => SetDropDownItem(item));
    }
    
    private void SetDropDownItem(ListItem Item)
    {
        Item.Text = Item.Text.ToLower().Equals("true") ? "Yes" : "No";
    }

    hope that helps./.

    Saturday, February 4, 2017 4:28 AM
  • User1038842493 posted

    Hi 

    Thanks for helping but. I'cant understand ddlist to sql connection. 

    My students table.

    studentid int 
    studentname nvarchar(50) 
    studentsurname nvarchar(50) 
    graduated bit

    ı want to be dropdownlist1 selected student name and 2 two textbox write studentname and surname also dropdownlist2 graduated change yes or no .

    dropdownlist1 selectedindex query normal sql query but I will change  with linq to sql.

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {


    try
    {
    string ConnectionString = ConfigurationManager.ConnectionStrings["student"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("select from students Where studentid='" + (DropDownList1.SelectedValue) + "'", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (Convert.ToInt32(DropDownList1.SelectedValue) < 1)
    {
    TextBox1.Text = "";
    TextBox2.Text = "";
    ddlList.SelectedValue = "-1";
    }
    else
    {
    while (dr.Read())
    {
    Session["userID"] = DropDownList1.SelectedValue;
    TextBox1.Text = dr["studentname"].ToString();
    TextBox2.Text = dr["studentsurname"].ToString();
    ddlList.SelectedValue = dr["graduated"].ToString();
    }

    }
    conn.Close();
    }

    finally
    {

    }

    Thanks

    Saturday, February 4, 2017 8:02 AM
  • User-1838255255 posted

    Hi Asp.net Learning,

    According to your code, as far as I know, you execute a query operation in SelectedIndexChanged event, then retrieve returned data. I am not clear what you can't understand, I need you could explain clearly.

    Asp.net Learning

    ı want to be dropdownlist1 selected student name and 2 two textbox write studentname and surname also dropdownlist2 graduated change yes or no .

    I am not clear this sentence meaning, I hope you could give us more detailed description!

    Best Regards,

    Eric Du

    Friday, February 17, 2017 9:19 AM