4

After the SQLDataSource gets the values from the database, I want to be able to use these values in the code behind. Kindly spare 5 minutes and give me some suggestions.

ASPX Markup:

<h2 id="pageHeader" runat="server"></h2>
            <div id="pageContent" runat="server">

            </div> 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:mkkConnectionString %>" 
        SelectCommand="SELECT [PageHeader], [PageContent] FROM [PageKeeper] WHERE ([PageName] = @PageName)">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="AboutUs" Name="PageName" 
                QueryStringField="Page" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Code behind:

Public Sub loadContent(ByVal _PageName As String)
pageHeader.InnerText= "How do i get the value from SQLDatasource ??"
pageContent.InnerHtml="How do i get the value from SQLDatasource ??"
End Sub

3 Answers 3

5

Try this in your code behind:

DataView dview = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView) 

And after, use the DataView to access the columns, if you wanna a a list:

For Each drow As DataRow In dview.Table.Rows 
    pageHeader.InnerText= CType(drow("PageHeader"), String)
Next drow 

Or single:

pageHeader.InnerText= CType(dview.Table.Rows(0)("PageHeader"), String)
Sign up to request clarification or add additional context in comments.

3 Comments

Dim dv As DataView dv = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView) pageHeader.InnerText = CType(dv.Table.Rows(0)(3), String) pageContent.InnerHtml = CType(dv.Table.Rows(0)(4), String) This is what m trying now with your suggestion...but i get there is no row at position 0
You try putting the select arguments?
Doesn't using "select" make a second call to SQL?
2

If you want that in C#, here's the code

First Don't forget

using System.Data;

Then In Event Code :

    DataView dview = (DataView) sdsBookInfo.Select(DataSourceSelectArguments.Empty);
    string value = (String)dview.Table.Rows[0]["YourColumnName"];

Comments

0

I used the following:

DataView dv = (DataView)GetDataSource().Select(DataSourceSelectArguments.Empty);

foreach (DataRow row in dv.Table.Rows)
{
    long id = Convert.ToInt64(row[0]);
    string name = row[1].toString();
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.