Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
add comment (requires an account with 50 reputation)

3 Answers

up vote 2 down vote accepted

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)
share|improve this answer
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 – user1150440 Feb 27 '12 at 14:47
You try putting the select arguments? – Vinicius Ottoni Feb 27 '12 at 14:51
1  
Your second option worked :) Thanks a ton mate. – user1150440 Feb 27 '12 at 14:54
1  
You're welcome! So, anytime! =) – Vinicius Ottoni Feb 27 '12 at 14:55
add comment (requires an account with 50 reputation)

You have to manually invoke the Select method: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select.aspx

share|improve this answer
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 – user1150440 Feb 27 '12 at 14:46
Is the page name being properly applied? Seems like it may be missing a value, which is causing no results to be returned. – Brian Mains Feb 27 '12 at 14:56
I used Vinicius Ottoni's snippet :) Thanks for you time. – user1150440 Feb 27 '12 at 15:00
add comment (requires an account with 50 reputation)

You can handle the Selected event:

<asp:SqlDataSource OnSelected="usersData_Selected" />

Then in code behind:

protected void usersData_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    string pageHeader = e.ReturnValue[0].PageHeader;
}
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.