Retireve data with SqlDatasource & dynamically manipulate before dispalying

C

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
 
C

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
 
A

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top