E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Your first VB.net Library
by Charles Carroll

Here is a simple program refactored to use a library.


   filename=/experiments/firstlib/searcher_better.aspx

<Test Script Below>


<%@ trace="true"%>
<%@ Assembly src="searcher_better_lib.vb" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script language="VB" runat="server">
Dim strConn as string="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";" 
Sub Page_Load(Src As Object, E As EventArgs)
    searchagain.visible=false
    If ispostback = false THEN
        Dim strSQL as string
        
        strSQL="select distinct city from publishers"
        lib_db.Helper.FillWebControl(strConn,strSQL,cy)

        strSQL="select distinct state from publishers"
        lib_db.Helper.FillWebControl(strConn,strSQL,st)
        
        strSQL="select distinct zip from publishers"
        lib_db.Helper.FillWebControl(strConn,strSQL,zp)
    
    END IF
End Sub

Sub Search_click(s as object, e as eventargs)
    trace.write("city",cy.selecteditem.text) 
    trace.write("state",st.selecteditem.text) 
    trace.write("zip",zp.selecteditem.text) 
    trace.write("cityinc",cyinclude.checked) 
    trace.write("stateinc",stinclude.checked) 
    trace.write("zipinc",zpinclude.checked) 
    
    DIM strSQL as string
    DIM prefix as string
    
    
    searchcriteria.visible=false
    search.visible=false
    searchagain.visible=true
    searchresults.visible=true
    
    strSQL="select * from publishers where "
    
    If cyinclude.checked=false AND stinclude.checked=false AND zpinclude.checked=false THEN
        message.text="You did not choose any search categories so we cannot Search"
        searchresults.visible=false
        exit sub
    ELSE
        message.text=""
    END IF
    
    IF cyinclude.checked THEN
        strSQL=strSQL & " city='" & cy.selecteditem.text & "' "
        prefix=" AND "
    END IF

    IF stinclude.checked THEN
        strSQL=strSQL & prefix & " state='" & st.selecteditem.text & "' "
        prefix=" AND "
    END IF

    IF zpinclude.checked THEN
        strSQL=strSQL & prefix & " zip='" & zp.selecteditem.text & "' "
    END IF
    
    trace.write("strSQL",strSQL) 

    lib_db.Helper.FillWebControl(strConn,strSQL,searchresults)
END SUB

SUB Searchagain_click(s as object, e as eventargs)
    searchcriteria.visible=true
    searchresults.visible=false
    searchagain.visible=false
    search.visible=true
END SUB
</script>
<html><head>
<title>Searcher</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
<asp:literal id="message" runat="server"/>
<asp:Table id="searchcriteria" runat="server" GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
    <ASP:DropDownList id="cy" datatextfield="city" runat="server"/>
</asp:TableCell>
<asp:TableCell>
    <ASP:checkbox id="cyinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
    <ASP:DropDownList id="st" datatextfield="state" runat="server"/>
</asp:TableCell>
<asp:TableCell>
    <ASP:checkbox id="stinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>Zip</asp:TableCell>
<asp:TableCell>
    <ASP:DropDownList id="zp" datatextfield="zip" runat="server"/>
</asp:TableCell>
<asp:TableCell>
    <ASP:checkbox id="zpinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:button id="search" text="search for data" onclick="search_click" runat="server"/><br>
<asp:button id="searchagain" text="search for data again" onclick="searchagain_click" runat="server"/>

<ASP:DataGrid id="searchresults" runat="server" 
Width="100%" 
BackColor="white" 
BorderColor="black" 
ShowFooter="false" 
CellPadding=3 
CellSpacing="0" 
Font-Name="Verdana" 
Font-Size="8pt" 
Headerstyle-BackColor="lightblue" 
Headerstyle-Font-Size="10pt" 
Headerstyle-Font-Style="bold" 
MaintainState="false" 
/> 

</form>

</body></html>
Here is the library:

filename=/experiments/firstlib/searcher_better_lib.vb

Option Strict Off
Imports System.Data.OLEDB
Imports System.Reflection
<Assembly: AssemblyVersion("0.9.8.0")> 
Namespace lib_db
    Public Class Helper
        public shared sub FillWebControl(strConn as string, strSQL as string, objToFill as object)
            Dim Conn1 as New OLEDBConnection(strConn)
            Dim Cmd1 as New OLEDBCommand(strSQL,Conn1)
            Conn1.Open()
            objToFill.DataSource = Cmd1.ExecuteReader(system.data.CommandBehavior.CloseConnection)
            objToFill.DataBind()    
            Conn1.Close()
        end sub
    end class
end namespace
Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site