将行从一个表移到另一个表。 [英] move row from one table to another .

查看:82
本文介绍了将行从一个表移到另一个表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表SlideShowTable,SlideShowTable1具有相同的结构





在这个项目中,当我点击链接按钮即激活激活行将移动到另一个表格,即SlideShowTable1



我该怎么办...





背后的设计部分和代码如下:





i have 2 table SlideShowTable,SlideShowTable1 with same structure


in this project when i click the link button i.e activate the activated row will move to the another table i.e SlideShowTable1

how i can do...


the deign part and code behind is given below...


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;

}
</style>
<script type="text/javascript">
function ConfirmationBox(username) {

var result = confirm('Are you sure you want to delete '+username+' Details?' );
if (result) {

return true;
}
else {
return false;
}
}
</script>
</head>
<body>
    <form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails"    runat="server"

          DataKeyNames = "UserId,UserName"   AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"

ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"

        onrowcancelingedit="gvDetails_RowCancelingEdit"

        onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"

        onrowupdating="gvDetails_RowUpdating"

        onrowcommand="gvDetails_RowCommand">

<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />

</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />

</FooterTemplate>
 </asp:TemplateField>
<asp:TemplateField HeaderText="UserId">
<EditItemTemplate>
<asp:Label ID="lbleditusrid" runat="server" Text='<%#Eval("UserId") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsrid" runat="server" Text='<%#Eval("UserId") %>'/>
</ItemTemplate>

 </asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("UserName") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("UserName") %>'/>
</ItemTemplate>




<FooterTemplate>

<asp:TextBox ID="txtftrusrname" runat="server"/>
<asp:RequiredFieldValidator ID="rfvusername" runat="server" ControlToValidate="txtftrusrname" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>

 <asp:TemplateField HeaderText="U_Email">
 <EditItemTemplate>
 <asp:TextBox ID="txtU_Email" runat="server" Text='<%#Eval("U_Email") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblU_Email" runat="server" Text='<%#Eval("U_Email") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrU_Email" runat="server"/>
 <asp:RequiredFieldValidator ID="rfvU_Email" runat="server" ControlToValidate="txtftrU_Email" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="ImageName">
 <EditItemTemplate>
 <asp:TextBox ID="txtImageName" runat="server" Text='<%#Eval("ImageName") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblImageName" runat="server" Text='<%#Eval("ImageName") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrImageName" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvImageName" runat="server" ControlToValidate="txtftrImageName" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="Description">
 <EditItemTemplate>
 <asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("Description") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrDescription" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="txtftrDescription" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>
<%-- <asp:TemplateField HeaderText="Description">
 <EditItemTemplate>
 <asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("Description") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblDescription" runat="server" Text='<%#Eval("Description") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrDescription" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="txtftrDescription" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>--%>
 <asp:TemplateField HeaderText="ADshop_name">
 <EditItemTemplate>
 <asp:TextBox ID="txtADshop_name" runat="server" Text='<%#Eval("ADshop_name") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblADshop_name" runat="server" Text='<%#Eval("ADshop_name") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrADshop_name" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvADshop_name" runat="server" ControlToValidate="txtftrADshop_name" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="ADShop_Address">
 <EditItemTemplate>
 <asp:TextBox ID="txtADShop_Address" runat="server" Text='<%#Eval("ADShop_Address") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblADShop_Address" runat="server" Text='<%#Eval("ADShop_Address") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrADShop_Address" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvADShop_Address" runat="server" ControlToValidate="txtftrADShop_Address" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="ADshop_city">
 <EditItemTemplate>
 <asp:TextBox ID="txtADshop_city" runat="server" Text='<%#Eval("ADshop_city") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblADshop_city" runat="server" Text='<%#Eval("ADshop_city") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrADshop_city" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvADshop_city" runat="server" ControlToValidate="txtftrADshop_city" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="ADShop_mob">
 <EditItemTemplate>
 <asp:TextBox ID="txtADShop_mob" runat="server" Text='<%#Eval("ADShop_mob") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblADShop_mob" runat="server" Text='<%#Eval("ADShop_mob") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrADShop_mob" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvADShop_mob" runat="server" ControlToValidate="txtftrADShop_mob" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="ADshop_Email">
 <EditItemTemplate>
 <asp:TextBox ID="txtADshop_Email" runat="server" Text='<%#Eval("ADshop_Email") %>'/>
 </EditItemTemplate>
 <ItemTemplate>
 <asp:Label ID="lblADshop_Email" runat="server" Text='<%#Eval("ADshop_Email") %>'/>
 </ItemTemplate>
 <FooterTemplate>
 <asp:TextBox ID="txtftrADshop_Email" runat="server"/>
  <asp:RequiredFieldValidator ID="rfvADshop_Email" runat="server" ControlToValidate="txtftrADshop_Email" Text="*" ValidationGroup="validaiton"/>
 </FooterTemplate>
 </asp:TemplateField>


 <asp:TemplateField HeaderText="New User Details">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server">Activate</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


 </Columns>
</asp:GridView>

    </div>

<div>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
    </form>
</body>
</html>









code part---









code part---


using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{

     protected   OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
   //  public OleDbConnection  con = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\NAGESH\\Desktop\\GridviewEditUpdateDelete - Copy\\App_Data\\DatabasePCG03.mdb;Persist Security Info=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEmployeeDetails();
            }
        }
        protected void BindEmployeeDetails()
        {
            con.Open();
           // OleDbCommand cmd = new OleDbCommand("Select  *  from LOGIN  , SlideShowTable  where LOGIN.UserId=SlideShowTable.UserId", con);
            OleDbCommand cmd = new OleDbCommand("Select u.UserId ,u.UserName, u.U_Email,s.ImageName ,s.Description,s.ADshop_name,s.ADShop_Address,s.ADshop_city,s.ADshop_mob,s.ADshop_Email from LOGIN u ,SlideShowTable s where u.UserId=s.UserId", con);
            cmd.ExecuteNonQuery();

            OleDbDataAdapter  da = new OleDbDataAdapter (cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvDetails.DataSource = ds;
                gvDetails.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                gvDetails.DataSource = ds;

                int columncount = gvDetails.Rows[0].Cells.Count;
                gvDetails.Rows[0].Cells.Clear();
                gvDetails.Rows[0].Cells.Add(new TableCell());
                gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
                gvDetails.Rows[0].Cells[0].Text = "No Records Found";
                gvDetails.DataBind();

            }

        }

        protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDetails.EditIndex = e.NewEditIndex;
            BindEmployeeDetails();
        }

        protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
            //ring user = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
            
            string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
            //string imagename = gvDetails.DataKeys[e.RowIndex].Values["ImageName"].ToString();
           
            TextBox txtU_Email = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtU_Email");
            TextBox txtImageName = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtImageName");
            TextBox txtDescription = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDescription");
            TextBox txtADshop_name = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtADshop_name");
            TextBox txtADShop_Address = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtADShop_Address");
            TextBox txtADshop_city = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtADshop_city");
            TextBox txtADshop_mob = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtADshop_mob");
            TextBox txtADshop_Email = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtADshop_Email");
            con.Open();
            //OleDbCommand cmd = new OleDbCommand("UPDATE LOGIN u ,SlideShowTable s SET u.U_Email = '" + txtU_Email.Text + "',s.ImageName='" + txtImageName.Text + "',s.Description='" + txtDescription.Text + "',s.ADshop_name='" + txtADshop_name.Text + "',s.ADShop_Address='" + txtADShop_Address.Text + "',s.ADshop_city='" + txtADshop_city.Text + "',s.ADshop_mob='" + txtADshop_mob.Text + "',s.ADshop_Email='" + txtADshop_Email.Text + "' where s.UserId and u.UserId= " + userid, con);
           // OleDbCommand cmd = new OleDbCommand("UPDATE LOGIN u ,SlideShowTable s SET u.U_Email = '" + txtU_Email.Text + "',s.ImageName='" + txtImageName.Text + "',s.Description='" + txtDescription.Text + "',s.ADshop_name='" + txtADshop_name.Text + "',s.ADShop_Address='" + txtADShop_Address.Text + "',s.ADshop_city='" + txtADshop_city.Text + "',s.ADshop_mob='" + txtADshop_mob.Text + "',s.ADshop_Email='" + txtADshop_Email.Text + "' where s.UserId=" + userid, con);
            OleDbCommand cmd =new OleDbCommand("UPDATE LOGIN u ,SlideShowTable s SET u.U_Email = '" + txtU_Email.Text + "',s.ImageName='" + txtImageName.Text + "',s.Description='" + txtDescription.Text + "',s.ADshop_name='" + txtADshop_name.Text + "',s.ADShop_Address='" + txtADShop_Address.Text + "',s.ADshop_city='" + txtADshop_city.Text + "',s.ADshop_mob='" + txtADshop_mob.Text + "',s.ADshop_Email='" + txtADshop_Email.Text + "' where s.UserId=" + userid +" and u.UserId= " + userid, con);
            
            
            
            cmd.ExecuteNonQuery();
            con.Close();
            lblresult.ForeColor = Color.Green;
            lblresult.Text = username + " Details Updated successfully";
            gvDetails.EditIndex = -1;
            BindEmployeeDetails();
        }

        protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDetails.EditIndex = -1;
            BindEmployeeDetails();
        }

        protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
            string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
            //string imagename = gvDetails.DataKeys[e.RowIndex].Values["ImageName"].ToString();

            con.Open();
            OleDbCommand cmd = new OleDbCommand("delete from SlideShowTable  where UserId=" + userid, con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
                lblresult.ForeColor = Color.Red;
                lblresult.Text = username + " details deleted successfully";
            }
        }

        protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //getting username from particular row
                string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"));
                string imagename = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ImageName"));

                //identifying the control in gridview
                ImageButton lnkbtnresult = (ImageButton)e.Row.FindControl("imgbtnDelete");
                //raising javascript confirmationbox whenver user clicks on link button
                if (lnkbtnresult != null)
                {
                    lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + username + "')");
                }

            }
        }

        protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           if(e.CommandName.Equals("AddNew"))
           {
               TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
               TextBox txtU_Email = (TextBox)gvDetails.FooterRow.FindControl("txtftrU_Email");
               TextBox txtImageName = (TextBox)gvDetails.FooterRow.FindControl("txtftrImageName");
               TextBox txtDescription = (TextBox)gvDetails.FooterRow.FindControl("txtftrDescription");
               TextBox txtADshop_name = (TextBox)gvDetails.FooterRow.FindControl("txtftrADshop_name");
               TextBox txtADShop_Address = (TextBox)gvDetails.FooterRow.FindControl("txtftrADShop_Address");
               TextBox txtADshop_city = (TextBox)gvDetails.FooterRow.FindControl("txtftrADshop_city");
               TextBox txtADshop_mob = (TextBox)gvDetails.FooterRow.FindControl("txtftrADshop_mob");
               TextBox txtADshop_Email = (TextBox)gvDetails.FooterRow.FindControl("txtftrADshop_Email");
               
               con.Open();
               OleDbCommand cmd = new OleDbCommand("insert into LOGIN u ,SlideShowTable s(UserName,U_Email,ImageName,Description,ADshop_name,ADShop_Address,ADshop_city,ADshop_mob,ADshop_Email) values('" + txtUsrname.Text + "','" + txtU_Email.Text + "','" + txtImageName.Text + "','" + txtDescription.Text + "','" + txtADshop_name.Text + "','" + txtADshop_city.Text + "','" + txtADshop_city.Text + "','" + txtADshop_mob.Text + "','" + txtADshop_Email.Text + "')", con);
              int result= cmd.ExecuteNonQuery();
              con.Close();
               if(result==1)
               {
                   BindEmployeeDetails();
                   lblresult.ForeColor = Color.Green;
                   lblresult.Text = txtUsrname.Text + " Details inserted successfully";
               }
               else
               {
                   lblresult.ForeColor = Color.Red;
                   lblresult.Text = txtUsrname.Text + " Details not inserted"; 
               }
               

           }
            

        }

        protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        
}

推荐答案

You can do it in two ways,

1. If it is like drag n drop then, when u get the data keep them in two temp tables and u manipulate(delete from one n add to another) them. After you are done with changes save them to db.

2. If for each change you are saving to db, them delete the row from on table and insert to another.
You can do it in two ways,
1. If it is like drag n drop then, when u get the data keep them in two temp tables and u manipulate(delete from one n add to another) them. After you are done with changes save them to db.
2. If for each change you are saving to db, them delete the row from on table and insert to another.


这篇关于将行从一个表移到另一个表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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