Monday, March 19, 2012

Im tryiong to fill one list from another

I have a databound dropdownlist that returns an integer (listID) I have a listbox that should return all the people on a membership list that aren't on the committee list with the ListID returned by the dropdownlist. The listbos code is:

<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="ListID" DataValueField="ListID"></asp:ListBox>

SqlDataSource2 code is:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mainshipsystemsConnectionString %>" >
</asp:SqlDataSource>

When the user clicks on the button for the dropdownlist to make their list selection I have the following in the code behind:

Protected Sub BtnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSelect.Click
'make visible panel containing code to move members
Dim sListID As Integer = DDLGetList.Text
SqlDataSource2.SelectCommand = "SELECT MemberID, First, Last, Prefix, Suffix FROM Mainship.CE_Members WHERE (MemberID NOT IN (SELECT MemberID FROM Mainship.CE_Emails WHERE (ListID = 1)))"
End Sub

I get the following error:

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ListID'.

I have listID = 1 in the above example for testing purposes.

Diane

The select command does not return a value for ListId. You need to change the select command to something like this

SELECT MemberID,ListID, First, Last, Prefix, Suffix FROM Mainship.CE_Members WHERE|||

ListID isn't a field in CE_Members, so it isn't in the first part of the select statement. It's in the second part, as it's a field of CE_Emails

Protected Sub BtnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSelect.Click
'make visible panel containing code to move members
Dim sListID As Integer = DDLGetList.Text
SqlDataSource2.SelectCommand = "SELECT MemberID, First, Last, Prefix,Suffix FROM Mainship.CE_Members WHERE (MemberID NOT IN(SELECTMemberID FROM Mainship.CE_Emails WHERE (ListID = 1)))"
End Sub

Diane

|||

Diane,

<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="ListID" DataValueField="ListID"></asp:ListBox>

The listbox expects a ListID to be returned.

SELECT MemberID, First, Last, Prefix, Suffix FROM Mainship.CE_Members

The first part of the query defines the fields to be return. ListID is not one of them so ListBox1 will have an error because it can not find ListID

|||

I feel like an idiot.

What you were saying made no sense for a good reason - ListID is only used as a filter for the member list. But I did indeed have the box returning listID. What I want it to return is member name. Of course it didn't work. In my defense I plead the looming tax deadline.

Thank you Ken. It's working now.

Diane

No comments:

Post a Comment