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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:21:20+00:00 2026-06-13T22:21:20+00:00

Scenario: Excel file is read and displayed in datagrid. Values in sql server must

  • 0

Scenario:

  1. Excel file is read and displayed in datagrid.
  2. Values in sql server must be updated if excel values are different.
  3. Table in Sql server don’t have primary key

After all these steps, when I am about to update the table, it throws the error saying “Update requires a valid UpdateCommand when passed DataRow collection with modified rows.”

There is no primary key. So I need to use update command. BUt how and what would be in update command? importdata is dictionary where data from excel are stored. PLz help!!! What should I do now? I have No idea….

foreach (DataColumn column in ds.Tables[0].Columns)
{
    string fieldName = column.ColumnName;
    string fieldNameValueE = string.Empty;
    if (importdata.ContainsKey(fieldName))
    {
        fieldNameValueE = importdata[fieldName];
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            string fieldNameValueD = dr[fieldName].ToString();
            if (fieldNameValueD != fieldNameValueE)
            {
                dr[fieldName] = fieldNameValueE;

            }
        }

    }

}

da.Update(ds);
connection.Close();    
  • 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-13T22:21:21+00:00Added an answer on June 13, 2026 at 10:21 pm

    So, let’s say we were dealing with a table that had a primary key:

    CREATE TABLE TableA
    {
        FieldA INT PRIMARY KEY IDENTITY(1, 1),
        FieldB VARCHAR(256) NULL,
        FieldC VARCHAR(256) NOT NULL,
    }
    

    If you were to use the SqlCommandBuilder (which you cannot because you don’t have a primary key), it would build a statement a bit like this:

    UPDATE TableA
        SET FieldB = @p1,
            FieldC = @p2
    WHERE (FieldA = @p3 AND
        ((FieldB IS NULL AND @p4 IS NULL) OR (FieldB = @p5)) AND
        FieldC = @p6)
    

    So, you’re going to need to build an UPDATE statement that’s very similar to do it the way they do. But one thing you need to remember is it’s not just the statement, you also have to add all of the parameters to the command that you build – and that’s going to become pretty cumbersome.

    So, I have two recommendations for you:

    1. Make a primary key on the table.
    2. Issue an ExecuteNonQuery in every iteration of the loop.

    The second recommendation would look like this:

    foreach (DataColumn column in ds.Tables[0].Columns)
    {
        string fieldName = column.ColumnName;
        string fieldNameValueE = string.Empty;
        if (importdata.ContainsKey(fieldName))
        {
            fieldNameValueE = importdata[fieldName];
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string fieldNameValueD = dr[fieldName].ToString();
                if (fieldNameValueD != fieldNameValueE)
                {
                    dr[fieldName] = fieldNameValueE;
                }
    
                var cmd = new SqlCommand(string.Format(
                    "UPDATE importdata SET {0} = {1} WHERE fielda = @fielda AND fieldb = @fieldb ...",
                    fieldName, fieldNameValueE), connectionObject);
                cmd.Parameters.AddWithValue(@fielda, dr["fielda", DataRowVersion.Original]);
                cmd.Parameters.AddWithValue(@fieldb, dr["fieldb", DataRowVersion.Original]);
    
                cmd.ExecuteNonQuery();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a excel file on web server where my website deployed. The scenario,
I am using SSIS to insert a Excel file into a Sql Server Table.
Here's my clients scenario my client wants to put a excel file on server
A stored proc (SQL Server 2008) returns multiple result sets. An Excel (.xls) file
Scenario: I am trying to read an excel file using JExcel and dump the
Scenario: WAMP server, InnoDB Table, auto-increment Unique ID field [ INT(10) ], 100+ concurrent
MY scenario is... I have 6,000 datas in Excel Sheet.. I try to import
I have a SQL Server 2005 database used by several applications. Some users wish
I have a simple Excel file that queries a database when it opens and
I am caught up in a real messy situation. Scenario: I have a Excel

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.