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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:31:32+00:00 2026-05-27T04:31:32+00:00

I have a DataTable dt with 2 columns. First col (call it CustomerId) is

  • 0

I have a DataTable dt with 2 columns. First col (call it CustomerId) is unique and doesn’t allow nulls. the second one allows nulls and is not unique.

From a method I get a CustomerId and then I would like to either insert a new record if this CustomerId doesn’t exist or increment by 1 what’s in the second column corresponding to that CustomerId if it exists.

I’m not sure how I should approach this. I wrote a select statement (which returns System.Data.DataRow) but I don’t know how to test whether it returned an empty string.

Currently I have:

//I want to insert a new row
if (dt.Select("CustomerId ='" + customerId + "'") == null) //Always true :|
    {
        DataRow dr = dt.NewRow();
        dr["CustomerId"] = customerId; 
    }
  • 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-05-27T04:31:33+00:00Added an answer on May 27, 2026 at 4:31 am

    If the datatable is being populated by a database. I would recommend making the customerid a identity column. That way when you add a new row it will automatically create a new customerid which will be unique and 1 greater than the previous id (depending on how you setup your identity column)

    I would check the row count which is returned from the select statement. Something like

    I would also use string.Format…

    So it would look like this

    var selectStatement = string.Format("CustomerId = {0}", customerId);
    var rows = dt.Select(selectStatement);
    if (rows.Count < 1){
      var dr = dt.NewRow();
      dr["CustomerId"] = customerId; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two DataTables. First is DataTable NameAddressPhones = new DataTable(); with Three columns
I have two datatable , first contain DataTable dtMaterialStatement = new DataTable(); dtMaterialStatement.Columns.Add(new DataColumn(MaterialNo,
I have one DataTable which has four columns such as MileStoneID MileStoneName Percentage SeqNbr
I have one datatable which has four or five columns. I dont know exactly
I have a DataTable with 3 columns (a,b,c) and a docx file with the
I'm using Visual Studio 2005 and have a DataTable with two columns and some
I have a Datatable with Id(guid) and Name(string) columns. I traverse through the data
Let's say I have a DataTable: DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(Country, typeof(string)));
I have DataTable with the following columns: ClientID date numberOfTransactions price ClientID is of
I have an ASP.NET GridView that has four columns. The first three are typical

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.