1

I've been searching but I don't know how, and I'm new in this, so if someone can help me, I'll be thankful!

The problem is that i need to insert into a table called Farmaco a lot of things. My table Farmaco:

CREATE TABLE "Farmaco" (
  "idFarmaco" bigint NOT NULL DEFAULT nextval('idfarmaco'::regclass),
  "Nombre" character varying(100) NOT NULL,
  "PrecioReferencial" real NOT NULL,
  "Descripcion" character varying(500),
  "Stock" integer NOT NULL,
  "MinStock" integer,
  "MaxStock" integer,
  "idPresentacion" integer NOT NULL,
  "idTipo" integer NOT NULL,
  "idUnidadDeManejo" integer NOT NULL,
  "idMarca" integer NOT NULL,
  CONSTRAINT "PKFarmaco" PRIMARY KEY ("idFarmaco" ),
  CONSTRAINT "FKFarmaco_Marca" FOREIGN KEY ("idMarca")
      REFERENCES "Marca" ("idMarca") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FKFarmaco_Presentacion" FOREIGN KEY ("idPresentacion")
      REFERENCES "Presentacion" ("idPresentacion") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FKFarmaco_Tipo" FOREIGN KEY ("idTipo")
      REFERENCES "Tipo" ("idTipo") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FKFarmaco_UnidadDeManejo" FOREIGN KEY ("idUnidadDeManejo")
      REFERENCES "UnidadDeManejo" ("idUnidadDeManejo") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "Farmaco"
  OWNER TO "Daniel";

All the id's except the idFarmaco are Foreing Keys of others tables. Those tables just have 2 columns, the id one and the name one. In my program code, those names are listed in comboboxes. What i need is to insert into farmaco all that data selected, and the names, etc.

For example

I want to insert in the table this "Farmaco":

"Nombre": Stack (textbox)
"PrecioReferencial": 150.30 (textbox)
"Descripcion" : spensive (textbox)
"Stock" : 20 (textbox)
"MinStock" : 10 (textbox)
"MaxStock" : 50 (textbox)
"idPresentacion" : Caja (this was selected in the combobox)
"idTipo" : Medicina (this was selected in the combobox)
"idUnidadDeManejo": Unidad (this was selected in the combobox)
"idMarca" : Bayer (this was selected in the combobox)

all those id are stored in the database and im filling those with a query, sorted alphabetically, but now i dont know how to make a query so i can insert the respectives ids with all the text's in the texboxes.

The farther i've get is this:

Insert into "Farmaco"
Select "idPresentacion", "idTipo", "idMarca", "idUnidadDeManejo"
From "Presentacion", "Tipo", "Marca", "UnidadDeManejo"
Where "NombrePresentacion" = 'Here should be the text in the ComboBox cbPresentacion.SelectedItem.ToString()' AND "NombreTipo" = '......

2 Answers 2

1

Are you looking for:

insert into "Farmaco" ("idPresentacion", "idTipo", "idMarca", "idUnidadDeManejo")
values ( cbPresentacion.SelectedItem.ToString(), ... ) 
Sign up to request clarification or add additional context in comments.

1 Comment

nop, cuz in the combobox i have the name, not the id. For example. My table Presentacion has a column idPresentacion and a column NamePresentacion. And since they are sorted, theres no correspondece between the SelectedIndex and the id. I need to compare in the table so i can get the id of that name
0

Assuming you are using a winforms project: you can provide a datasource for a combobox and set the displaymember and the datamember. The displaymember is the member that is displayed as choice in the combobox, the datamember is the member you used behind the scenes who corresponds to the selected displaymember. In your case : create a datasource with the information from your table Presentacion and set idPresentacion as datamember and NamePresentacion as displaymember

1 Comment

Nah, for now im just gonna get every id and make a simple insert, but i know there should be a way to do it in just a insert, comparing and getting every name of the ids.

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.