Dropdown box Data binding two ways

Source: Internet
Author: User

1, the use of CS package, DataAccess.cs

#region
String m_str = @ "Select Sname from service Group by sname";
DataTable M_dt = DataAccess.DBHelper.GetList (m_str);
Dropdownlist2.datavaluefield = "Sname";
Dropdownlist2.datatextfield = "Sname";
Binding data Source this
. Dropdownlist2.datasource = M_dt;
Dropdownlist2.databind ();

DropDownList2.Items.Insert (0, New ListItem ("Please select", ""));
#endregion

And in the web.config,

<appSettings>
    <!--database connection string-->
    <add key= "dbconnstring" value= "Data source=localhost; Database=manager;uid=sa;pwd=sa; "/>
  </appSettings>

2,

 SqlConnection conn = new SqlConnection ("Data Source = localhost; Database = Manager; Uid=sa;
        Pwd=sa; "); Conn.

        Open (); Task type binding #region SqlDataAdapter SDT = new SqlDataAdapter ("Select sname from service Group by Sname", conn)

        ;
        DataSet dt = new DataSet (); Sdt.
        Fill (DT);
        Dropdownlist2.datavaluefield = "Sname";
        Dropdownlist2.datatextfield = "Sname"; This.
        Dropdownlist2.datasource = DT;

        Dropdownlist2.databind ();
        DropDownList2.Items.Insert (0, New ListItem ("Please select", "")); #endregion Conn. Close (); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.