Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btnshow_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into EMP1(ENAME,DESIGNATION)values('" + txtname.Text + "','" + txtdesig.Text + "')", con);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
        bind();
        con.Open();
        SqlCommand cmd1 = new SqlCommand("Insert into DEPARTMENT(DEPTNAME) values('" + txtdepartment.Text + "')", con);
        cmd1.ExecuteNonQuery();    
        cmd1.Dispose();
        con.Close();
        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();
    }
Posted
Updated 30-Jan-15 1:50am
v2
Comments
Richard Deeming 30-Jan-15 7:48am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Sinisa Hajnal 30-Jan-15 9:04am    
Also, use finally to dispose of objects otherwise in case of exception they don't get disposed.

And instead of parametrized query, you can use stored procedure
Sinisa Hajnal 30-Jan-15 9:06am    
You didn't actually describe what happens, which table gets updated, which doesn't and how did you check it? Also, why closing and reopening the connection between two calls?
(in the stored procedure you could pass all three parameters and have transaction open and much less code to worry about)

1 solution

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

ASP.NET
<%@ 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

C#
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() //dropdownlist
        { 
   
           // List<ListItem> lstForDDL = new List<ListItem>();
            while (reader.Read())
            {
                ddlDEPTNO.Items.Add(reader[0].ToString());
                //ListItem li = new ListItem();
               // li.Value = reader[0].ToString();
                //li.Text = reader[1].ToString();
                //lstForDDL.Add(li);
            }

            con.Close();

          //  ddlDEPTNO.DataSource = lstForDDL;
           // ddlDEPTNO.DataBind();
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900