Saturday 30 April 2016

Execute multiple select query in a single SQL statement using C# in asp.net

Execute Multiple SQL statement

Description:

In this example we explain that how to execute multiple select query in a single SQL statement in asp.net using C#. or execute multiple SQL queries in a one  statement using C#.

Sometime we have requirement like bind multiple GridView at a time like bind employee and department grid information in a single SQL statement using C#.

Here we demonstrate how to execute two select query simultaneously in one statement using C#.
Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Execute multiple SQL Queries in one Statement using C# and VB.Net</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="EMP. Name" ItemStyle-Width="150px" />
                <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
                <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:GridView ID="grdDepartment" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="DeptName" HeaderText="Department Name" ItemStyle-Width="150px" />
                <asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
                <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100px" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["cnstring"].ConnectionString;
        string query = "SELECT  Name, City, Country FROM Employees;";
        query += "SELECT DeptName, City, Country FROM Department";

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sa = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sa.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sa.Fill(ds);
                        grdEmployee.DataSource = ds.Tables[0];
                        grdEmployee.DataBind();
                        grdDepartment.DataSource = ds.Tables[1];
                        grdDepartment.DataBind();
                    }
                }
            }
        }
    }
}
    }

}

0 comments:

Post a Comment