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 9120753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:41:41+00:00 2026-06-17T05:41:41+00:00

How can I add a column to a datatable and add data to each

  • 0

How can I add a column to a datatable and add data to each row based on a condition.
This is what I am trying to do

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["ProductCode"];
            productTitle = items[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"];

        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (dr["ProductCode"].ToString().Equals(productCode))
                {
                    //here I want to add a new column and add data (productTitle) to the column


                }
            }

        }
  • 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-17T05:41:42+00:00Added an answer on June 17, 2026 at 5:41 am
    dba.Fill(dsSql,"Products");
    DataTable dt = dsSql.Tables["Products"];
    
    dt.Columns.Add("ColumnName", typeof(DataType));
    
    if (dr["ProductCode"].ToString().Equals(productCode))
    {
        dr["ColumnName"] = value;    
    }
    

    Further i would extend the code to avoid NullReferenceException

     if (!String.IsNullOrEmpty(dr["ProductCode"]) && dr["ProductCode"].ToString().Equals(productCode))
     {
            dr["ColumnName"] = value;    
     }
    

    http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i insert each time a diffrent row in datatable? The data table
I have the ability to total each column and add the row -- using
I added a column to my datatable and add values to the column based
I have this Google Bar Chart: function drawChart() { var data = new google.visualization.DataTable();
How can I add an Edit column in this GridView dynamically? I have been
I make some table use DataTable. I make this table can filtering data from
Is there a way you can add a column to the Details view of
Can we add a single column of a JTable inside a scroll pane?
How can I add a computed column to a table that already exists? S.O.
What formula would I use in a persisted column so that I can add

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.