473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Used Datalist and No DatA ON THE PAGE

Hi guys,
Just using these small snippet below using Datalist with sorting!
But it appears i can see no Data on the screen when compiled with Visual
Studio .Net!
With WebMatrix its running fine!:)
Whats missing in VStudio .Net?

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Public Class sort_datalist
Inherits System.Web.UI.Page
Protected WithEvents MyDataList As System.Web.UI.WebControls.DataList

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Dim myView As DataView
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
PullCategories()
myView.Sort = "CategoryName"
PopulateCategories()
End If
End Sub
Sub SortByName_Click(ByVal sender As Object, ByVal e As EventArgs)
PullCategories()
myView.Sort = "CategoryName"
PopulateCategories()
End Sub
Sub SortByDescription_Click(ByVal sender As Object, ByVal e As EventArgs)
PullCategories()
myView.Sort = "Description"
PopulateCategories()
End Sub
Sub PullCategories()
Dim myTable As New DataTable
Dim myConn As New
SqlConnection("Server=localhost;Database=Northwind ;integrated security=true;")
Dim myAdapter As New SqlDataAdapter("Select CategoryName,
Description FROM Categories", myConn)
myAdapter.Fill(myTable)
myView = New DataView(myTable)
End Sub
Sub PopulateCategories()
MyDataList.DataSource = myView
MyDataList.DataBind()
End Sub

End Class
--------------------------------
<asp:DataList id="MyDataList" Runat="server" ItemStyle-BorderWidth="1">
<HeaderTemplate>
Sort By<asp:LinkButton ID="SortByName" Runat="server"
OnClick="SortByName_Click">Name</asp:LinkButton> /
<asp:LinkButton ID="SortByDescription" Runat="server"
OnClick="SortByDescription_Click">Description</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>

<%# Container.DataItem("CategoryName") %>
<br>
<%# Container.DataItem("Description") %>
</ItemTemplate>
</asp:DataList>
Nov 18 '05 #1
6 1578
Try declaring your datatable at the class level along with your dataview.
It looks to me like your datatable is going out of scope after
PullCategories.

A dataview has no data of its own, it is only a representation of a
datatable.

Best of luck!

"=?Utf-8?B?UGF0cmljay5PLklnZQ==?="
<Pa*********@discussions.microsoft.com> wrote in
news:23**********************************@microsof t.com:
Hi guys,
Just using these small snippet below using Datalist with sorting!
But it appears i can see no Data on the screen when compiled with
Visual Studio .Net!
With WebMatrix its running fine!:)
Whats missing in VStudio .Net?

Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Public Class sort_datalist
Inherits System.Web.UI.Page
Protected WithEvents MyDataList As
System.Web.UI.WebControls.DataList

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the
Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer 'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Dim myView As DataView
Private Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
If Not IsPostBack Then
PullCategories()
myView.Sort = "CategoryName"
PopulateCategories()
End If
End Sub
Sub SortByName_Click(ByVal sender As Object, ByVal e As EventArgs)
PullCategories()
myView.Sort = "CategoryName"
PopulateCategories()
End Sub
Sub SortByDescription_Click(ByVal sender As Object, ByVal e As
EventArgs)
PullCategories()
myView.Sort = "Description"
PopulateCategories()
End Sub
Sub PullCategories()
Dim myTable As New DataTable
Dim myConn As New
SqlConnection("Server=localhost;Database=Northwind ;integrated
security=true;")
Dim myAdapter As New SqlDataAdapter("Select CategoryName,
Description FROM Categories", myConn)
myAdapter.Fill(myTable)
myView = New DataView(myTable)
End Sub
Sub PopulateCategories()
MyDataList.DataSource = myView
MyDataList.DataBind()
End Sub

End Class
--------------------------------
<asp:DataList id="MyDataList" Runat="server"
ItemStyle-BorderWidth="1"> <HeaderTemplate>
Sort By<asp:LinkButton ID="SortByName" Runat="server"
OnClick="SortByName_Click">Name</asp:LinkButton> /
<asp:LinkButton ID="SortByDescription" Runat="server"
OnClick="SortByDescription_Click">Description</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>

<%# Container.DataItem("CategoryName") %>
<br>
<%# Container.DataItem("Description") %>
</ItemTemplate>
</asp:DataList>


Nov 18 '05 #2
Thx cbDevelopment,
Can you just kindly hint me how i should define it.
Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
From your code:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer 'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Dim myView As DataView
Dim myTable As New DataTable
Private Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
If Not IsPostBack
Remove the "Dim myTable As New DataTable" line from PullCategories.

naija naija <pa*********@crazyjohns.com.au> wrote in
news:e#**************@TK2MSFTNGP15.phx.gbl:
Thx cbDevelopment,
Can you just kindly hint me how i should define it.
Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #4
Hi cbDevelopment,
Tried doing that but still no luck!Its just blank.Guess 'm
missing something on Vsidual Studio.Net
Or is it my Import Namespace?
My html looks like this:-
<asp:DataList id="MyDataList" Runat="server" ItemStyle-BorderWidth="1">
<HeaderTemplate>
Sort By<asp:LinkButton ID="SortByName" Runat="server"
OnClick="SortByName_Click">Name</asp:LinkButton> /
<asp:LinkButton ID="SortByDescription" Runat="server"
OnClick="SortByDescription_Click">Description</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem,"CategoryName") %>
<br>
<%# DataBinder.Eval(Container.DataItem,"Description") %>
</ItemTemplate>
</asp:DataList>
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5
I'm sorry, I missed something obvious.

Some observations:
You should only read from the database once. This happens when
postback=false. I changed the pageLoad method to either get the data or
used the data saved in session.
I declared the dataview as new right off the bat. There is no
compelling need to control its creation as you will use it every trip
through the page. Notice the datatable is not declared as New
immediately, because in one case (non-postback), the datatable is created
in the PullCategories, and in the other (postback), the datatable already
exists in session.
You have to define the table on a view before you define the sort,
so this is always done in the page load.
Each event handler sub has the PullCategories removed from it since
we are getting the data from session.
Here's how I would do it:
(Code copied from original posting; untested)
Public Class sort_datalist
Inherits System.Web.UI.Page

Protected WithEvents MyDataList As System.Web.UI.WebControls.DataList
Dim myView As new DataView
Dim myTable As DataTable

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
PullCategories()
myview.table=mytable
myView.Sort = "CategoryName"
PopulateCategories()

else
mytable=ctype(session("DataTable"),datatable)
myview.table=mytable

End If
End Sub

Sub SortByName_Click(ByVal sender As Object, ByVal e As EventArgs)
myView.Sort = "CategoryName"
PopulateCategories()
End Sub

Sub SortByDescription_Click(ByVal sender As Object, _
ByVal e As EventArgs)
myView.Sort = "Description"
PopulateCategories()
End Sub

Sub PullCategories()
Dim myConn As New
SqlConnection("Server=localhost;Database=Northwind ;integrated
security=true;")
Dim myAdapter As New SqlDataAdapter("Select CategoryName,
Description FROM Categories", myConn)

mytable=new datatable
myAdapter.Fill(myTable)
session("DataTable")=mytable

End Sub

Sub PopulateCategories()

MyDataList.DataSource = myView
MyDataList.DataBind()
End Sub

End Class

Best of luck!

naija naija <pa*********@crazyjohns.com.au> wrote in
news:O8**************@TK2MSFTNGP12.phx.gbl:
Hi cbDevelopment,
Tried doing that but still no luck!Its just blank.Guess 'm
missing something on Vsidual Studio.Net
Or is it my Import Namespace?
My html looks like this:-
<asp:DataList id="MyDataList" Runat="server" ItemStyle-BorderWidth="1">
<HeaderTemplate>
Sort By<asp:LinkButton ID="SortByName" Runat="server"
OnClick="SortByName_Click">Name</asp:LinkButton> /
<asp:LinkButton ID="SortByDescription" Runat="server"
OnClick="SortByDescription_Click">Description</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>

<%# DataBinder.Eval(Container.DataItem,"CategoryName") %>
<br>
<%# DataBinder.Eval(Container.DataItem,"Description") %>
</ItemTemplate>
</asp:DataList>
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #6
Thx cbDevelopment for the new ideas!
But after go throw the code again i added EnableViewState="true" in my
DataList.
And it worked fine.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hardy Wang | last post by:
Hi all; I have a DataList with ItemCreated event like below: private void myList_ItemCreated(object sender , DataListItemEventArgs e) { DataRowView myRowView; DataRow myRow; if (e.Item.DataItem...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
2
by: Allen Davis | last post by:
I have some hierarchical data bound to a series of nested DataLists and DataGrids for which I'd like to be able to provide the end-user some targeted printing capabilities. By that I mean being...
4
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a...
2
by: IGotYourDotNet | last post by:
Can anyone tell me how to find a table within a datalist? I can find the labels and textboxes in the dl and change the formatting based on the data, but I can't find the table in the DL. Can anyone...
5
by: Martman | last post by:
First here is my goal: When a datalist is rendered to a page and you use the <itemtemplate> the datalist automatically prints a <tr> <td> start and end tags. Now this may not be too bad but I...
7
by: tshad | last post by:
Is there a way to move a row in a Datalist up or down without having to re-read the data? I have a datalist which has embedded Datagrids in it. I want to allow the user to move a row up or down...
3
by: Mirek Endys | last post by:
I have DataList as part of DataList item. DataList in DataList. The parent DataList working well including Edit command, that shows Edit template and correctly bind the data into edit template...
1
by: David | last post by:
Hi, This one appears a little strange to me. Here is the scenario. I load a datalist the first time a page is run. The page has a !IsPostback and the datalist gets loaded at this point. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.