VotingDemoDataContext db = new VotingDemoDataContext();
var x = (from name in db.Elections select name);
int n = x.Count();
Election election = new Election();
election.ElectionID = n.ToString();
Constituency c=new Constituency();
election.ConstituencyId = Convert.ToInt32(from m in db.Elections where (c.ConstituencyName==ddlConstitency.SelectedItem.Text) select m.ConstituencyId);
election.Date = Convert.ToDateTime(txtDate.Text);
election.ElectionType = ddlEType.SelectedItem.Text;
election.EO = txtEOName.Text;
election.EOPassword = txtEOPassword.Text;
db.Elections.InsertOnSubmit(election);
db.SubmitChanges();
txtEOPassword.Text = "";
txtDate.Text = "";
txtResultDate.Text = "";
ddlConstitency.SelectedIndex = 0;
ddlEType.SelectedIndex = 0;
txtEOName.Text = "";
此代码返回行的唯一空例外:election.ConstituencyId = Convert.ToInt32(from m in db.Elections where (c.ConstituencyName==ddlConstitency.SelectedItem.Text) select m.ConstituencyId);
答案 0 :(得分:0)
where (c.ConstituencyName==ddlConstitency.SelectedItem.Text)
我敢打赌问题就在这里......你的下拉列表中可能有一个选定的值,但是你刚刚创建了该选区对象......而且我没有看到随后为该ConstituencyName设置的任何属性创建对象。因此,您将下拉列表中的有效值与某些默认的ConstituencyName或null进行比较。由于没有匹配,查询将失败 - 等于评估将始终失败。