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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:11:16+00:00 2026-05-15T23:11:16+00:00

I need to open a connection to SQL database and read a subset of

  • 0

I need to open a connection to SQL database and read a subset of a table and either update a record if exists or insert if not found. Having truoble updating

    SqlConnection conn = new SqlConnection(ConnectionStrings.PgenIntranet.SqlClientConnectionString);
                        SqlDataAdapter indicators = new SqlDataAdapter();
                        string sql = "SELECT * FROM BusinessApplications.tbl_WPI_Site_Indicators where Year = '" + year +
            "' and  Month = '" + month + "' and PlantId = " + site.ID;

  indicators.SelectCommand = new SqlCommand(sql, conn);
  SqlCommandBuilder cb = new SqlCommandBuilder(indicators);
  indicators.UpdateCommand = cb.GetUpdateCommand();
  DataSet ds = new DataSet();
  indicators.Fill(ds, "indtable");
  DataTable indtable = ds.Tables["indtable"];
    // this logic not working
       if (indtable.Rows.Count == 0) { indtable.NewRow(); }
        DataRow dr = indtable.NewRow();

    /// not sure how to make this work
      indtable[1]["PlantId"] = site.ID;
      dr["PlantId"] = site.ID;
  • 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-15T23:11:16+00:00Added an answer on May 15, 2026 at 11:11 pm

    It’s been a while since I’ve used DataSets/DataTables/DataRows, but I think you’re close. If I remember correctly, you’ll need to create a new row object like you do here:

    DataRow dr = indtable.NewRow();
    

    Then populate that row with your data, also similar to how you were doing it:

    dr["PlantId"] = site.ID;
    

    Then finally add that row to the rows collection in the DataTable. (You’ll want to double-check if your DataTable instance is actually the DataTable in the Tables collection on the DataSet, I don’t recall the specifics. It may be safer to reference the Tables collection directly rather than put it in its own object.)

    Note that this does not add the row back to the database. It just adds it to the DataTable instance. You’ll need to update back to the database accordingly. And this setup that you have here is a bit… messy… so I have no quick answer for that. Since you’re just using plain old ADO then I guess you’ll need to create an update command and populate it and run it against the connection accordingly.

    In doing so, please take care to fix SQL injection vulnerabilities like the one you have there 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My understanding is that Perlin noise is designed so all… May 16, 2026 at 5:12 pm
  • Editorial Team
    Editorial Team added an answer Yes - check out the GetVerifiedStatus Method in the new… May 16, 2026 at 5:12 pm
  • Editorial Team
    Editorial Team added an answer You ask about performance, but don't indicate what should perform,… May 16, 2026 at 5:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I need to open and read a Dbase file in my ASP.NET application. When
I created a C#.net app that uses dates from a SQL Server 2008 database
For an application we are developing we need to read n rows from a
I have a database running on MS SQL Server 2005 and an ASP.NET 3.5
Receiving Error message when performing Update Statement, but database is being updated. Error: You
I want check my SQL server connection before to connect with DB, and I
I need a way to make a process keep a certain file open forever.
I have an application running in IIS which connects to a SQL Server 2008
I'm writing a server application that's communication with a local sql server. Each client
I'm developing a Windows Mobile WinForm application that uses Sql Server CE 3.1 with

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.