我想在属性表中获取attributeName enteres。属性表包含AttributeID,AttributeID,FactorID和Range等列。 [英] I want to get the attributeName enteres in attribute table. The attribute table contains columns like AttributeID, AttributeID, FactorID, and Range.

查看:114
本文介绍了我想在属性表中获取attributeName enteres。属性表包含AttributeID,AttributeID,FactorID和Range等列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CauseFactor表和属性表之间有一对多的关系。

我的部分代码是:

There is one to many relation between CauseFactor table and Attribute table.
My code for the portion is:

private void cbFactorSelect_SelectedIndexChanged(object sender, EventArgs e)
{
   MedicalEntities2 db = new MedicalEntities2();
   CauseFactor selFactor = cbFactorSelect.SelectedItem as CauseFactor;
   txtbxSelectedFactor.Text = selFactor.FactorName;
}

private void btnAddAttribute_Click(object sender, EventArgs e)
{
   MedicalEntities2 db = new MedicalEntities2();
   try
   {
       if(db.Attributes.Any(s => s.AttributeName.Contains(tbAttribute.Text)))
       {
           txtAttributeForFactor.Text = "Attribute already exists in the list";
       }
       else
       {
           txtAttributeForFactor.Text = " ";
           var id = ((CauseFactor)cbFactorSelect.SelectedItem).FactorID;
           Attribute objAttributeName = new Attribute();
           objAttributeName.AttributeName = tbAttribute.Text;
           objAttributeName.FactorID = id;
           db.Attributes.Add(objAttributeName);
           db.SaveChanges();
           PopulateAttributeList();
       }
   }
   catch(Exception)
   {
       txtAttributeForFactor.Text = "Unable to insert record";
   }
}

public void PopulateAttributeList()
{
   MedicalEntities2 db = new MedicalEntities2();
   var attributes = db.Attributes.Where(s => s.CauseFactor.FactorName.Equals(txtbxSelectedFactor.Text));
   clbAttributes.DataSource = attributes.ToList();
   clbAttributes.DisplayMember = "AttributeName";
   clbAttributes.ValueMember = "AttributeID";
}



每次运行程序并尝试输入属性名称时,都会执行异常块。请帮我解释一下我可以使用attributeName和相应的FactorID填充Attribute表。


Everytime I run the program and try to enter the attribute name, exception block executes. Please help me out the way in which I can populate the Attribute table with the attributeName and corresponding FactorID.

推荐答案

首先查看异常是什么:目前你正在捕捉所有异常,但实际上并没有看到它们,所以你不知道插入失败的原因。
所以在 catch 语句,并使用调试器来查看它的内容:

Start by looking at what the exception is: at the moment you are catching all exceptions, but not actually looking at them, so you don't know why the insert fails.
So add an exception object to the catch statement, and use the debugger to look at it's content:
catch(Exception ex)

还要检查任何InnerException数据。

这应该可以让你知道问题可能是什么 - 但是如果没有这些信息你就会猜测。

Also check any InnerException data.
That should give you an idea what the problem might be - but without that info you would just be guessing.

这篇关于我想在属性表中获取attributeName enteres。属性表包含AttributeID,AttributeID,FactorID和Range等列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆