views:

395

answers:

3

Hi there!, Im having troubles right now, i cant update a table X in DBDataContext, so this is my snippet, by the way when I Update just one table it works! but when I Insert and then update, It Throw Exception ... "cant cast object from System.Int32 to System.String type"

{
            using (DBDataContext db = new DBDataContext())
            {
                int codigo = Convert.ToInt32(lblNroInforme.Content);
                int sucessfull = 0;


                INFORMEMEDICO varInf = (from i in db.INFORMEMEDICOs
                                        where i.numeroinforme == codigo
                                        select i).SingleOrDefault();


                if (varInf == null)
                {
                    varInf = new INFORMEMEDICO();
                    varInf.codigoclase = Convert.ToInt32(lblCodigoClase.Content.ToString());
                    varInf.codigoestudio = lblCodigoEstudio.Content.ToString();
                    varInf.codigopaciente = Convert.ToInt32(lblCodigoPaciente.Content.ToString());
                    varInf.conclusion = GetText(rtbConclusion);
                    varInf.fechainforme = DateTime.Today;
                    varInf.firmauno = getJPGFromImageControl(firmaUno.Source as BitmapImage);
                    varInf.firmados = getJPGFromImageControl(firmaDos.Source as BitmapImage);
                    varInf.hallazgo = GetText(rtbHallazgo);
                    varInf.horainforme = Convert.ToDateTime(DateTime.Today.ToShortTimeString());
                    varInf.impreso = 0;
                    varInf.medicoinforma = cboTurnoMed.SelectedValue.ToString();
                    varInf.nombreinforme = txtNombreExamen.Text;
                    varInf.numeroinforme = Convert.ToInt32(lblNroInforme.Content.ToString());
                    varInf.tecnica = GetText(rtbTecnica);
                    varInf.turnosocio = lblTurnoMedico.Content.ToString();

                    try
                    {
                        db.INFORMEMEDICOs.InsertOnSubmit(varInf);
                        db.SubmitChanges();
                        sucessfull = 1;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error : " + ex.Message);
                    }
                    if (sucessfull==1)
                    {
                        EXAMENXATENCION varExA = (from ea in db.EXAMENXATENCIONs
                                                  where ea.codigo == codigo
                                                  select ea).SingleOrDefault();
                        varExA.estadoestudio = 'I';
                        db.SubmitChanges();
                    }

                }

            }

        }

Im getting this stack from debugger when I perform udpate :

en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
   en System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
   en System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
   en System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
   en System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   en System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   en System.Data.Linq.DataContext.SubmitChanges()
   en Demo.View.InformeMedico.btnGuardar_Click(Object sender, RoutedEventArgs e) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 430
A: 

Open your DMBL file as an XML file. Check your types for each column, and see if something that should be an int is really a string.

Bramha Ghosh
checked already :(
Angel Escobedo
+2  A: 

LINQ to Sql does not support foreign keys to unique key constraints (i.e. alternate keys). I had the same problem and found reading some blogs. I gess it is not a bug.

Non-Primary Keys and LINQ to SQL Problems (is this a bug)?

jrojo
+1  A: 

After a lot of research, I found out that this bug only exists on WinXP-Machines with .net 3.5 SP1. On Windows 7 it works fine. Unfortunately, there are two different builds of the same framework.

There is a hotfix from microsoft, which resolves my problem on the xp machines. http://support.microsoft.com/hotfix/KBHotfix.aspx?kbln=ja&kbnum=963657

Regards, Florian

FloWi
Thats true, by the way I cant figure it out on Windows Vista Machine 1 year ago, I'll try these days on Windows 7, Keep in contact :)
Angel Escobedo