Update one DataTable column values into another using C# and VB.Net in ASP.Net

amar
 
on Oct 20, 2021 07:01 AM
317 Views

How to update one table values into another table

Table 1


ID
Intial Sal
1 a 10
2 b 20
3 c 30
4 a 10

Table 2

Dept Fullname Intial
Acc xyz a
SW abc b
lab aa c

OUTPUT should be like below

ID Fullname Sal
1 xyz 10
2 abc 20
3 aa 30
4 xyz 10

I need c# code not sql query

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 20, 2021 07:31 AM
on Oct 20, 2021 08:31 AM

Hi amar,

Refer below sample.

HTML

<asp:GridView runat="server" ID="gvDetails"></asp:GridView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    DataTable table1 = new DataTable();
    table1.Columns.Add("ID");
    table1.Columns.Add("Intial");
    table1.Columns.Add("Sal");
    table1.Rows.Add(1, "a", 10);
    table1.Rows.Add(2, "b", 20);
    table1.Rows.Add(3, "c", 30);
    table1.Rows.Add(4, "a", 10);

    DataTable table2 = new DataTable();
    table2.Columns.Add("Dept");
    table2.Columns.Add("Fullname");
    table2.Columns.Add("Intial");
    table2.Rows.Add("Acc", "xyz", "a");
    table2.Rows.Add("SW", "abc", "b");
    table2.Rows.Add("lab", "a", "c");

    DataTable tblResult = new DataTable();
    tblResult.Columns.Add("ID");
    tblResult.Columns.Add("Fullname");
    tblResult.Columns.Add("Sal");

    var result = (from tbl1 in table1.AsEnumerable()
                    join tbl2 in table2.AsEnumerable() on tbl1["Intial"] equals tbl2["Intial"]
                    select new
                    {
                        ID = tbl1["Id"],
                        Fullname = tbl2["Fullname"],
                        Sal = tbl1["Sal"]
                    }).ToList();
    foreach (var item in result)
    {
        tblResult.Rows.Add(item.ID, item.Fullname, item.Sal);
    }

    gvDetails.DataSource = tblResult;
    gvDetails.DataBind();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) handles Me.Load
    Dim table1 As DataTable = New DataTable()
    table1.Columns.Add("ID")
    table1.Columns.Add("Intial")
    table1.Columns.Add("Sal")
    table1.Rows.Add(1, "a", 10)
    table1.Rows.Add(2, "b", 20)
    table1.Rows.Add(3, "c", 30)
    table1.Rows.Add(4, "a", 10)
    Dim table2 As DataTable = New DataTable()
    table2.Columns.Add("Dept")
    table2.Columns.Add("Fullname")
    table2.Columns.Add("Intial")
    table2.Rows.Add("Acc", "xyz", "a")
    table2.Rows.Add("SW", "abc", "b")
    table2.Rows.Add("lab", "a", "c")
    Dim tblResult As DataTable = New DataTable()
    tblResult.Columns.Add("ID")
    tblResult.Columns.Add("Fullname")
    tblResult.Columns.Add("Sal")
    Dim result = (From tbl1 In table1.AsEnumerable() _
                  Join tbl2 In table2.AsEnumerable() On tbl1("Intial") Equals tbl2("Intial") _
                  Select New With {Key
                      .ID = tbl1("Id"), Key
                      .Fullname = tbl2("Fullname"), Key
                      .Sal = tbl1("Sal")
                  }).ToList()

    For Each item In result
        tblResult.Rows.Add(item.ID, item.Fullname, item.Sal)
    Next

    gvDetails.DataSource = tblResult
    gvDetails.DataBind()
End Sub

Screenshot