0
 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 = "";

This code is returning null exception for the line: election.ConstituencyId = Convert.ToInt32(from m in db.Elections where (c.ConstituencyName==ddlConstitency.SelectedItem.Text) select m.ConstituencyId);

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 08 '14 at 15:39
  • Well, without much knowledge of LINQ, I would presume this is because your query doesn't return any record, and therefore it is not possible to convert to an Int32, causing an exception... but you should be able to find the source of the problem very easilly when debugging. – Laurent S. Apr 08 '14 at 15:39
  • @Bartdude: not so much _when_ debugging, but rather _if_ debugging. Most people who receive this exception are not using the debugger. – John Saunders Apr 08 '14 at 15:43
  • You don't seem to understand the query syntax. For example, don't you mean `m.ConstituencyName == ddlConstitency.SelectedItem.Text`? Because otherwise, you are not referencing `m` in the query. Are you trying to find the `ConstituencyId` of the row with the name selected in the dropdown? – John Saunders Apr 08 '14 at 15:46
  • is `ddlConstitency` your variable name, or are you missing a "u" in there to make it `ddlConstituency`? ;) – Andy Raddatz Apr 08 '14 at 15:46
  • @john : I know, that's a pitty especially if you consider how much the debugger is a great tool. I've been doing old ASP in VBSCript and PHP in the past, in simple text editors, and God knows how much I would have loved to get a debugger for these at the time. – Laurent S. Apr 08 '14 at 15:49

1 Answers1

0
  where (c.ConstituencyName==ddlConstitency.SelectedItem.Text)

I would bet the problem is here...you probably have a selected value for your dropdownlist, however you have just created that Constituency object...and I don't see any properties being set for that ConstituencyName subsequent to the creation of the object. So, you are comparing a valid value from the dropdownlist to either some default ConstituencyName or to null. The query will fail since there isn't a match - the equals evaluation would always fail.

Brinky
  • 418
  • 3
  • 9