Monday, 13 November 2017

Show data in comboBox window application through sqlserver connectivity using App.config file in ASP.Net with C#


*These namespaces are must be required for SqlServer Connectivity in your web form:-
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

App.config File
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <appSettings>
    <add key="ConnectionString" value="Data Source=PRADEEP-HP;Initial Catalog=Decofloore2;User ID=User_006;Password=user006"/>
  </appSettings>
</configuration>

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DemoWindowApplication
{
    public partial class Form3 : Form
    {   
        public Form3()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            #region ddlList
            string StrCon = ConfigurationManager.AppSettings["ConnectionString"];
            SqlConnection SqlCon = new SqlConnection(StrCon);
            string SqlQuery = "Select ID,UserName,Address from Contectus";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlCon);
            SqlCon.Open();
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader();
            BindingSource bs = new BindingSource();
            bs.DataSource = dr;
            comboBox1.DataSource = bs;
            comboBox1.DisplayMember = "UserName";
            comboBox1.ValueMember = "ID";
            comboBox1.Text = "       -------Name-------";
            #endregion
        }
    }
}

0 comments

Post a Comment