Tuesday, March 27, 2012

datasource question

im using this datasource to extract values from a database.

i simply want to extract the values for ratingsum and ratingcount to populate variables so as to be checked within a code loop.

how do i do this? in classis asp would be something like <%=rsRecords(RatingSum)%
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
ProviderName="<%$ ConnectionStrings:MyConnectionString.ProviderName %>" SelectCommand="SELECT SUM(rating) As RatingSum, COUNT(*) As RatingCount FROM tbl_Rating WHERE Itemid=100">
</asp:SqlDataSource>

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dv As System.Data.DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.DataView)


If dv.Count > 0 Then
Dim myvalue = dv(0).Item("RatingSum")
Dim myvalue2 = dv(0).Item(1) 'orItem("RatingCount")
Label2.Text = CType(myvalue, Int16) & "*************Count=" & CType(myvalue2, Int16)

Else
Label2.Text = "not data!"

End If


End Sub

This way you can send the value back from your code. In this case from the label.text.

You can also use this section within your select parameter to bind your where clause parameter to a textbox control instead of the hard-coded value.

<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="itemId" PropertyName="Text" Type="int16" />

</SelectParameters>