Tuesday, May 17, 2016

DYNAMIC GRIDVIEW USING INNER JOIN QUERY IN ASP .NET


DYNAMIC GRIDVIEW USING INNER JOIN QUERY IN ASP .NET



protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        String selecteditem = DropDownList1.Text;
        int i = 0;
        string sql = null;
        string connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\daTA\Documents\Visual Studio 2010\WebSites\WebSite2\App_Data\EmpDet.mdf;Integrated Security=True;User Instance=True";
        sql = "select employee.emp_id,employee.emp_name,employee.designation,employee.salary,dept_list.dept_name from employee INNER JOIN dept_list on employee.dept_id=dept_list.dept_id where dept_list.dept_name='"+selecteditem+"';";
        SqlConnection connection = new SqlConnection(connetionString);
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        adapter.Fill(ds);
        adapter.Dispose();
        command.Dispose();
        connection.Close();
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();

}


DATABASE SCHEMA:


mysql> desc dept_list;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dept_id   | int(11)     | NO   | PRI | 0       |       |
| dept_name | varchar(50) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.32 sec)

mysql> desc employee;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| emp_id      | int(11)     | NO   | PRI | 0       |       |
| emp_name    | varchar(50) | YES  |     | NULL    |       |
| designation | varchar(50) | YES  |     | NULL    |       |
| salary      | varchar(50) | YES  |     | NULL    |       |
| dept_id     | int(11)     | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.15 sec)

OUTPUT:


+--------+----------+-------------------+--------+-----------+
| emp_id | emp_name | designation       | salary | dept_name |
+--------+----------+-------------------+--------+-----------+
|      3 | Priya    | HR                | 20,000 | HR        |
|      2 | Mathavan | Android DEVELOPER | 10,000 | MOBILE    |
|      1 | Ram      | JAVA DEVELOPER    | 10,000 | WEB       |
+--------+----------+-------------------+--------+-----------+

No comments:

Post a Comment