Data inserted both table but as you joins two table using DEPTNO column which should exist in both table and in your code DEPTNO column not inserted for any table.So that EMP1.DEPTNO=DEPARTMENT.DEPTNO conditon is not fullfill if you would use INNER join you could not see any data in your gridview although there is data in both table(in your database) .In your case as you are using RIGHT join it only found data from the right table whether DEPTNO matches or not . By realizing your need I suggest you to use a Dropdownlist that will hold DEPTNO(auto column) for DEPERTMENT table and use separate button to insert data into DEPERTMENT table and EMP1 table .And first insert data in DEPARTMENT table the INSERT data in EMP1 table while inserting in EMP1 select a DEPTNO from the dropdownlist .According to your level Although I know your approach is not optimized but I am giving a solution your level best .In dropdownlist you can use value and text property as per your need I guess you are a beginner so I provide it as follows
In ASPX
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplicatioCode._Default" %>
<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" >
<h2>Deptment INSERT</h2>
Enter Department name:<asp:textbox id="txtdepartment" runat="server"></asp:textbox>
<asp:button id="btnDeptINSERT" text="INSERT" onclick="btnDeptINSERT_Click" runat="server" />
<h2>Employee</h2>
DEPTNO<asp:dropdownlist id="ddlDEPTNO" runat="server"></asp:dropdownlist>
Enter Name:<asp:textbox id="txtname" runat="server"></asp:textbox>
Enter Designation:<asp:textbox id="txtdesig" runat="server"></asp:textbox>
<asp:button id="btnshow" text="Show" onclick="btnshow_Click" runat="server" />
<asp:gridview id="grid_id" runat="server">
</asp:gridview>
</asp:content>
In .cs Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplicatioCode
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(@"Data Source=ADDIE-ANIS\ANISSQLSERVER12;Initial Catalog=CodeProjectDB;Persist Security Info=True;User ID=sa;Password=Sa123");
if (!IsPostBack)
{
BindDDL();
bind();
}
}
protected void btnshow_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into EMP1(ENAME,DESIGNATION,DEPTNO)values('" + txtname.Text + "','" + txtdesig.Text + "',"+Convert.ToInt32(ddlDEPTNO.SelectedValue)+")", con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
bind();
}
protected void btnDeptINSERT_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd1 = new SqlCommand("Insert into DEPARTMENT(DEPTNAME) values('" + txtdepartment.Text + "')", con);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
con.Close();
BindDDL();
bind();
}
public void bind()
{
con.Open();
SqlCommand sqlcom = new SqlCommand("SELECT EMP1.ENAME,DEPARTMENT.DEPTNAME,EMP1.DESIGNATION FROM EMP1 RIGHT JOIN DEPARTMENT ON EMP1.DEPTNO=DEPARTMENT.DEPTNO ", con);
SqlDataAdapter ad = new SqlDataAdapter(sqlcom);
DataTable dt = new DataTable();
ad.Fill(dt);
sqlcom.Dispose();
con.Close();
grid_id.DataSource = dt;
grid_id.DataBind();
}
private void BindDDL()
{
while (reader.Read())
{
ddlDEPTNO.Items.Add(reader[0].ToString());
}
con.Close();
}
}
}