Postback code not working properly

matt001

New member
Joined
Nov 9, 2005
Messages
2
Location
Australia
Programming Experience
Beginner
Hi Guys


I have got some asp.net coded in VB, it should work like to what you see here http://www.synnex.com.au/Default.aspx?tabid=27

But some of my code works, could you guys when you have time to go through my code to see whats the problem, as it should work.

Thanks if you can help me out, please email the code back to matthew01@gmail.com or reply to the thread.

-------------

Code:

HTML:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Page Language="VB" Debug="true" %>
<html>
<head>
<title>View Products</title>
<link rel="stylesheet" href="netdemos.css">
</head>
<script language="VB" runat="server" src="fetchData_oledb.vb"/>
<Script runat=server>
Dim rowCount as Integer
Dim Category as String, query as string
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If ispostback =false then
query = "SELECT distinct Category FROM Products;"
typesList.DataSource = fetchReader ( query, "productdb" )
typesList.DataBind ( )
typesList.SelectedIndex = 1
getSubTypes ()
end if
End Sub
 
Protected Sub getSubTypes () 
query = "select distinct SubCategory from Products where Category=" & typesList.SelectedItem.value
subtypesList.DataSource = fetchReader ( query, "productdb" )
subtypesList.DataBind ( )
subtypesList.SelectedIndex = 0
Category = subtypesList.SelectedItem.Value
getFirstPage ()
End Sub
Protected Sub getFirstPage () 
myGrid.VirtualItemCount = countRows ( )
myGrid.CurrentPageIndex = 0
ViewState("topID") = ""
ViewState("endID") = ""
query = "select top " & myGrid.PageSize & " * from Products where SubCategory='" & Category & "' order by ProductID"
bindGrid ( )
End Sub
Protected Sub setPage (src as Object, e as DataGridPageChangedEventArgs) 
if (e.NewPageIndex = myGrid.CurrentPageIndex + 1 ) Then
query = "select top " & myGrid.PageSize & " * from Products where SubCategory='" & Category & "' and ProductID > '" & ViewState ( "endID" ) & "' order by ProductID"
else
query = "select top " & myGrid.PageSize & " * from Products where SubCategory='" & Category & "' and ProductID < '" & ViewState ( "topID" ) & "' order by ProductID desc"
End If
myGrid.CurrentPageIndex = e.NewPageIndex
bindGrid ( )
End Sub
Protected Sub bindGrid ( ) 
myGrid.DataSource = fetchView ( )
myGrid.DataBind ( )
lblTracker.Text = "Page " & ( myGrid.CurrentPageIndex+1 ) & " of " & myGrid.PageCount
End Sub
Function fetchView ( ) as DataView
Dim dataSegment as DataTable = fetchData ( query, "productdb" ).Tables ( 0 )
Dim currentRows as DataRow() = dataSegment.Select ( "","ProductID" )
ViewState ("topID") = currentRows ( 0 ) ( "ProductID" )
ViewState ("endID") = currentRows ( currentRows.Length-1 ) ( "ProductID" )
Dim gridView as DataView = dataSegment.DefaultView
gridView.Sort = "ProductID"
return gridView
End Function
Function countRows ( ) as Integer
Dim myConn as OleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("productdb.mdb" ))
query = "select Count ( * ) from Products where SubCategory='" + Category + "'"
Dim getCount as OleDbCommand = new OleDbCommand ( query, myConn )
myConn.Open ( )
Dim rowCount as Integer = getCount.ExecuteScalar ( )
myConn.Close ( )
return rowCount
End Function
Protected Sub typesList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
getSubTypes ()
End Sub
Protected Sub subtypesList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'Code here
End Sub
</script>
 
<body>
<div class="header"><h3>View Products</h3></div>
<hr size=1 width=90%>
<div align="center">
<form runat="server">
<table width="92%">
<tr>
<td>Select Gear: <asp:dropdownlist id="typesList" datatextfield="Category" onSelectedIndexChanged="typesList_SelectedIndexChanged" autopostback=True runat="server" /></td>
<td>Category: <asp:dropdownlist id="subtypesList" datatextfield="SubCategory" onSelectedIndexChanged="subtypesList_SelectedIndexChanged" autopostback=True runat="server" /></td>
<td align="right" width=30%><br><asp:label id="lblTracker" runat="server" /></td></tr>
</table>
<asp:datagrid id="myGrid" runat="server"
width="90%" cellpadding=4
font-size="8pt"
gridlines="horizontal"
showheader=true
itemstyle-verticalalign="top"
autogeneratecolumns=false
allowpaging=True
allowcustompaging=True
pagesize=20
onPageIndexChanged="setPage">
<pagerstyle
position="topandbottom"
nextpagetext="Next" prevpagetext="Prev"
backcolor="lightsteelblue"
font-bold=True
horizontalalign="right" />
 
<columns>
<asp:boundcolumn headertext="Product Code"
datafield="ProductID" 
itemstyle-forecolor="darkslategray" 
itemstyle-font-size="8pt" />
<asp:boundcolumn headertext="Brand"
datafield="Brand" 
itemstyle-forecolor="darkslategray" 
itemstyle-font-size="8pt" />
<asp:HyperlinkColumn
HeaderText="Product" 
DataTextField="Model" 
SortExpression="ProductID" 
DataNavigateUrlField="ProductID"
DataNavigateUrlFormatString=
"product_details.aspx?id={0}"
/>
<asp:boundcolumn headertext="Price"
datafield="Price"
dataformatstring="{0:c}"
itemstyle-horizontalalign="left" />
</columns>
</asp:datagrid>
</form>
</div>
<hr size=1 width=90%>
 
</body>
</html>
--------------------------
Include file: fetchData_oledb.vb

HTML:
Function fetchReader(ByVal query As String, ByVal db As String) As oleDBDataReader
Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("/sites/add/" + db + ".mdb"))
myConn.Open()
Dim myCmd As OleDbCommand = New OleDbCommand(query, myConn)
Return myCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
 
End Function
Function fetchData(ByVal query As String, ByVal db As String) As DataSet
Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("/sites/add/" + db + ".mdb"))
 
Dim myAdapter As OleDbDataAdapter = New OleDbDataAdapter(query, myConn)
 
Dim myData As Dataset = New DataSet()
myAdapter.Fill(myData)
 
Return myData
End Function
Function fetchScalar(ByVal query As String, ByVal db As String) As Object
Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("matthew/test/" + db + ".mdb"))
 
Dim myCmd As OleDbCommand = New OleDbCommand(query, myConn)
 
myConn.Open()
 
Dim scalar As Object = myCmd.ExecuteScalar()
 
myConn.Close()
 
Return scalar
End Function
 
Back
Top