Used Datalist and No DatA ON THE PAGE

G

Guest

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>
 
C

cbDevelopment

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!
 
C

cbDevelopment

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.
 
N

naija naija

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>
 
C

cbDevelopment

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!
 
N

naija naija

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

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

Top