I'm having troubles right now, I can't 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 throws exception: "can't cast object from System.Int32 to System.String type"
我现在遇到了麻烦,我无法更新DBDataContext中的表格X,所以这是我的片段,顺便说一下我只更新了一个表它的工作方式!但是当我插入然后更新时,它抛出异常:“无法将对象从System.Int32强制转换为System.String类型”
{
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();
}
}
}
}
I'm getting this stack from debugger when I perform update :
我在执行更新时从调试器获取此堆栈:
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
3 个解决方案
#1
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.
经过大量的研究,我发现这个bug只存在于.net 3.5 SP1的WinXP-Machines上。在Windows 7上它工作正常。不幸的是,同一框架有两种不同的构建。
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
有一个来自microsoft的修补程序,它解决了我在xp机器上的问题。 http://support.microsoft.com/hotfix/KBHotfix.aspx?kbln=ja&kbnum=963657
Regards, Florian
#2
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.
LINQ to Sql不支持外键到唯一键约束(即备用键)。我有同样的问题,发现阅读一些博客。我认为这不是一个错误。
Non-Primary Keys and LINQ to SQL Problems (is this a bug)?
非主键和LINQ to SQL问题(这是一个错误)?
#3
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.
将DMBL文件作为XML文件打开。检查每列的类型,看看应该是int的东西是否真的是一个字符串。
#1
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.
经过大量的研究,我发现这个bug只存在于.net 3.5 SP1的WinXP-Machines上。在Windows 7上它工作正常。不幸的是,同一框架有两种不同的构建。
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
有一个来自microsoft的修补程序,它解决了我在xp机器上的问题。 http://support.microsoft.com/hotfix/KBHotfix.aspx?kbln=ja&kbnum=963657
Regards, Florian
#2
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.
LINQ to Sql不支持外键到唯一键约束(即备用键)。我有同样的问题,发现阅读一些博客。我认为这不是一个错误。
Non-Primary Keys and LINQ to SQL Problems (is this a bug)?
非主键和LINQ to SQL问题(这是一个错误)?
#3
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.
将DMBL文件作为XML文件打开。检查每列的类型,看看应该是int的东西是否真的是一个字符串。