SubQuery Or NestedQuery Or InnerQuery Using in SQL

SubQuery

A Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE clause.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.

                                  DEMO

HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</script>
</head>
<body>
    <form id="form1" runat="server">
      <div>
            <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
          
 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" />
      
         <table><tr><td></td><td>   <asp:GridView ID="GridView1" runat="server">
                 <AlternatingRowStyle BackColor="#9999FF" />
                 <HeaderStyle BackColor="#FF3300" />
                                <RowStyle BackColor="#FF99FF" />
            </asp:GridView>
             </td></tr></table>  
        </div>
    </form>
</body>
</html>

                              C# CODING

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


public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }  

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();

        SqlCommand cmd = new SqlCommand           
            ("select * from country where  country in (select country from register where country='"+txtCountry.Text+"')",con);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
}



First - Create New Web Form - Create Two DataBase Tables & Insert The Values  






Next - Add TextBox & Button & Gridview From Toolbox 







Next - Add The SubQuery  in Where Condition







Next - Run[F5] The Program - Enter Country Name - Related  Data's Get Another Table









0 comments:

Post a Comment