ASP.Net connect to SQL

Get data from SQL table. The related connection string can be stored in web.Config file. Data from the SQL table will be populated in the drop down.
Web.config : Within Web.Config place the connection String within <appSettings>.
<appSettings>
 <add key="ConnInfo"  value="Data Source=SQLSource;Persist Security Info=True;User ID=nnnnn;Password=vvvvv" />   
  </appSettings>
<connectionStrings/>

Aspx.cs page:
SqlConnection  conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnInfo"].ToString());
conn.Open();
if (!IsPostBack)
{
  string Query = "SELECT DISTINCT TITLE FROM V_DATA Where CATEGORY_CODE='Vendor';
  SqlCommand Vendorcomand = new SqlCommand(VendorQuery, conn);
  SqlDataAdapter Vendoradapter = new SqlDataAdapter(Vendorcomand);
  DataSet Vendords = new DataSet();
  Vendoradapter.Fill(Vendords);
  DropVendorName.DataSource = Vendords;
  DropVendorName.DataTextField = "TITLE";
  DropVendorName.DataValueField = "TITLE";
  DropVendorName.DataBind();

  DropVendorName.Items.Insert(0, "Select a Vendor");
  DropVendorName.SelectedIndex = 0;

}
Conn.close();Conn.Dispose();

Leave your comments below.

No comments: