1

I have this:

NpgsqlConnection c = new NpgsqlConnection(conx.getConexion());
c.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter("Insert into \"Marca\" (\"NombreMarca\") values ('" + cbMarca.Text + "')", c);
c.Close();

And the table is:

CREATE SEQUENCE idmarca
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 2147483647
  START 10
  CACHE 1;

CREATE TABLE "Marca"
( "idMarca" integer NOT NULL DEFAULT nextval('idmarca'::regclass),
  "NombreMarca" character varying(100) NOT NULL,
  CONSTRAINT "PKMarca" PRIMARY KEY ("idMarca" ),
  CONSTRAINT "UNombreMarca" UNIQUE ("NombreMarca" )
);

The problem is, when I try to insert into the table, for some reason nothing happens. That's on a try - catch, so no exceptions are being generated from the query. cbMarca is a combobox. The connection with the database is already tested.

Also, this works:

cbMarcaB.Items.Clear();
DataTable dt = cons.Select("Select \"NombreMarca\" From \"Marca\" Order by \"NombreMarca\"");
for (int i = 0; i < dt.Rows.Count; i++)
     {
         cbMarcaB.Items.Add(dt.Rows[i]["NombreMarca"]);
     }

So I don't know what the problem is ...

1
  • Have you checked resulting query string? Commented Feb 24, 2012 at 2:25

1 Answer 1

2

I don't think you should be using the NpgsqlDataAdapter to do an insert rather use the NpgsqlCommand see the example below found here.

string insertQuery = "INSERT INTO npdata VALUES (@key, @ndata)";
NpAdapter.InsertCommand = new NpgsqlCommand(insertQuery, conn);

NpParam = NpAdapter.InsertCommand.Parameters.Add("@key", NpgsqlTypes.NpgsqlDbType.Text);
NpParam.SourceColumn = "key";
NpParam.SourceVersion = DataRowVersion.Current;

NpParam = NpAdapter.InsertCommand.Parameters.Add("@ndata", NpgsqlTypes.NpgsqlDbType.Bigint);
NpParam.SourceVersion = DataRowVersion.Current;
NpParam.SourceColumn = "ndata";
Sign up to request clarification or add additional context in comments.

2 Comments

I saw that, but theres no NpAdapter in the library. Thats why i was using DataAdapter. What reference do i have to import to be able to use the Adapter?
Sorry, at the top of his code, private NpgsqlDataAdapter NpAdapter;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.