Click here to Skip to main content
15,891,033 members
Home / Discussions / C#
   

C#

 
AnswerRe: Salted hash - Storing Salt In password or a seperate field Pin
Paul Conrad18-Sep-08 7:49
professionalPaul Conrad18-Sep-08 7:49 
AnswerRe: Salted hash - Storing Salt In password or a seperate field Pin
Guffa18-Sep-08 9:33
Guffa18-Sep-08 9:33 
QuestionWhere's my (computer's) memory gone? Pin
Rob Philpott18-Sep-08 5:32
Rob Philpott18-Sep-08 5:32 
AnswerRe: Where's my (computer's) memory gone? Pin
Mark Salsbery18-Sep-08 6:41
Mark Salsbery18-Sep-08 6:41 
AnswerRe: Where's my (computer's) memory gone? Pin
Alan N18-Sep-08 6:52
Alan N18-Sep-08 6:52 
GeneralRe: Where's my (computer's) memory gone? Pin
Rob Philpott18-Sep-08 6:58
Rob Philpott18-Sep-08 6:58 
Questioninsight on data recording to msaccess Pin
djjedi18-Sep-08 5:16
djjedi18-Sep-08 5:16 
QuestionHow to Compare the two database tables(table1,Table2) and update the difference into (table2) using c# Pin
bruze18-Sep-08 5:08
bruze18-Sep-08 5:08 
Can any one Help

I am trying Compare the two database tables(table1,Table2) and update the difference into (table2) using c#.

I have Compared two tables and got the difference value.

How to update the difference value into table2 particular row?




Student.aspx
*************
In aspx page I have added the three Gridview

Student.aspx.cs
********************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Student : System.Web.UI.Page
{
    Comapre objComapre = new Comapre();
    protected void Page_Load(object sender, EventArgs e)
    {
      DataTable First=new DataTable();
      DataTable Second = new DataTable();
      DataTable Compare = new DataTable();

      First = objComapre.GetStudentTableOne();
      Second = objComapre.GetStudentTableTwo();
      Compare=compareDataTables(First, Second);

      GridView1.DataSource = First;
      GridView1.DataBind();
      GridView2.DataSource = Second;
      GridView2.DataBind();
      GridView3.DataSource = Compare;
      GridView3.DataBind();
    }

    public DataTable compareDataTables(DataTable First, DataTable Second)
    {
        First.TableName = "FirstTable";
        Second.TableName = "SecondTable";

        //Create Empty Table
        DataTable table = new DataTable("Difference");

        try
        {
            //Must use a Dataset to make use of a DataRelation object
            using (DataSet ds = new DataSet())
            {
                //Add tables
                ds.Tables.AddRange(new DataTable[] { First.Copy(), Second.Copy() });

                //Get Columns for DataRelation
                DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];

                for (int i = 0; i < firstcolumns.Length; i++)
                {
                    firstcolumns[i] = ds.Tables[0].Columns[i];
                }

                DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

                for (int i = 0; i < secondcolumns.Length; i++)
                {
                    secondcolumns[i] = ds.Tables[1].Columns[i];
                }

                //Create DataRelation
                DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

                ds.Relations.Add(r);

                //Create columns for return table
                for (int i = 0; i < First.Columns.Count; i++)
                {
                    table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
                }

                //If First Row not in Second, Add to return table.
                table.BeginLoadData();

                foreach (DataRow parentrow in ds.Tables[0].Rows)
                {
                    DataRow[] childrows = parentrow.GetChildRows(r);
                    if (childrows == null || childrows.Length == 0)
                        table.LoadDataRow(parentrow.ItemArray, true);
                }

                table.EndLoadData();

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return table;
    }
}


Comapre.cs
***********

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Comapre
/// </summary>
public class Comapre
{
    //Get Connection string from Web.config file
    string strConnection = ConfigurationManager.ConnectionStrings["MystudentConnString"].ConnectionString.ToString().Trim();

    DataTable dsGetStudentOne = new DataTable();
    DataTable dsGetStudentTwo = new DataTable();


    public DataTable GetStudentTableOne()
    {
        try
        {
            string GetConnection = strConnection;
            SqlConnection strConnectionString = new SqlConnection();
            strConnectionString.ConnectionString = GetConnection;
            strConnectionString.Open();
            //Executing Stored Procedure 
            SqlCommand cmdsearch = new SqlCommand("SP_GetStudent_1", strConnectionString);
            cmdsearch.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter daAdapter = new SqlDataAdapter(cmdsearch);
            daAdapter.Fill(dsGetStudentOne);
            strConnectionString.Close();

        }
        catch
        {
        }
        return dsGetStudentOne;

    }

    public DataTable GetStudentTableTwo()
    {
        try
        {
            string GetConnection = strConnection;
            SqlConnection strConnectionString = new SqlConnection();
            strConnectionString.ConnectionString = GetConnection;
            strConnectionString.Open();
            //Executing Stored Procedure 
            SqlCommand cmdsearch = new SqlCommand("SP_GetStudent_2", strConnectionString);
            cmdsearch.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter daAdapter = new SqlDataAdapter(cmdsearch);
            daAdapter.Fill(dsGetStudentTwo);
            strConnectionString.Close();

        }
        catch
        {
        }

        return dsGetStudentTwo;

    }

}



Web.config file
********************
connectionStrings>

add name="MystudentConnString" connectionString="Data Source=Poweredge;User Id=sa;password=sa;Initial Catalog=MyStudent;" providerName="System.Data.SqlClient"/>

/connectionStrings>



Database
Table1
*******

USE [MyStudent]
GO
/****** Object:  Table [dbo].[Student_1]    Script Date: 09/18/2008 20:35:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student_1](
	[Stname] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Age] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[DateofBirth] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[School] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[College] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


Table2
*******
USE [MyStudent]
GO
/****** Object:  Table [dbo].[Student_2]    Script Date: 09/18/2008 20:37:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Student_2](
	[Stname] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[Age] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[DateofBirth] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[School] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[College] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]



Storeprocedure
SP_GetStudent_1
***************

USE [MyStudent]
GO
/****** Object:  StoredProcedure [dbo].[SP_GetStudent_1]    Script Date: 09/18/2008 20:38:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[SP_GetStudent_1]
as
begin
select * from Student_1 
end


SP_GetStudent_2
***************

USE [MyStudent]
GO
/****** Object:  StoredProcedure [dbo].[SP_GetStudent_2]    Script Date: 09/18/2008 20:39:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[SP_GetStudent_2]
as
begin
select * from Student_2
end




Thanks.
QuestionSetting selected controls programatically. Pin
djjedi18-Sep-08 5:08
djjedi18-Sep-08 5:08 
AnswerRe: Setting selected controls programatically. Pin
Alan N18-Sep-08 7:23
Alan N18-Sep-08 7:23 
Question64-bit Programming Pin
HosamAly18-Sep-08 5:00
HosamAly18-Sep-08 5:00 
AnswerRe: 64-bit Programming Pin
Giorgi Dalakishvili18-Sep-08 5:04
mentorGiorgi Dalakishvili18-Sep-08 5:04 
Questionhow can i increse the size of the picture box with picture? Pin
maifs18-Sep-08 4:18
maifs18-Sep-08 4:18 
AnswerRe: how can i increse the size of the picture box with picture? Pin
Mbah Dhaim18-Sep-08 4:27
Mbah Dhaim18-Sep-08 4:27 
AnswerRe: how can i increse the size of the picture box with picture? Pin
nelsonpaixao18-Sep-08 13:58
nelsonpaixao18-Sep-08 13:58 
Questionhow can i increase the font size which is written in text box.. Pin
maifs18-Sep-08 4:11
maifs18-Sep-08 4:11 
AnswerRe: how can i increase the font size which is written in text box.. Pin
GuruPrasadh18-Sep-08 10:26
GuruPrasadh18-Sep-08 10:26 
Questionc# caching of sql query Pin
swjam18-Sep-08 3:53
swjam18-Sep-08 3:53 
AnswerRe: c# caching of sql query Pin
Simon P Stevens18-Sep-08 4:05
Simon P Stevens18-Sep-08 4:05 
GeneralRe: c# caching of sql query [modified] Pin
Mbah Dhaim18-Sep-08 4:17
Mbah Dhaim18-Sep-08 4:17 
GeneralRe: c# caching of sql query Pin
swjam18-Sep-08 4:32
swjam18-Sep-08 4:32 
GeneralRe: c# caching of sql query Pin
Simon P Stevens18-Sep-08 5:14
Simon P Stevens18-Sep-08 5:14 
QuestionPrinting a datagridview Pin
harcaype18-Sep-08 3:48
harcaype18-Sep-08 3:48 
AnswerRe: Printing a datagridview Pin
Giorgi Dalakishvili18-Sep-08 3:56
mentorGiorgi Dalakishvili18-Sep-08 3:56 
QuestionSearching DataTable Pin
Muammar©18-Sep-08 3:42
Muammar©18-Sep-08 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.