Sunday, February 19, 2012

How to filter sqldatasource

Hi,

I am new in framework 2 and I can't find a way to filter the sqldatasource.

I have an sqldatasource control that retrive data from data base-"Select * from myTable"

I set the fiterExpression property-sqlDataSource1.FilterExpression="ID='" + strID + "' " ;

I don't know how to continue from here.If I bound the sqlDataSource1 to a control like gridView it works good and I see the filter oparation. but I want to get the result set in the code and loop threw it like I did with ver 1.1 with sqldataReader:

While sqlDatareader1.Read { myCode ... }

How can I do it with sqlDataSource ?

Thanks,

David

I copied my answer for this question:

If your DataSourceMode of your SQLDataSource stays as default which is DataSet, you can retrieve a dataview object of your SQLDataSource. If DataSourceMode="DataReader" in yourSQLDataSource, you can retrieve a datareader object.

Here is the an sample for dataview:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'DataView
Dim mydataview As System.Data.DataView =CType(SQLDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.DataView)

For Each dr As DataRow In dv.Table.Rows
...

Next

'For a Datareader

Dim myreader as System.Data.SqlClient.SqlDataReader = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.SqlClient.SqlDataReader)

While myreader.reader

...

End While

You can use parameterized query for your datasource's sql selection statement.

For example:(DDL1 ia a dropdownlist)

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Personal %>"
SelectCommand="SELECT [mycol1], [mycol2], [mycol3], [myDate], [firstName] FROM [Table1] WHERE ([firstName=@.firstName)">
<SelectParameters>
<asp:ControlParameter ControlID="DDL1" Name="firstName" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

HTH.

|||hi david,
I think a much simpler way to handle code-behind data operations would be to use the current version of the Data Access Application Block in the Enterprise Library, available at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlib2.asp

IMHO, it's much simpler for the kind of while datareader.read and the stuff we used to do in 1.1. The download comes with quite comprehensive documentation, give it a try.

fendi,|||

Hi Limno,

Thanks for your helpful answer. Now I understand sqlDataSource better.

Have a good week.

David.

|||

Hi fendi,

Thanks for your advice.I'll keep it .

Have a good week.

David

No comments:

Post a Comment