一个表值更新了另一个未添加到网格视图的表值 [英] one table values updated another table values not added to grid view

查看:84
本文介绍了一个表值更新了另一个未添加到网格视图的表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  btnshow_Click(对象发​​件人,EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand( Insert进入EMP1(ENAME,DESIGNATION)值(' + txtname.Text + ',' + txtdesig.Text + '),con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
bind();
con.Open();
SqlCommand cmd1 = new SqlCommand( Insert进入DEPARTMENT(DEPTNAME)值(' + 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 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();
}

解决方案

数据同时插入两个表但是as你使用DEPTNO列连接两个表,该列应该存在于表和你的代码DEPTNO列中,不存在任何表插入。因此,如果你使用INNER连接,那么EMP1.DEPTNO = DEPARTMENT.DEPTNO条件不是fullfill你看不到任何数据在你的gridview中,尽管两个表中都有数据(在你的数据库中)。在你使用RIGHT join的情况下,它只能从右表中找到DEPTNO是否匹配的数据。通过实现您的需要,我建议您使用一个Dropdownlist,它将DEPATNO(自动列)保存为DEPERTMENT表,并使用单独的按钮将数据插入DEPERTMENT表和EMP1表。并首先在DEPARTMENT表中插入数据EMP1表中的INSERT数据在EMP1中插入从下拉列表中选择DEPTNO。根据您的级别虽然我知道您的方法没有优化,但我给出的解决方案是您的最佳级别。在下拉列表中,您可以根据需要使用值和文本属性我猜你是一个初学者所以我提供如下



在ASPX中



 <%@       标题  = 主页    Langua ge   =  C#    MasterPageFile   = 〜/ Site.master    AutoEventWireup   =  true  

< span class =code-attribute> CodeBehind = Default.aspx。 cs 继承 = WebApplicatioCode._Default %>

< asp :content id = HeaderContent runat = server contentplaceholderid = HeadContent > ;
< / asp:content >
< asp:content id = BodyContent runat = server contentplaceholderid = 主要内容 >
< h2 > 部门INSERT < / h2 >
输入部门名称:< asp:textbox id = txtdepartment runat = 服务器 > < / asp:textbox >
< asp:button id = btnDeptINSERT text = INSERT onclick = btnDeptINSERT_Click runat = server / >
< h2 > 员工< / h2 >
DEPTNO < asp:dropdownlist id = ddlDEPTNO runat = server > < ; / asp:dropdownlist >
输入名称:< asp:textbox id = txtname runat = 服务器 > < / asp:textbox >
输入名称:< asp:textbox id = txtdesig runat = server > < / asp:textbox < span class =code-keyword>>
< asp:button id = btnshow text = 显示 onclick = btnshow_Click < span class =code-attribute> runat = server / >
< asp:gridview id = grid_id runat = server >
< / asp:gridview >
< / asp:content >



在.cs代码背后



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Data;

命名空间 WebApplicatioCode
{
public partial class _Default:System.Web.UI.Page
{
SqlConnection con;
受保护 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();
}
}
受保护 void btnshow_Click( object sender,EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand( Insert进入EMP1(ENAME,DESIGNATION,DEPTNO)值(' + txtname.Text + ',' + txtdesig.Text + ', + Convert.ToInt32(ddlDEPTNO.SelectedValue )+ ,con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
bind();

}
受保护 void btnDeptINSERT_Click( object sender,EventArgs e)
{
con.Open();
SqlCommand cmd1 = new SqlCommand( Insert进入DEPARTMENT(DEPTNAME)值(' + 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 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() // 下拉列表
{

// 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();
}
}
}


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();
    }

解决方案

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() //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();
        }
    }
}


这篇关于一个表值更新了另一个未添加到网格视图的表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆