Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opinions & improvements

Status
Not open for further replies.

arcnon

Programmer
Aug 12, 2003
242
US
ok here is my first DOTnet page is there a better way to do this?
Code:
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>

<html>
<head>
</head>
<body>
<script runat="server">

Function GetDataForYear(ByVal year As String)

            Dim MyDataReader As System.Data.SQLClient.SqlDataReader
            Dim MyDataReaderTest As System.Data.SQLClient.SqlDataReader 'work around till I can test it without killing the first entry
            Dim MyCommand As System.Data.SQLClient.SqlCommand
            Dim MyConnection As System.Data.SQLClient.SqlConnection

            MyConnection = New System.Data.SQLClient.SqlConnection("server='ip'; trusted_connection=true; database='newspub'")
            MyConnection.Open()

            MyCommand = New System.Data.SQLClient.SqlCommand
            MyCommand.Connection = MyConnection
            MyCommand.CommandText = "select year from onlinearticles WHERE year='" & year &"'"
            MyDataReader = MyCommand.ExecuteReader()


            if MyDataReader.Read 'read 1 to test for results
                Response.Write("<h2 class=subtitle>News Articles for "& year &"</h2>")
            END IF

            MyDataReader = Nothing
            MyCommand = Nothing
            MyConnection.Close()
            MyConnection = Nothing

         End Function

         Function GetData(ByVal month As String, ByVal year As String)

            Dim MyDataReader As System.Data.SQLClient.SqlDataReader
            Dim MyDataReaderTest As System.Data.SQLClient.SqlDataReader 'work around till I can test it without killing the first entry
            Dim MyCommand As System.Data.SQLClient.SqlCommand
            Dim MyConnection As System.Data.SQLClient.SqlConnection

            MyConnection = New System.Data.SQLClient.SqlConnection("server='ip'; trusted_connection=true; database='newspub'")
            MyConnection.Open()

            MyCommand = New System.Data.SQLClient.SqlCommand
            MyCommand.Connection = MyConnection
            MyCommand.CommandText = "select sort_date,title,link from onlinearticles WHERE month = '" & month & "'AND year='" & year &"'"
            MyDataReader = MyCommand.ExecuteReader()



            if MyDataReader.Read 'read 1 to test for results
            Response.Write("<strong>"& month &"</strong>")
            Response.Write("<dl>")
            Response.Write("<dd>" & MyDataReader.Item("sort_date") &" ")
            Response.Write("<a class=external href=" & MyDataReader.Item("link") & ">")
            Response.Write(MyDataReader.Item("title") & "</a></dd>")

            While MyDataReader.Read
                Response.Write("<dd>" & MyDataReader.Item("sort_date") &" ")
                Response.Write("<a class=external href=" & MyDataReader.Item("link") & ">")
                Response.Write(MyDataReader.Item("title") & "</a></dd>")
            End While
             Response.Write("</dl>")
            END IF

            MyDataReader = Nothing
            MyCommand = Nothing
            MyConnection.Close()
            MyConnection = Nothing

         End Function


         Sub Page_load

         Dim currentMonth as Integer
         Dim currentYear as Integer

         currentYear = DateTime.Now.Year
         currentMonth = 1

         While currentYear >= 1999
             GetDataForYear(currentYear)

             While currentMonth <= 12

                 Dim monthNum as String
                 Select Case currentMonth   ' Must be a primitive data type
                     Case 1
                         monthNum = "January"
                     Case 2
                         monthNum = "Febuary"
                     Case 3
                         monthNum = "March"
                     Case 4
                         monthNum = "April"
                     Case 5
                         monthNum = "May"
                     Case 6
                         monthNum = "June"
                     Case 7
                         monthNum = "July"
                     Case 8
                         monthNum = "August"
                     Case 9
                         monthNum = "September"
                     Case 10
                         monthNum = "October"
                     Case 11
                         monthNum = "November"
                     Case 12
                         monthNum = "December"
                     Case Else

                 End Select

                 GetData(monthNum,currentYear)


                 currentMonth += 1
             End While

             currentMonth = 1

             currentYear -= 1
        End While


    End Sub

</script>
</body>
</html>
 
From an architectural viewpoint, it's not so good to have your database access code in your page. You would typically want it in another assembly which you'd call. But seeing how this is your first page, it's probably OK for now.

Also - shouldn't the variable be named "MonthName", not "MonthNum" ?

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
no I am converting the month to a number hince monthnum
 
One thing you should definately stay away from is Response.Write.

If you need to write things out to a page identify which control would be best suited (e.g. a Literal Control, Repeater, Datagrid etc). Using Response.Write is OK for testing purposes but IMO that is the only situation where it should be used.

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

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Hi,
My 2 cents:

1. Make use of .Net's OOP. Create your own classes to handle things, much easier/simpler.
2. It is a bad idea to put your codes in your page. Unlike ASP where your HTML and codes are on the same page, .Net allows you to separate them. You will realise the strength.
3. Don't use the Response.Write often. Use the built in objects (lables, etc, etc) and put your codes in the Page_Load subroutine --oh, remember the IsPostBack. It is VERY important.
4. You can use monthNum = MonthName( currentMonth, false ) --this line will save you 25% of your code length ;)

regards,
- Joseph
==================


~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 
I agree with fhlee's comments that you can definately make use of another class to handle things such as database connections as this will come in handy throughout your project.

You should also take note of his suggestion to use objects such as labels (or Literal controls) for displaying data as it will prove to be invaluable (using Response.Write really is bad programming/design).

However, it's not necessarily a "bad idea to put your codes in your page". The current version of VS/Framework allows you to create code-behind pages (with intellisense and debugging) however these code-behind pages will be scrapped in favour of a "code-beside" when the next version of VS and the framework is released. Many other editors such as the ASP.NET Web Matrix don't offer the functionailty of code-behind pages (although there are workarounds such as setting the src attribute on the age) so a lot of users favour the inline approach.

Also, the MonthName function originates from older versions of Visual Basic which means it isn't available when programming in C#. An alternative way would be to use the GetMonthName function from the Globalization class e.g. To return "December":
Code:
System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(12)



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

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I dont know anything really about the vb.net framework. I purchased a couple of books and have worked through a few of the examples BUT I haven't been able to find find any sample code that directly relates to what I am trying to accomplish.

If there is a better way... I am all for it. I dont have a clue about how to 'get er done'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top