Home All Groups Group Topic Archive Search About

Retireve data with SqlDatasource & dynamically manipulate before dispalying

Author
19 Jul 2006 4:30 PM
collinsd
Hi

I have a sqlDataSource control which currently populates a gridview.
No problems with that.  However I would like to populate an array with
the data from the sqlDataSource and  then manipulate the data, creating
extra fields based upon the data within each row.  I would then
populate the gridview with the adjusted data from my array.  I assume
this could be done in VB in the on-load method for the page.

However I can see no way of getting the data out of the sqlDataSource
into an array - can anybody advise how this can be done or do I have to
learn what appears to be a tortuous process of connecting to the
database via VB?

Many thanks for any help

DC

Author
20 Jul 2006 12:21 PM
collinsd
Update on above message

I have been able to get the data from the sqlDataSource into an array
but cannot now get the databack onto the gridview so help in this area
would be useful.

Many Thanks

DC

Show quoteHide quote
> Hi
>
> I have a sqlDataSource control which currently populates a gridview.
> No problems with that.  However I would like to populate an array with
> the data from the sqlDataSource and  then manipulate the data, creating
> extra fields based upon the data within each row.  I would then
> populate the gridview with the adjusted data from my array.  I assume
> this could be done in VB in the on-load method for the page.
>
> However I can see no way of getting the data out of the sqlDataSource
> into an array - can anybody advise how this can be done or do I have to
> learn what appears to be a tortuous process of connecting to the
> database via VB?
>
> Many thanks for any help
>
> DC
Author
20 Jul 2006 4:26 PM
Alessandro Zifiglio
hi, try the following code. i just do something basic, like adding some more
additional rows to the existing data
returned by the sqldatasource control. You wanted to add some columns, i
have included a reference on msdn along with the code below. It should help
you move forward.

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net


<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        'The sqlDataSrouce returns a DataView
        ' Currently returning one column with a list of topics from the
topics table
        ' in database MyDatabase1.
        Dim dv As DataView =
SqlDataSource1.Select(DataSourceSelectArguments.Empty)
        'Retrieve the DataTable, which should contain the data from our
sqldatasource
        Dim table As DataTable = dv.Table()
        ' now lets populate DataTable, by adding 5 more extra rows to our
existing data.
        ' you can add some more columns and do some other stuff, but lets
keep it simple
        ' For more info on how to manipulate data in DataView.Table, you can
reference
        ' the following document on msdn.
        '
http://msdn2.microsoft.com/en-us/library/system.data.dataview.table.aspx

        Dim id As Integer
        For id = 1 To 5
            ' start adding rows to our DataTable.
            table.Rows.Add(New Object() {String.Format("New Topic {0}",
id)})
        Next id
        Dim view As DataView = New DataView(table)
        GridView1.DataSource = view
        GridView1.DataBind()
        ' OR just render the values in our DataTable directly into our
webpage
        ' by commenting out the following line.
        'PrintTable(view.Table, "DataTable")

    End Sub 'Page_Load

    Private Sub PrintTable(ByVal table As DataTable, ByVal label As String)
        ' This function prints values in the table or DataView.
        Response.Write("<br />" + label)
        Dim row As DataRow
        Dim column As DataColumn
        For Each row In table.Rows
            For Each column In table.Columns
                Response.Write("<br /> table " & row(column))
            Next column
        Next row
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
            SelectCommand="SELECT [Topic_TopicName] FROM
[MyDatabase1_Topic]">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

    </div>
    </form>
</body>
</html>

<colli***@thewinesociety.com> ha scritto nel messaggio
Show quoteHide quote
news:1153398086.837729.65780@m73g2000cwd.googlegroups.com...
> Update on above message
>
> I have been able to get the data from the sqlDataSource into an array
> but cannot now get the databack onto the gridview so help in this area
> would be useful.
>
> Many Thanks
>
> DC
>
>> Hi
>>
>> I have a sqlDataSource control which currently populates a gridview.
>> No problems with that.  However I would like to populate an array with
>> the data from the sqlDataSource and  then manipulate the data, creating
>> extra fields based upon the data within each row.  I would then
>> populate the gridview with the adjusted data from my array.  I assume
>> this could be done in VB in the on-load method for the page.
>>
>> However I can see no way of getting the data out of the sqlDataSource
>> into an array - can anybody advise how this can be done or do I have to
>> learn what appears to be a tortuous process of connecting to the
>> database via VB?
>>
>> Many thanks for any help
>>
>> DC
>