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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:03:52+00:00 2026-06-10T21:03:52+00:00

I’m trying to use the NpgsqlCommandBuilder to automatically generate SQL commands to perform insert/update/delete

  • 0

I’m trying to use the NpgsqlCommandBuilder to automatically generate SQL commands to perform insert/update/delete operations on a dataset. The dataset will be bound to a datagrid (which I really don’t know how to do yet). So I will not be manually editing the data set. When trying to automatically generate the sql commands, it will throw an exception when calling NpgsqlDataAdaptor.Update()

Here is the schema for the db table:

tblFee
{
    character(4) dest,
    character(4) id,
    character(2) indicator,
    double fee
}
the primary key is (dest, id, indicator)

Here is my code:

string m_strConnection = "Server=192.168.253.20;Port=5432;User Id=alex;Password=asdf;Database=mydb;";
DataSet m_ds = new DataSet("EcnFeeData");
NpgsqlConnection m_conn = new NpgsqlConnection(m_strConnection);
NpgsqlDataAdapter m_dAdapter = new NpgsqlDataAdapter();
m_dAdapter.SelectCommand = new NpgsqlCommand("SELECT * FROM \"tblFee\"", m_conn);
NpgsqlCommandBuilder builder = new NpgsqlCommandBuilder(m_dAdapter);

m_conn.Open();

m_dAdapter.Fill(m_ds, "tblFee");

m_ds.Tables[0].Rows[0]["fee"] = 1;

builder.GetUpdateCommand();

m_dAdapter.Update(m_ds, "tblFee");

This exception will get thrown while the last line is executing:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

The same thing happens when trying to insert a row, only the exception message is different:

Additional information: Update requires a valid InsertCommand when passed DataRow collection with new rows.

The last code example was an attempt to use the NpgsqlCommandBuilder the same way the SqlCommandBuilder is used in the example that is provided by the .NET 4.0 documentation. The Npgsql documentation does not provide an example to do it this way. Is this feature not supported in NpgsqlCommandBuilder? I’d prefer to have the NpgsqlCommandBuilder automatically generate the sql commands, but if it doesn’t work then I’ll have to manually create them.

After following the example in the Npgsql documentation for manually generating the InsertCommand, I was able to get it working, but I could not figure out how to do this for the UpdateCommand. Here is the code I used to create the InsertCommand:

m_dAdapter.InsertCommand = new NpgsqlCommand("insert into \"tblFee\" (dest, id, indicator, fee) values (:a, :b, :c, :d)", m_conn);
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiStringFixedLength));
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("b", DbType.AnsiStringFixedLength));
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("c", DbType.AnsiStringFixedLength));
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("d", DbType.Double));

m_dAdapter.InsertCommand.Parameters[0].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[1].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[2].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[3].Direction = ParameterDirection.Input;

m_dAdapter.InsertCommand.Parameters[0].SourceColumn = "dest";
m_dAdapter.InsertCommand.Parameters[1].SourceColumn = "id";
m_dAdapter.InsertCommand.Parameters[2].SourceColumn = "indicator";
m_dAdapter.InsertCommand.Parameters[3].SourceColumn = "fee";
m_dAdapter.InsertCommand.Connection = m_conn;

Here is my attempt to do the UpdateCommand, which doesn’t work:

m_dAdapter.UpdateCommand = new NpgsqlCommand("update \"tblFee\" set dest = :a, id = :b, indicator = :c, fee = :d where dest = :e and id = :f and indicator = :g");
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("b", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("c", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("d", DbType.Double)); 
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("e", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("f", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("g", DbType.AnsiStringFixedLength));

m_dAdapter.UpdateCommand.Parameters[0].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[1].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[2].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[3].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[4].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[5].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[6].Direction = ParameterDirection.Input;

m_dAdapter.UpdateCommand.Parameters[0].SourceColumn = "dest";
m_dAdapter.UpdateCommand.Parameters[1].SourceColumn = "id";
m_dAdapter.UpdateCommand.Parameters[2].SourceColumn = "indicator";
m_dAdapter.UpdateCommand.Parameters[3].SourceColumn = "fee";
m_dAdapter.UpdateCommand.Parameters[4].SourceColumn = "dest_orig";
m_dAdapter.UpdateCommand.Parameters[5].SourceColumn = "id_orig";
m_dAdapter.UpdateCommand.Parameters[6].SourceColumn = "indicator_orig";
m_dAdapter.UpdateCommand.Connection = m_conn;

Any help would be much appreciated.

Alex

  • 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-10T21:03:53+00:00Added an answer on June 10, 2026 at 9:03 pm

    You can set and manipulate the NpgsqlDataAdapter‘s UpdateCommand just as you have it’s InsertCommand above.

    As well as the documentation at http://npgsql.projects.postgresql.org/docs/api/Npgsql.NpgsqlDataAdapterMembers.html it works analogously to SqlDataAdapter, so the example at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx will be worth a look.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.