First make the connection with the database
Example
suppose a table" Employee"
Make the connection with database in the web.config file
the below connection in case the database is Sql server Authentication
<connectionStrings>
<add name="MyString" connectionString="DataSouce = LocalHost; DataBase=DBName; UID=sa; Password = PWDName;"/>
</connectionStrings>
2: For Windows Authentication
<ConnectionStrings>
<add name="MyString" ConnectionString="server= Sqlservername; database=DataBaseName; Integrated Security = "True";>
</ConnectionStrings>
Now the connection has been established with the database , and required to read data from
the database
string str = ConfigurationManager.ConnectionString["MyString"].connectionString;
SqlConnection Con = new SqlConnection(str);
Con.Open();
SqlCommand cmd = new SqlCommand("Select * From TableName", Con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // here object of data adapter fills the data Table
Con.Close();
dropdownlistid.DataSource = dt;
dropdownlistid.DisplayMember = "Your Display Column Name from Data Table"; dropdownlistid.ValueMember= "Your Value Column Name from DataTable" , this canbe ID; dropdownlistid.DataBind();

0 comments:
Post a Comment