none
ListBox datasource question - basic

    Question

  • Hello,

    I'm a VBA developer, learning VB in VS. My apologies for such simplistic questions, but I'm a novice in Visual Studio. My project is a Windows Application with an SQL Express "backend".

     

    Is it possible in Visual Studio, as it is in VBA, to use an SQL string to provide the datasource for a ListBox, or must I bind the ListBox?

     

    Further, when binding a ListBox, how do you sort the results? I have bound a ListBox to a dataset, but see no easy way to apply a WHERE clause.

     

    Is it typical to create a query to bind to the ListBox? Are there, as in VBA, a number of different ways to accomplish this?

     

    Is there any benefit to binding vs. not binding?

     

    I'm very experienced in VBA and Access applications, but am a bit lost on the basics of supplying data for the controls in VS. If someone could lay it out simply for me, I'd appreciate it. Thank you.

     

    Sunday, July 01, 2007 4:22 PM

Answers

  •  Q: Is it possible in Visual Studio, as it is in VBA, to use an SQL string to provide the datasource for a ListBox, or must I bind the ListBox?

    A: We cannot to use a SQL string to provide the datasource for a ListBox. We must bind to the ListBox.

     

    Q: Further, when binding a ListBox, how do you sort the results? I have bound a ListBox to a dataset, but see no easy way to apply a WHERE clause.

    A: Use BindingSource as a middle tier between data source (e.g. DataSet) and Control (e.g. ListBox). The BindingSource provides the sort and filter (where) functionalities.

     

    Q: Is it typical to create a query to bind to the ListBox? Are there, as in VBA, a number of different ways to accomplish this?

    A: Use a middle tier data source (e.g. DataSet) instead of a query. Any data source which implements the IEnumerable interface that can be used to bind to a Control (e.g. ListBox).

     

    Q: Is there any benefit to binding vs. not binding?

    A: Binding is simpler than not binding.

     

    For more detailed explanations, refer the link below,

    ADO.NET: http://msdn2.microsoft.com/en-us/library/wzabh8c4(VS.80).aspx

    Data Binding: http://msdn2.microsoft.com/en-us/library/ef2xyb33(VS.80).aspx

     

    Monday, July 02, 2007 7:52 AM

All replies

  • There are several ways of binding data. Since you use the 2.0 version youcan use the SQLDataSource control. Drag the control to you form and mention the connection string, Database, table and selection query amd finally this can be bound to the listbox

    <asp:ListBox ID="ListBox1" DataSourceID="SqlDataSource1" runat="server"></asp:ListBox>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"

    SelectCommand="SELECT [City] FROM [City]"></asp:SqlDataSource>

    Monday, July 02, 2007 5:55 AM
  •  Q: Is it possible in Visual Studio, as it is in VBA, to use an SQL string to provide the datasource for a ListBox, or must I bind the ListBox?

    A: We cannot to use a SQL string to provide the datasource for a ListBox. We must bind to the ListBox.

     

    Q: Further, when binding a ListBox, how do you sort the results? I have bound a ListBox to a dataset, but see no easy way to apply a WHERE clause.

    A: Use BindingSource as a middle tier between data source (e.g. DataSet) and Control (e.g. ListBox). The BindingSource provides the sort and filter (where) functionalities.

     

    Q: Is it typical to create a query to bind to the ListBox? Are there, as in VBA, a number of different ways to accomplish this?

    A: Use a middle tier data source (e.g. DataSet) instead of a query. Any data source which implements the IEnumerable interface that can be used to bind to a Control (e.g. ListBox).

     

    Q: Is there any benefit to binding vs. not binding?

    A: Binding is simpler than not binding.

     

    For more detailed explanations, refer the link below,

    ADO.NET: http://msdn2.microsoft.com/en-us/library/wzabh8c4(VS.80).aspx

    Data Binding: http://msdn2.microsoft.com/en-us/library/ef2xyb33(VS.80).aspx

     

    Monday, July 02, 2007 7:52 AM
  • Thank you very much.

     

    Wednesday, August 15, 2007 3:16 AM