Using Connected Architecture Display the Table 's Column Data in Combobox

By
There are two type of architecture in Ado.Net. which is given below:-
  1. Connected architecture
  2. Disconnected architecture
Now here i am going to discuss about connected architecture:-In connected architecture application directly connected to the database.

Step1:- First create a student table in Sql Database.
create table student(sid int, 'sname' varchar(50),sage int)
insert into student values(101,'ram',22)
insert into student values(102,'shyam',21)
insert into student values(103,'manish',20)
insert into student values(104,'rakesh',18)

select *from student
See it:-

Step2:-  Open your visual studio->click File->New project->select windows form application->click ok.
see it:-



Step3:- Now drag and drop ComboBox  and one button from  on form1 from Toolbox .
see it:-

Step4:- Now write the code on button click(program.cs File).


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

namespace connected_architecture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data source=RAMASHANKER-PC; integrated Security=yes; Database=master");
            con.Open();
            SqlCommand cmd = new SqlCommand("select*from student", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr[0]);
            }
            con.Close();
        }
    }
}

Description:-  Here we have include Namespace using System.Data.SqlClient;  first for connection to the database. here student sid column value comes first cmd variable , after that SqlDataReader read this value and store it into dr variable. After that dr value add in ComboBox.


set it:-

Step4:- Now run your application(press F5)..
see it:-

I hope this is helpful for you.
click below for download whole application.

               DOWNLOAD

NEXT TUTORIAL WILL SHOW YOU  "HOW CAN FETCH VALUE  TO THE DATABASE ON THE BASIC OF  SELECTED COMBOBOX VALUE".

0 comments:

Post a Comment

Powered by Blogger.