Asp.net--dropdownlist Control binds database data

Source: Internet
Author: User

DropDownList control binds database data: DB is a public class, Redt is a method in a public class

Put it in the Page_Load event.

   if(!IsPostBack) {db db=NewDB (); DataTable DT= Db.redt ("SELECT * from Provinceinfo ORDER by Provinceid ASC"); Ddlsmprovince. DataSource=DT; Ddlsmprovince. DataTextField= Ddlsmprovince. DataValueField ="Province"; Ddlsmprovince.            DataBind (); Ddlsmprovince. Items.insert (0,"Not Limited"); }

Redt Method:

 PublicDataTable Redt (stringCMDSTR)//queries the data in the database through SQL statements and stores the query results in a DataSet dataset, eventually returning the data table for the query results in that dataset    {               Try{SqlConnection con=NewSqlConnection (system.configuration.configurationmanager.appsettings["ConnectionString"].            ToString ()); SqlDataAdapter da=NewSqlDataAdapter (Cmdstr, con); DataSet DS=NewDataSet (); Da.            Fill (DS); return(ds. tables[0]);//returns a DataSet object that can be used as the data source for a data-bound control, where the data can be edited        }        Catch(Exception) {Throw; }           }

Asp.net--dropdownlist Control binds database data

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.