locked
DropDown DataText field RRS feed

  • Question

  • User-1499457942 posted

    Hi

       How we can combine 2 fields in DropDown DataText field

    ddl.employee = "FirstName" + "LastName" 

    Thanks

    Saturday, October 13, 2018 10:12 AM

Answers

  • User-369506445 posted

    yes, you can do it

    SELECT *, FirstName + '-' + LastName as fullName FROM Employee

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 14, 2018 6:18 AM

All replies

  • User-369506445 posted

    hi

     

    You can add an additional column to the <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="6" data-gr-id="6">datatable</g> that is a computed column and use it as your <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="7" data-gr-id="7">datatextfield</g>

    ds.Tables[0].Columns.Add("FristAndLast",typeof(string),"FirstName + LastName");
    
    ddl.DataSource = ds;
    ddl.DataTextField = "FristAndLast";
    ddl.DataBind();
    ddl.Items.Insert(0, new ListItem("Please select"));

    Saturday, October 13, 2018 10:17 AM
  • User-1499457942 posted

    Hi Vahid

      In my code how it can be done

    SqlDataAdapter sdaEmployee = new SqlDataAdapter(cmd);
    DataTable dtEmployee = new DataTable();
    sdaEmployee.Fill(dtEmployee);
    if (dtEmployee.Rows.Count > 0)
    {
    ddl_Employee1.DataSource = dtEmployee;
    ddl_Employee1.DataTextField = "FirstName";
    ddl_Employee1.DataValueField = "EmployeeId";
    ddl_Employee1.DataBind();

    }

    Thanks

    Sunday, October 14, 2018 5:57 AM
  • User-369506445 posted

    you can combine them in your SQL query below like :

    using (SqlConnection c = new SqlConnection("Connection String"))
                {
                    c.Open();
                    using (SqlDataAdapter a = new SqlDataAdapter(
                        @"SELECT EmployeeId, FirstName + '-' + LastName as fullName FROM Employee", c))
                    {
                        // 3
                        // Use DataAdapter to fill DataTable
                        DataTable dtEmployee = new DataTable();
                        a.Fill(dtEmployee);
                        ddl_Employee1.DataSource = dtEmployee;
                        ddl_Employee1.DataTextField = "fullName";
                        ddl_Employee1.DataValueField = "EmployeeId";
                        ddl_Employee1.DataBind();
    
                    }
                }

    Sunday, October 14, 2018 6:13 AM
  • User-1499457942 posted

    Hi Vahid

      If i have to select all fields from table in select statement

    Thanks

    Sunday, October 14, 2018 6:14 AM
  • User-369506445 posted

    yes, you can do it

    SELECT *, FirstName + '-' + LastName as fullName FROM Employee

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 14, 2018 6:18 AM