none
Problem with inserting data in related tables: RRS feed

  • Question

  • Cheers;


    Turns out I enter some data from a DataGridView to a table in a database in SQL Server, this is my code:

            private void btnGuardar_Click(object sender, EventArgs e)
            {
                using (SqlConnection Connection = new SqlConnection("Data source=ASUS-X455LD; Initial Catalog=FATELARES2012; User Id=sa; Password=123"))
                {
                    Connection.Open();
    
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
    
                        string consultaSql = "INSERT INTO MVNOVEDA (CODIGO, CONCEP, FECFIN,FECINI, VALOR) VALUES (@CODIGO, @CONCEP, @FECFIN, @FECINI, @VALOR)";
                        using (SqlCommand cmd = new SqlCommand(consultaSql, Connection))
                        {                        
                            cmd.Parameters.AddWithValue("@CODIGO", row.Cells["Codigo"].Value.ToString());
                            cmd.Parameters.AddWithValue("@CONCEP", row.Cells["Concepto"].Value.ToString());
                            cmd.Parameters.AddWithValue("@FECFIN", Convert.ToDateTime(row.Cells["FechaFinal"].Value.ToString()));
                            cmd.Parameters.AddWithValue("@FECINI", Convert.ToDateTime(row.Cells["FechaInicial"].Value.ToString()));
                            cmd.Parameters.AddWithValue("@VALOR", row.Cells["Valor"].Value.ToString());
                            cmd.ExecuteNonQuery();
                        }
    
                    }
                }
    
            }

    But when I do, I get the following error to another table of the database this is the image:


    You may need to first obtain CentroConstros CentroCostos code table to let me do the insert?

    This is the design of the database tables:

    CREATE TABLE [dbo].[CENTCOS](
    	[AUXILIAR] [bit] NULL CONSTRAINT [DF_CENTCOS_AUXILIAR]  DEFAULT ((0)),
    	[CAPACIDAD] [numeric](15, 4) NULL CONSTRAINT [DF_CENTCOS_CAPACIDAD]  DEFAULT ((0)),
    	[CODCC] [char](10) NOT NULL CONSTRAINT [DF_CENTCOS_CODCC]  DEFAULT (''),
    	[NITFABRICA] [char](20) NULL CONSTRAINT [DF_CENTCOS_NITFABRICA]  DEFAULT (''),
    	[NOMBRE] [char](60) NULL CONSTRAINT [DF_CENTCOS_NOMBRE]  DEFAULT (''),
    	[STADSINCRO] [bit] NULL,
    	[TIPO] [char](2) NULL,
    PRIMARY KEY CLUSTERED 
    (
    	[CODCC] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object:  Table [dbo].[MTEMPLEA]    Script Date: 21/08/2015 3:26:31 p. m. ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[MTEMPLEA](
    	[ACTIVO] [numeric](1, 0) NULL CONSTRAINT [DF_MTEMPLEA_ACTIVO]  DEFAULT ((0)),
    	[ACUMDIASP] [numeric](2, 0) NULL CONSTRAINT [DF_MTEMPLEA_ACUMDIASP]  DEFAULT ((0)),
    	[ADMITIVO] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_ADMITIVO]  DEFAULT (''),
    	[AFP] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_AFP]  DEFAULT ('0'),
    	[AP1CAUSANT] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_AP1CAUSANT]  DEFAULT (''),
    	[AP2CAUSANT] [char](30) NULL CONSTRAINT [DF_MTEMPLEA_AP2CAUSANT]  DEFAULT (''),
    	[APELLIDO] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_APELLIDO]  DEFAULT (''),
    	[APELLIDO2] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_APELLIDO2]  DEFAULT (''),
    	[APORTEV] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_APORTEV]  DEFAULT ('0'),
    	[APPAT] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_APPAT]  DEFAULT ((0)),
    	[APRENDIZ] [bit] NULL CONSTRAINT [DF_MTEMPLEA_APRENDIZ]  DEFAULT ((0)),
    	[AREA] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_AREA]  DEFAULT (''),
    	[ATEP] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_ATEP]  DEFAULT ('0'),
    	[BANCO] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_BANCO]  DEFAULT ('0'),
    	[BANCOEMP] [char](9) NULL CONSTRAINT [DF_MTEMPLEA_BANCOEMP]  DEFAULT ('0'),
    	[CALLE] [char](60) NULL CONSTRAINT [DF_MTEMPLEA_CALLE]  DEFAULT (''),
    	[CALSUBTR] [bit] NULL CONSTRAINT [DF_MTEMPLEA_CALSUBTR]  DEFAULT ((1)),
    	[CARGO] [char](35) NULL CONSTRAINT [DF_MTEMPLEA_CARGO]  DEFAULT ('0'),
    	[CATEGORISS] [numeric](2, 0) NULL CONSTRAINT [DF_MTEMPLEA_CATEGORISS]  DEFAULT ((0)),
    	[CDCIIU] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_CDCIIU]  DEFAULT ('0'),
    	[CEDULA] [char](15) NULL CONSTRAINT [DF_MTEMPLEA_CEDULA]  DEFAULT (''),
    	[CERTIFICAS] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_CERTIFICAS]  DEFAULT ((0)),
    	[CESANGRAV] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_CESANGRAV]  DEFAULT ((0)),
    	[CESANNOGRA] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_CESANNOGRA]  DEFAULT ((0)),
    	[CESANPAG] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_CESANPAG]  DEFAULT ((0)),
    	[CODCAJA] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_CODCAJA]  DEFAULT ('0'),
    	[CODCARGO] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_CODCARGO]  DEFAULT ('0'),
    	[CODCC] [char](10) NULL CONSTRAINT [DF_MTEMPLEA_CODCC]  DEFAULT ('0'),
    	[CODFONDO] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_CODFONDO]  DEFAULT ('ISS'),
    	[CODIGO] [char](15) NOT NULL CONSTRAINT [DF_MTEMPLEA_CODIGO]  DEFAULT (''),
    	[COLONIA] [char](60) NULL CONSTRAINT [DF_MTEMPLEA_COLONIA]  DEFAULT (''),
    	[COMENTARIO] [text] NOT NULL CONSTRAINT [DF_MTEMPLEA_COMENTARIO]  DEFAULT (''),
    	[COMPFLEXI] [bit] NULL CONSTRAINT [DF_MTEMPLEA_COMPFLEXI]  DEFAULT ((0)),
    	[CONTRATA] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_CONTRATA]  DEFAULT (''),
    	[CP] [char](10) NULL CONSTRAINT [DF_MTEMPLEA_CP]  DEFAULT (''),
    	[CTACTE] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_CTACTE]  DEFAULT (''),
    	[CUOTADIA] [numeric](17, 4) NULL CONSTRAINT [DF_MTEMPLEA_CUOTADIA]  DEFAULT ((0)),
    	[CUOTAFIJA] [numeric](17, 2) NULL CONSTRAINT [DF_MTEMPLEA_CUOTAFIJA]  DEFAULT ((0)),
    	[CURP] [char](50) NULL CONSTRAINT [DF_MTEMPLEA_CURP]  DEFAULT ('0'),
    	[DEPEND] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_DEPEND]  DEFAULT ('0'),
    	[DESTAJO] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DESTAJO]  DEFAULT ((0)),
    	[DETALLE] [char](150) NULL CONSTRAINT [DF_MTEMPLEA_DETALLE]  DEFAULT (''),
    	[DIASLIQ] [numeric](2, 0) NULL CONSTRAINT [DF_MTEMPLEA_DIASLIQ]  DEFAULT ((0)),
    	[DIASTRAB] [numeric](2, 0) NULL CONSTRAINT [DF_MTEMPLEA_DIASTRAB]  DEFAULT ((0)),
    	[DIGCHEQUEO] [numeric](1, 0) NULL CONSTRAINT [DF_MTEMPLEA_DIGCHEQUEO]  DEFAULT ((0)),
    	[DIRECCION] [char](200) NULL CONSTRAINT [DF_MTEMPLEA_DIRECCION]  DEFAULT (''),
    	[DUMY1] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY1]  DEFAULT ((0)),
    	[DUMY2] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY2]  DEFAULT ((0)),
    	[DUMY3] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY3]  DEFAULT ((0)),
    	[DUMY4] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY4]  DEFAULT ((0)),
    	[DUMY5] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY5]  DEFAULT ((0)),
    	[DUMY6] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY6]  DEFAULT ((0)),
    	[DUMY7] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY7]  DEFAULT ((0)),
    	[DUMY8] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY8]  DEFAULT ((0)),
    	[DUMY9] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_DUMY9]  DEFAULT ((0)),
    	[EDOCIV] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_EDOCIV]  DEFAULT ('S'),
    	[EMAIL] [char](200) NULL CONSTRAINT [DF_MTEMPLEA_EMAIL]  DEFAULT (''),
    	[EMBARGO] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_EMBARGO]  DEFAULT ('N'),
    	[ENTIDAD] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_ENTIDAD]  DEFAULT (''),
    	[ESOPERARIO] [bit] NULL CONSTRAINT [DF_MTEMPLEA_ESOPERARIO]  DEFAULT ((0)),
    	[EXCEDENTEP] [bit] NULL CONSTRAINT [DF_MTEMPLEA_EXCEDENTEP]  DEFAULT ((0)),
    	[EXTRANJERO] [bit] NULL CONSTRAINT [DF_MTEMPLEA_EXTRANJERO]  DEFAULT ((0)),
    	[FECFIN] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FECFIN]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FECING] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FECING]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FECINI] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FECINI]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FECNAC] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FECNAC]  DEFAULT ([dbo].[F_FechasinHora](getdate())-(7200)),
    	[FECRETIRO] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FECRETIRO]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FFINNOM] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FFINNOM]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FHTRPENSIO] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FHTRPENSIO]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FINCONT] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FINCONT]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FPAGCESAN] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FPAGCESAN]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FPAGINTCES] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FPAGINTCES]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[FSALIDVAC] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_FSALIDVAC]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[GRUPO] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_GRUPO]  DEFAULT ('0'),
    	[GRUPOAUTOL] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_GRUPOAUTOL]  DEFAULT ('0'),
    	[HORASMES] [numeric](8, 4) NULL CONSTRAINT [DF_MTEMPLEA_HORASMES]  DEFAULT ((240)),
    	[HTRABDIA] [numeric](3, 1) NULL CONSTRAINT [DF_MTEMPLEA_HTRABDIA]  DEFAULT ((0)),
    	[HTRABNOCHE] [numeric](3, 1) NULL CONSTRAINT [DF_MTEMPLEA_HTRABNOCHE]  DEFAULT ((0)),
    	[IBBASDEV] [numeric](1, 0) NULL CONSTRAINT [DF_MTEMPLEA_IBBASDEV]  DEFAULT ((1)),
    	[IDADJUNTOS] [char](50) NULL CONSTRAINT [DF_MTEMPLEA_IDADJUNTOS]  DEFAULT (''),
    	[INTERPAG] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_INTERPAG]  DEFAULT ((0)),
    	[JUBILACION] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_JUBILACION]  DEFAULT ('0'),
    	[LUGNAC] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_LUGNAC]  DEFAULT (''),
    	[MADRE] [char](30) NULL CONSTRAINT [DF_MTEMPLEA_MADRE]  DEFAULT (''),
    	[MUNICIPIO] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_MUNICIPIO]  DEFAULT (''),
    	[NITCAUSANT] [char](15) NULL CONSTRAINT [DF_MTEMPLEA_NITCAUSANT]  DEFAULT (''),
    	[NM1CAUSANT] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_NM1CAUSANT]  DEFAULT (''),
    	[NM2CAUSANT] [char](30) NULL CONSTRAINT [DF_MTEMPLEA_NM2CAUSANT]  DEFAULT (''),
    	[NOMBRE] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_NOMBRE]  DEFAULT (''),
    	[NOMINA] [bit] NULL CONSTRAINT [DF_MTEMPLEA_NOMINA]  DEFAULT ((0)),
    	[NOMITUR] [bit] NULL CONSTRAINT [DF_MTEMPLEA_NOMITUR]  DEFAULT ((0)),
    	[NROHIJOS] [numeric](2, 0) NULL CONSTRAINT [DF_MTEMPLEA_NROHIJOS]  DEFAULT ((0)),
    	[NUMERO] [char](10) NULL CONSTRAINT [DF_MTEMPLEA_NUMERO]  DEFAULT (''),
    	[NVECESSM] [numeric](9, 4) NULL CONSTRAINT [DF_MTEMPLEA_NVECESSM]  DEFAULT ((0)),
    	[PADRE] [char](30) NULL CONSTRAINT [DF_MTEMPLEA_PADRE]  DEFAULT (''),
    	[PAGOSITUAD] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_PAGOSITUAD]  DEFAULT (''),
    	[PASSWORD] [char](15) NULL CONSTRAINT [DF_MTEMPLEA_PASSWORD]  DEFAULT (''),
    	[PENSCOMPAR] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_PENSCOMPAR]  DEFAULT (''),
    	[PENSIONADO] [bit] NULL CONSTRAINT [DF_MTEMPLEA_PENSIONADO]  DEFAULT ((0)),
    	[PERIODOPAG] [numeric](5, 2) NULL CONSTRAINT [DF_MTEMPLEA_PERIODOPAG]  DEFAULT ((7)),
    	[PORCDESC] [numeric](7, 2) NULL CONSTRAINT [DF_MTEMPLEA_PORCDESC]  DEFAULT ((0)),
    	[PORCEMBARG] [numeric](6, 2) NULL CONSTRAINT [DF_MTEMPLEA_PORCEMBARG]  DEFAULT ((0)),
    	[PORCENTDS] [numeric](6, 2) NULL CONSTRAINT [DF_MTEMPLEA_PORCENTDS]  DEFAULT ((12.00)),
    	[PORCENTRS] [numeric](6, 2) NULL CONSTRAINT [DF_MTEMPLEA_PORCENTRS]  DEFAULT ((0)),
    	[PORCFLEXI] [numeric](6, 2) NULL CONSTRAINT [DF_MTEMPLEA_PORCFLEXI]  DEFAULT ((0)),
    	[PREFIJO] [char](10) NULL CONSTRAINT [DF_MTEMPLEA_PREFIJO]  DEFAULT (''),
    	[PRESTA] [bit] NULL CONSTRAINT [DF_MTEMPLEA_PRESTA]  DEFAULT ((0)),
    	[PROMRET] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_PROMRET]  DEFAULT ((0)),
    	[REGIMLAB] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_REGIMLAB]  DEFAULT ('2'),
    	[RESIDEEXTE] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_RESIDEEXTE]  DEFAULT (''),
    	[RETCESAN] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_RETCESAN]  DEFAULT ((0)),
    	[SALFIJO] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_SALFIJO]  DEFAULT ('S'),
    	[SALINICESA] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_SALINICESA]  DEFAULT ((0)),
    	[SALINTEGR] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_SALINTEGR]  DEFAULT ('S'),
    	[SALMIXTO] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_SALMIXTO]  DEFAULT ('N'),
    	[SALPROMA] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_SALPROMA]  DEFAULT ((0)),
    	[SALPROMM] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_SALPROMM]  DEFAULT ((0)),
    	[SALPROMS] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_SALPROMS]  DEFAULT ((0)),
    	[SALPROMT] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_SALPROMT]  DEFAULT ((0)),
    	[SALUD] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_SALUD]  DEFAULT ('0'),
    	[SEXO] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_SEXO]  DEFAULT ('M'),
    	[SSOCIAL] [char](20) NULL CONSTRAINT [DF_MTEMPLEA_SSOCIAL]  DEFAULT (''),
    	[STADSINCRO] [bit] NULL,
    	[SUBTIPOCOT] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_SUBTIPOCOT]  DEFAULT ('1'),
    	[SUCURSAL] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_SUCURSAL]  DEFAULT ('0'),
    	[TARJETISS] [char](15) NULL CONSTRAINT [DF_MTEMPLEA_TARJETISS]  DEFAULT (''),
    	[TELEFONO] [char](15) NULL CONSTRAINT [DF_MTEMPLEA_TELEFONO]  DEFAULT (''),
    	[TIPCONTRA] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_TIPCONTRA]  DEFAULT ('INDEF'),
    	[TIPOCOTIZA] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_TIPOCOTIZA]  DEFAULT ('01'),
    	[TIPOEMBARG] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_TIPOEMBARG]  DEFAULT (''),
    	[TIPOIDCAUS] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_TIPOIDCAUS]  DEFAULT (''),
    	[TIPOIDENT] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_TIPOIDENT]  DEFAULT ('C'),
    	[TIPOPENSIO] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_TIPOPENSIO]  DEFAULT (''),
    	[TPENSIONAD] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_TPENSIONAD]  DEFAULT (''),
    	[TPERPRUEB] [datetime] NULL CONSTRAINT [DF_MTEMPLEA_TPERPRUEB]  DEFAULT ([dbo].[F_FechasinHora](getdate())),
    	[TRF] [char](1) NULL CONSTRAINT [DF_MTEMPLEA_TRF]  DEFAULT ('1'),
    	[TRPENSION] [bit] NULL CONSTRAINT [DF_MTEMPLEA_TRPENSION]  DEFAULT ((0)),
    	[TURNO] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_TURNO]  DEFAULT ('0'),
    	[VALINTED] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_VALINTED]  DEFAULT ((0)),
    	[VALINTVIV] [numeric](15, 2) NULL CONSTRAINT [DF_MTEMPLEA_VALINTVIV]  DEFAULT ((0)),
    	[VALORHORA] [numeric](17, 4) NULL CONSTRAINT [DF_MTEMPLEA_VALORHORA]  DEFAULT ((487.5)),
    	[VALORSDI] [numeric](17, 4) NULL CONSTRAINT [DF_MTEMPLEA_VALORSDI]  DEFAULT ((0)),
    	[NUEVOCC] [nchar](10) NULL CONSTRAINT [DF_MTEMPLEA_NUEVOCC]  DEFAULT (''),
    	[CLASCON] [nchar](10) NULL CONSTRAINT [DF_MTEMPLEA_CLASCON]  DEFAULT (''),
    	[DECLARANTE] [bit] NULL CONSTRAINT [DF_MTEMPLEA_DECLARANTE]  DEFAULT ((0)),
    	[NOMBRE2] [char](50) NULL CONSTRAINT [DF_MTEMPLEA_NOMBRE2]  DEFAULT (''),
    	[PERSOCARGO] [bit] NULL CONSTRAINT [DF_MTEMPLEA_PERSOCARGO]  DEFAULT ((0)),
    	[PORRETE] [numeric](12, 7) NULL CONSTRAINT [DF_MTEMPLEA_PORRETE]  DEFAULT ((0)),
    	[CPTOSALAR] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_CPTOSALAR]  DEFAULT ('0'),
    	[ESCOLARIDA] [char](30) NULL CONSTRAINT [DF_MTEMPLEA_ESCOLARIDA]  DEFAULT (''),
    	[ESPTU] [bit] NULL CONSTRAINT [DF_MTEMPLEA_ESPTU]  DEFAULT ((0)),
    	[ESSINDICAL] [bit] NULL CONSTRAINT [DF_MTEMPLEA_ESSINDICAL]  DEFAULT ((0)),
    	[CODTIPCONT] [char](5) NULL CONSTRAINT [DF_MTEMPLEA_CODTIPCONT]  DEFAULT ('0'),
    	[CELULAR] [char](15) NULL CONSTRAINT [DF_MTEMPLEA_CELULAR]  DEFAULT (''),
    	[CONDTRAB] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_CONDTRAB]  DEFAULT (''),
    	[TIPDOC] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_TIPDOC]  DEFAULT (''),
    	[TIPVINCUL] [char](2) NULL CONSTRAINT [DF_MTEMPLEA_TIPVINCUL]  DEFAULT (''),
    PRIMARY KEY CLUSTERED 
    (
    	[CODIGO] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object:  Table [dbo].[MVNOVEDA]    Script Date: 21/08/2015 3:26:31 p. m. ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[MVNOVEDA](
    	[CODBANCO] [char](15) NULL,
    	[CODCC] [char](10) NULL,
    	[CODIGO] [char](15) NULL,
    	[CONCEP] [char](5) NULL,
    	[DIASPAG] [numeric](5, 2) NULL,
    	[FECFIN] [datetime] NULL,
    	[FECHPAGO] [datetime] NULL,
    	[FECING] [datetime] NULL,
    	[FECINI] [datetime] NULL,
    	[FECMOD] [datetime] NULL,
    	[GRUPO] [char](5) NULL,
    	[IDMVNOVEDA] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    	[INTEGCONT] [bit] NULL,
    	[INTEGRBAN] [bit] NULL,
    	[NROHORAS] [numeric](6, 2) NULL,
    	[NUMCHEQ] [numeric](15, 0) NULL,
    	[PASSWORDIN] [char](20) NULL,
    	[PASSWORDMO] [char](20) NULL,
    	[PERIODO] [char](2) NULL,
    	[REGISTRO] [numeric](15, 0) NULL,
    	[STADSINCRO] [bit] NULL,
    	[VALOR] [numeric](15, 2) NULL,
    	[VLANTERIOR] [numeric](15, 2) NULL,
    	[VLRGRAVA] [numeric](15, 2) NULL,
     CONSTRAINT [PK__MVNOVEDA__BC85512C4DF47A4E] PRIMARY KEY NONCLUSTERED 
    (
    	[IDMVNOVEDA] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    SET ANSI_PADDING OFF
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_CODBANCO]  DEFAULT ('') FOR [CODBANCO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_CODCC]  DEFAULT ('') FOR [CODCC]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_CODIGO]  DEFAULT ('') FOR [CODIGO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_CONCEP]  DEFAULT ('0') FOR [CONCEP]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_DIASPAG]  DEFAULT ((0)) FOR [DIASPAG]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_FECFIN]  DEFAULT ([dbo].[F_FechasinHora](getdate())) FOR [FECFIN]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_FECHPAGO]  DEFAULT ([dbo].[F_FechasinHora](getdate())) FOR [FECHPAGO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_FECING]  DEFAULT ([dbo].[F_FechasinHora](getdate())) FOR [FECING]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_FECINI]  DEFAULT ([dbo].[F_FechasinHora](getdate())) FOR [FECINI]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_FECMOD]  DEFAULT ([dbo].[F_FechasinHora](getdate())) FOR [FECMOD]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_GRUPO]  DEFAULT ('') FOR [GRUPO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_INTEGCONT]  DEFAULT ((0)) FOR [INTEGCONT]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_INTEGRBAN]  DEFAULT ((0)) FOR [INTEGRBAN]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_NROHORAS]  DEFAULT ((0)) FOR [NROHORAS]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_NUMCHEQ]  DEFAULT ((0)) FOR [NUMCHEQ]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_PASSWORDIN]  DEFAULT ('') FOR [PASSWORDIN]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_PASSWORDMO]  DEFAULT ('') FOR [PASSWORDMO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_PERIODO]  DEFAULT ('') FOR [PERIODO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_REGISTRO]  DEFAULT ((0)) FOR [REGISTRO]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_VALOR]  DEFAULT ((0)) FOR [VALOR]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_VLANTERIOR]  DEFAULT ((0)) FOR [VLANTERIOR]
    GO
    ALTER TABLE [dbo].[MVNOVEDA] ADD  CONSTRAINT [DF_MVNOVEDA_VLRGRAVA]  DEFAULT ((0)) FOR [VLRGRAVA]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_CENTCOS] FOREIGN KEY([CODCC])
    REFERENCES [dbo].[CENTCOS] ([CODCC])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_CENTCOS]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTBANEMP] FOREIGN KEY([BANCO])
    REFERENCES [dbo].[MTBANEMP] ([BANCO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTBANEMP]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTBANPRO] FOREIGN KEY([BANCOEMP])
    REFERENCES [dbo].[MTBANPRO] ([BANCO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTBANPRO]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTCAJAS] FOREIGN KEY([CODCAJA])
    REFERENCES [dbo].[MTCAJAS] ([CODCAJA])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTCAJAS]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTCARGOS] FOREIGN KEY([CODCARGO])
    REFERENCES [dbo].[MTCARGOS] ([CODCARGO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTCARGOS]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTDEPEN] FOREIGN KEY([DEPEND])
    REFERENCES [dbo].[MTDEPEN] ([DEPEN])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTDEPEN]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTFONDO] FOREIGN KEY([CODFONDO])
    REFERENCES [dbo].[MTFONDO] ([CODFONDO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTFONDO]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTGAUTOL] FOREIGN KEY([GRUPOAUTOL])
    REFERENCES [dbo].[MTGAUTOL] ([GRUPOAUTOL])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTGAUTOL]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTGRUPO] FOREIGN KEY([GRUPO])
    REFERENCES [dbo].[MTGRUPO] ([GRUPO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTGRUPO]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTSUCUR] FOREIGN KEY([SUCURSAL])
    REFERENCES [dbo].[MTSUCUR] ([SUCURSAL])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTSUCUR]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTTIPCONTR] FOREIGN KEY([CODTIPCONT])
    REFERENCES [dbo].[MTTIPCONTR] ([CODIGO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTTIPCONTR]
    GO
    ALTER TABLE [dbo].[MTEMPLEA]  WITH CHECK ADD  CONSTRAINT [FK_MTEMPLEA_MTTURNOS] FOREIGN KEY([TURNO])
    REFERENCES [dbo].[MTTURNOS] ([TURNO])
    GO
    ALTER TABLE [dbo].[MTEMPLEA] CHECK CONSTRAINT [FK_MTEMPLEA_MTTURNOS]
    GO
    ALTER TABLE [dbo].[MVNOVEDA]  WITH CHECK ADD  CONSTRAINT [FK_MVNOVEDA_CENTCOS] FOREIGN KEY([CODCC])
    REFERENCES [dbo].[CENTCOS] ([CODCC])
    GO
    ALTER TABLE [dbo].[MVNOVEDA] CHECK CONSTRAINT [FK_MVNOVEDA_CENTCOS]
    GO
    ALTER TABLE [dbo].[MVNOVEDA]  WITH CHECK ADD  CONSTRAINT [FK_MVNOVEDA_MTCONCEP] FOREIGN KEY([CONCEP])
    REFERENCES [dbo].[MTCONCEP] ([CONCEP])
    GO
    ALTER TABLE [dbo].[MVNOVEDA] CHECK CONSTRAINT [FK_MVNOVEDA_MTCONCEP]
    GO
    ALTER TABLE [dbo].[MVNOVEDA]  WITH CHECK ADD  CONSTRAINT [FK_MVNOVEDA_MTEMPLEA] FOREIGN KEY([CODIGO])
    REFERENCES [dbo].[MTEMPLEA] ([CODIGO])
    GO
    ALTER TABLE [dbo].[MVNOVEDA] CHECK CONSTRAINT [FK_MVNOVEDA_MTEMPLEA]
    GO
    ALTER TABLE [dbo].[MVNOVEDA]  WITH CHECK ADD  CONSTRAINT [FK_MVNOVEDA_MTGRUPO] FOREIGN KEY([GRUPO])
    REFERENCES [dbo].[MTGRUPO] ([GRUPO])
    GO
    ALTER TABLE [dbo].[MVNOVEDA] CHECK CONSTRAINT [FK_MVNOVEDA_MTGRUPO]
    GO
    Thanks if you can help me.





    Walter Gómez

    • Moved by Kristin Xie Monday, August 24, 2015 8:52 AM move to better forum
    Friday, August 21, 2015 8:42 PM

All replies

  • The error message says that you are having a conflict with the foreign key constraint in the table that contains the foreign key.  I will guess that is a detail table.  The problem could be that you are missing a record in the table which is acting as the master table.  Try adding a record to your main table with a key value that will match whatever the key value is that is causing the conflict.


    Rich P

    Friday, August 21, 2015 10:29 PM
  • It's complaining about the constraint FK_MVNOVEDA_CENTCOS:

    ALTER TABLE [dbo].[MVNOVEDA]  WITH CHECK ADD  CONSTRAINT [FK_MVNOVEDA_CENTCOS] FOREIGN KEY([CODCC])
    REFERENCES [dbo].[CENTCOS] ([CODCC])

    MVNOVEDA has a foreign key CODCC which therefore must not be null and must match an entry in CENTCOS.

    When you look at your insert statement:

    string consultaSql = "INSERT INTO MVNOVEDA (CODIGO, CONCEP, FECFIN,FECINI, VALOR) VALUES (@CODIGO, @CONCEP, @FECFIN, @FECINI, @VALOR)";
    

    You don't have CODCC there at all.

    You need to add CODCC and give it a value which will match an entry in CENTCOS


    Saturday, August 22, 2015 9:41 AM