locked
Create new record in another table RRS feed

  • Question

  • Senary have the following two tables STUDENT and HISTORICOESCOLAR, there is a very relationship-to-one. It turns out that when the year ends, you must update the student's education, do it across the table year, however need to keep the school updated historiography, when I change the school year in student table should be created a new record in the table HISTORICOESCOLAR. For this I did the code below, the first part of the code responsible for making and updating the table HISTORICOESCOLAR if the school name and school year are the same table STUDENT, that part is correct, the second part of the code, after else is responsible for creating a new record if it does not find the table HISTORICOESCOLAR the school name and school year, however this condition ever happens when I get the following message: a value is required. I know this is happening because I am not able to pass the field NomeAluno in Visual Studio LightSwitch shows the following error "Can not implicitly convert type 'string' to 'LightSwitchAplication.Aluno."

    The field is NomeAluno STUDENT table and type string and must be passed to the field AlunoNome LightSwitchApplication.Aluno is the type and the table is HISTORICOESCOLAR field is a relationship between the tables.

    Could anyone help me on this issue?

    partial void Alunoes_Updating(Aluno entity)
            {
                foreach (HistoricoEscolar detalhe in entity.HistoricoEscolars)
                {
                    if (detalhe.AnoEscolar == entity.AnoEscolar && detalhe.EscolaAcademica == entity.EscolaAcademica)
                    {
                        detalhe.HoraInicioAula = entity.HoraAulaInio;
                        detalhe.HoraFimAula = entity.HoraAulaFim;
                        detalhe.AnoEscolar = entity.AnoEscolar;
                        detalhe.EscolaAcademica = entity.EscolaAcademica;
                        detalhe.NivelEnsino = entity.NivelEnsino;
                        detalhe.Sitaucao = entity.Sitaucao;
                    }
                    else
                    {
                        HistoricoEscolar novo = new HistoricoEscolar();
                        novo.HoraInicioAula = entity.HoraAulaInio;
                        novo.HoraFimAula = entity.HoraAulaFim;
                        novo.AnoEscolar = entity.AnoEscolar;
                        novo.EscolaAcademica = entity.EscolaAcademica;
                        novo.NivelEnsino = entity.NivelEnsino;
                        novo.Sitaucao = entity.Sitaucao;
                        novo.AlunoNome = entity.NomeAluno;
                        DataWorkspace.ApplicationData.HistoricoEscolars.AddNew();
                    }
                }


    Monday, April 1, 2013 4:00 AM

Answers

  • OK, this is what you are missing.  Replace the oAddNew.AnoEscolar = entity.AnoEscolar with this:

    oAddNew.Alunoe = entity

    You need to use an entity instead of the key field.  Because entity is is what is already there, you probably can use that instead of doing a query like I will show below.

    So you do not use the foreign key directly.

    If this were in a button that updated something on a selected record, you might use code similar to the following.  This is from my code, so it is my table names.

    RecordReportsLogs.SelectedItem.RecordReportLogStatus1 = (From tstat In Me.DataWorkspace.ATGRecordsReportingData.RecordReportLogStatuses _
                               Where tstat.RecordReportLogStatusID = 2).SingleOrDefault
    The part to look at is the part that begins with "(From".  The right side of the equals must return an entity, not the foreign key.

    Would someone please turn on the LIGHT?

    • Proposed as answer by JoeB_LS103 Tuesday, April 2, 2013 5:26 PM
    • Marked as answer by Angie Xu Wednesday, April 24, 2013 7:55 AM
    Monday, April 1, 2013 9:24 PM

All replies

  • I think your problem is that you are simply not doing the adding of a record in the right place:

    It should be something like this (obviously not tested... but hopefully you get the idea):

                                                                                                                           

    Dim oAddNew As HistoricoEscolar
    oAddNew = HistoricoEscolars.AddNew()
    oAddNew.HoraInicioAula = entity.HoraAulaInio;
    oAddNew.HoraInicioAula = entity.HoraAulaInio;
    oAddNew.HoraInicioAula = entity.HoraAulaInio;
    oAddNew.HoraFimAula = entity.HoraAulaFim;
    oAddNew.AnoEscolar = entity.AnoEscolar;
    oAddNew.EscolaAcademica = entity.EscolaAcademica;
    oAddNew.NivelEnsino = entity.NivelEnsino;
    oAddNew.Sitaucao = entity.Sitaucao;
    oAddNew.AlunoNome = entity.NomeAluno;

    Besides that, I'm not following your non-english variable names, so you might need to set references  to related tables differently.  But adding of a record is your first problem.

    Good Luck!

             


    Would someone please turn on the LIGHT?

    Monday, April 1, 2013 6:58 PM
  • From what I could understand from your example we are doing the same thing, but in languages ​​difirentes, my code is in C # and VB is in her.
    The real problem is how to pass the field of relationship NomeAluno STUDENT table to table HISTORICOESCOLAR AlunoNome field because the field is a string NomeAluno now the field is of type AlunoNome LightSwitchApplication.Aluno

    I do not speak English using Google Translate to translate from Portuguese to English.


    Monday, April 1, 2013 9:02 PM
  • OK, this is what you are missing.  Replace the oAddNew.AnoEscolar = entity.AnoEscolar with this:

    oAddNew.Alunoe = entity

    You need to use an entity instead of the key field.  Because entity is is what is already there, you probably can use that instead of doing a query like I will show below.

    So you do not use the foreign key directly.

    If this were in a button that updated something on a selected record, you might use code similar to the following.  This is from my code, so it is my table names.

    RecordReportsLogs.SelectedItem.RecordReportLogStatus1 = (From tstat In Me.DataWorkspace.ATGRecordsReportingData.RecordReportLogStatuses _
                               Where tstat.RecordReportLogStatusID = 2).SingleOrDefault
    The part to look at is the part that begins with "(From".  The right side of the equals must return an entity, not the foreign key.

    Would someone please turn on the LIGHT?

    • Proposed as answer by JoeB_LS103 Tuesday, April 2, 2013 5:26 PM
    • Marked as answer by Angie Xu Wednesday, April 24, 2013 7:55 AM
    Monday, April 1, 2013 9:24 PM