Jump to content



Photo

C# ListBox to LINQ method help


  • Please log in to reply
3 replies to this topic

#1 Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 09 November 2012 - 23:05

Trying something new here.

I have a form where the user selects an item from the ListBox. It polls the database based on the ID and returns the correct values to the textboxes on the form.

I've tried a few different ways, all of which returns a null value except the ID.

	    // Select Mode
	    private void LstBuildersSelectedIndexChanged(object sender, EventArgs e)
	    {
		    var mId = LstBuilders.SelectedIndex + 1;
		    Accessor.SelectedBuilder(mId, MBuilder, MAddress, MCity, MState, MPhoneNumber, MFaxNumber, MEmailAddress);
		    TxtBuilderName.Text = MBuilder;
		    TxtAddress.Text = MAddress;
		    TxtCity.Text = MCity;
		    TxtState.Text = MState;
		    TxtPhoneNumber.Text = MPhoneNumber;
		    TxtFaxNumber.Text = MFaxNumber;
		    TxtEmailAddress.Text = MEmailAddress;
	    }

Accessor.cs

	    // Selected Builder
	    public static void SelectedBuilder (int mId, string mBuilder, string mAddress, string mCity, string mState, string mPhoneNumber, string mFaxNumber, string mEmailAddress)
	    {
		    var dataConnect = new PxLinqSqlDataContext();
		    return (from b in dataConnect.GetTable<TblBuilders>()
				    where (b.IdBuilder == mId)
				    select // SOMETHING HERE TO MAKE THIS MAGIC WORK
								   mBuilder = b.BName,
								   mAddress = b.BAddress,
								   mCity = b.BCity,
								   mState = b.BState,
								   mPhoneNumber = b.BPhoneNumber,
								   mFaxNumber = b.BFaxNumber,
								   mEmailAddress = b.BEmailAddress)
		    )
	    }



#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 11
  • Joined: 26-October 05

Posted 10 November 2012 - 03:26

You can't do that directly. A LINQ query returns an IEnumerable, i.e. a collection. Here it looks like you're only interested in one result, so you could do:

var table = dataConnect.GetTable<TblBuilders>().FirstOrDefault(t => t.IdBuilder == mId);
if (table != null) {
    mBuilder = table.BName;
    mCity = table.BAddress;
    // etc
}


#3 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 10 November 2012 - 05:25

Alrit, I am bit drunk riht now (Scotch Fridy) i'll tey it taomroorw.

#4 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 11 November 2012 - 23:10

You can't do that directly. A LINQ query returns an IEnumerable, i.e. a collection. Here it looks like you're only interested in one result, so you could do:

var table = dataConnect.GetTable<TblBuilders>().FirstOrDefault(t => t.IdBuilder == mId);
if (table != null) {
	mBuilder = table.BName;
	mCity = table.BAddress;
	// etc
}


That helped out a lot thanks.