Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:21:44+00:00 2026-06-17T08:21:44+00:00

I added a column to my datatable and add values to the column based

  • 0

I added a column to my datatable and add values to the column based on a condition but the value is not added to the datatable(even though there should be values)

This is part of the code I used

conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;
                                Data Source =" + Server.MapPath("App_Data\\LR Product Database 2000.mdb"));
        conn.Open();

        Dictionary<string, string> items = new Dictionary<string, string>();
        OleDbCommand cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT CODE, TITLE FROM tblProducts";

        OleDbDataReader dbread = cmd.ExecuteReader();

        while (dbread.Read())
        {
            productCode = (string)dbread["CODE"];
            productTitle = (string)dbread["TITLE"];
            System.Diagnostics.Debug.Write(productCode + " ");
            items.Add(productCode, productTitle);
        }

        sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["LRVWebsite"].ToString());
        sqlCon.Open();
        dsSql = new DataSet();
        SqlDataAdapter dba = new SqlDataAdapter(@"SELECT C.CustomerFirstName,C.CustomerLastName, C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", connString);
        dba.Fill(dsSql,"Products");
        DataTable dt = dsSql.Tables["Products"];
        dt.Columns.Add("Title", typeof(string));

        foreach (DataRow dr in dt.Rows)
        {
            if(items.ContainsKey(dr["ProductCode"].ToString()))
            {

                dr["Title"] = items[dr["ProductCode"].ToString()];

            }
        }

How can I add the values to the new column based on the condition.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:21:45+00:00Added an answer on June 17, 2026 at 8:21 am

    You can shorten the first part (+ use using) and use Enumerable.Join:

    DataTable tblAccess = new DataTable();
    using(var con = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source =" + Server.MapPath("App_Data\\LR Product Database 2000.mdb"))
    using(var da = new OleDbDataAdapter("SELECT CODE, TITLE FROM tblProducts", con))
    { 
        da.Fill(tblAccess);
    }
    
    DataTable tblSqlServer = new DataTable();
    using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["LRVWebsite"].ToString()))
    using(var da = new SqlDataAdapter("SELECT C.CustomerFirstName,C.CustomerLastName, C.CustomerCompany,C.CustomerPosition,C.CustomerCountry,C.CustomerProvince,C.CustomerContact,CP.ActionDate,CP.ProductCode,CP.CustomerEmail FROM tblCustomers C INNER JOIN tblCustomerProducts CP ON C.CustomerEmail = CP.CustomerEmail ORDER BY ActionDate DESC", con))
    { 
        da.Fill(tblSqlServer);
    }
    

    Now it’s easy to join both tables with Linq-To-DataSet and get the new title for your sql-server row:

    var both = from rowSql in tblSqlServer.AsEnumerable()
               join rowAcc in tblAccess.AsEnumerable()
               on rowSql.Field<string>("ProductCode") equals rowAcc.Field<string>("ProductCode")
               select new { rowSql = rowSql, newTitle = rowAcc.Field<string>("Title") };
    
    foreach (var x in both)
    {
        x.rowSql.SetField("Title", x.newTitle);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a DataTable object, is there added overhead to looking up a column value
Is there a better way to do this function? Even though i'm doing the
This link states that all columns that are nullable should be added to the
There is a datatable ( source ), and a copy of this datatable is
DataTable dt1 = new DataTable(); //I made this as a backup //add 3 columns
I found this bug while working with a DataTable. I added a primary key
I've added a new column to a database and want to populate it from
am using the application in this link http://www.jeasyui.com/tutorial/app/crud2.php .I have added one more column
I added some columns to a table using rails and rake, but they do
There is a constant change (!) in our database, new columns are often added.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.