Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 1st, 2003, 12:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default Question

I have a table with 3 fields:
ID, news_title, the_news


I want to pull out all stories with a certain title and display them on the left side, the next title in the middle, and the other title on the right. Is this possible? HEre is my code so far:

Function trainingNews()
        Dim objConn As New OleDbConnection(strConnection)
        Dim strSQL As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Training News');"

        ' DataAdapter setup
        Dim objAdapter As New OleDbDataAdapter(strSQL, objConn)

        ' DataSet & Adapter & Table

        objAdapter.Fill(objDataSet, "dtNews")
        strResultsTraining = "<table border=0 cellpadding=1 cellspacing=0>"
        strResultsTraining &= "<tr>"

        strResultsTraining &= "<td>Training News</td></tr><tr>"

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            'rTraining = objDataSet.Tables(0).Rows(0)(0).ToString()
            strResultsTraining &= "<tr><td><a href=#>" & r("the_news") & "</a></td></tr>"
        Next
        strResultsTraining &= "</table><br><br><br>"
        Return strResultsTraining
    End Function

then in the page_load i call the variable 'strResultsTraining'
There are 2 others that I didn't paste because they are the same just different strResults with different SQL statements.

What's happening is the first has the correct information but the middle column has the left column's info plus the middle and the right column has all three. I'm stumped on why this is happening.
 
Old October 1st, 2003, 12:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Try this out...

Function trainingNews()
    Dim objConn As New OleDbConnection(strConnection)
    Dim strSQL As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Training News');"

    ' DataAdapter setup
    Dim objAdapter As New OleDbDataAdapter(strSQL, objConn)

    ' DataSet & Adapter & Table
    objAdapter.Fill(objDataSet, "dtNews")

    Dim objTable As New HtmlTable
    Dim objRow As New HtmlTableRow
    Dim objCell As New HtmlTableCell
    objTable.Border = 0
    objTable.CellPadding = 1
    objTable.CellSpacing = 0
    objCell.InnerText = "Training News"
    objRow.Cells.Add(objCell)
    objTable.Rows.Add(objRow) 'Add the header row to the table
    objRow = New HtmlTableRow 'Create a new row as first data row
    objTable.Rows.Add(objRow) 'Add the first data row to the table
    Dim r As DataRow
    For Each r In objDataSet.Tables("dtNews").Rows
        If objRow.Cells.Count = 3 Then
            objRow = New HtmlTableRow 'Create a new row
            objTable.Rows.Add(objRow) 'Add the row to the table
        End If
        objCell = New HtmlTableCell
        objCell.InnerHtml = String.Format("<a href=#>{0}</a>", r("the_news"))
        objRow.Cells.Add(objCell)
    Next
    Return objTable.InnerHtml
End Function

You should probably put a table with runat="server" on the form, and then just use that table object in this function instead of returning raw HTML. Then you can just change this into a sub.


Peter
 
Old October 1st, 2003, 01:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

The HTML actually turned out right. Let me try to draw what the output is doing.

[u]Training News</u> [u]Current Events</u> [u]Latest News</u>
training article1 training article1 training article1
training article2 training article2 training article2
                             events article1 events article1
                             events article2 events article2
                                                         latestnews article1
                                                 latestnews article2

It should only obtain the news from each news_title and display the stories relating to the title. instead, it's keeping the previous ones and adding to it like I have tried to show above. I thought the WHERE statement would only include what it asked for and not add them all together. Does this make sense?
 
Old October 1st, 2003, 05:38 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'll venture a guess given that I can't see the other calls you are making. I don't see where objDataSet is being instantiated, so perhaps this is the problem. Is it getting cleared before you call objAdapter.Fill(objDataSet,...) the second and third time?

Also, it looks to me like you are opening 2 rows at once:
strResultsTraining &= "<td>Training News</td></tr><tr>"
then again in the for loop.

Peter
 
Old October 2nd, 2003, 07:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Dim objDataSet As New DataSet()
    Dim strConnection As String = ConfigurationSettings.AppSettings("newsdb")

    Dim strResultsTraining As String
    Dim strResultsUpdates As String
    Dim strResultsTips As String
    Dim strResultsProjects As String
    Dim strResultsCategory As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        trainingNews()
        updatesPatches()
        tipsTricks()
        currentProjects()
        otherCat()

        leftSide.InnerHtml = strResultsTraining & strResultsUpdates
        middle1.InnerHtml = strResultsTips
        rightSide.InnerHtml = strResultsProjects & strResultsCategory
    End Sub
    Function trainingNews()
        Dim objConn As New OleDbConnection(strConnection)
        Dim strSQL As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Training News');"

        'DataAdapter(setup)
        Dim objAdapter As New OleDbDataAdapter(strSQL, objConn)

        ' DataSet & Adapter & Table

        objAdapter.Fill(objDataSet, "dtNews")
        strResultsTraining = "<table border=0 cellpadding=1 cellspacing=0>"
        strResultsTraining &= "<tr><td>Training News</td></tr>"

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            strResultsTraining &= "<tr><td><a href=#>" & r("the_news") & "</a></td></tr>"
        Next
        strResultsTraining &= "</table><br><br><br>"
        Return strResultsTraining
    End Function
    Function updatesPatches()
        Dim objConn2 As New OleDbConnection(strConnection)
        Dim strSQL2 As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Updates and Patches');"

        ' DataAdapter setup
        Dim objAdapter As New OleDbDataAdapter(strSQL2, objConn2)

        ' DataSet & Adapter & Table

        objAdapter.Fill(objDataSet, "dtNews")

        strResultsUpdates = "<table border=0 cellpadding=1 cellspacing=0>"
        strResultsUpdates &= "<tr><td>Updates and Patches</td></tr>"

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            strResultsUpdates &= "<tr><td><a href=#>" & r("the_news") & "</a></td></tr>"
        Next
        strResultsUpdates &= "</table><br><br><br>"
        Return strResultsUpdates
        strResultsUpdates = ""
    End Function
    '************************************MIDDLE
    Sub tipsTricks()
        Dim objConn3 As New OleDbConnection(strConnection)
        Dim strSQL3 As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Tips & Tricks');"

        ' DataAdapter setup
        Dim objAdapter As New OleDbDataAdapter(strSQL3, objConn3)

        ' DataSet & Adapter & Table

        objAdapter.Fill(objDataSet, "dtNews")

        strResultsTips = "<table border=0 cellpadding=1 cellspacing=0>"
        strResultsTips &= "<tr><td>Tips and Tricks</td></tr>"

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            strResultsTips &= "<tr><td><a href=#>" & r("the_news") & "</a></td></tr>"
        Next
        strResultsTips &= "</table><br><br>"
        Exit Sub
    End Sub
    '**************************************RIGHT
    Sub currentProjects()
        Dim objConn4 As New OleDbConnection(strConnection)
        Dim strSQL4 As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Current Projects');"

        ' DataAdapter setup
        Dim objAdapter As New OleDbDataAdapter(strSQL4, objConn4)

        ' DataSet & Adapter & Table

        objAdapter.Fill(objDataSet, "dtNews")

        strResultsProjects = "<table border=0 cellpadding=1 cellspacing=0>"
        strResultsProjects &= "<tr><td>Current Projects</td></tr>"

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            strResultsProjects &= "<tr><td><a href=#>" & r("the_news") & "</a></td></tr>"
        Next
        strResultsProjects &= "</table><br><br>"
        Exit Sub
    End Sub
    Sub otherCat()
        Dim objConn4 As New OleDbConnection(strConnection)
        Dim strSQL4 As String = "SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='Other Categories');"

        ' DataAdapter setup
        Dim objAdapter As New OleDbDataAdapter(strSQL4, objConn4)

        ' DataSet & Adapter & Table

        objAdapter.Fill(objDataSet, "dtNews")

        strResultsCategory = "<table border=0 cellpadding=1 cellspacing=0>"
        strResultsCategory &= "<tr><td>Other Category</td></tr>"

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            strResultsCategory &= "<tr><td><a href=#>" & r("the_news") & "</a></td></tr>"
        Next
        strResultsCategory &= "</table><br>"
        Exit Sub
    End Sub


I went ahead and posted all the relevant code. I also added 2 more categories. Now it should appear like this:

  Category1 Category4

                   Category3

  Category2 Category5

objDataSet is at the top so all subs and functions can use it.

Good eye on the extra <tr>. I would never have caught it. That would make your way of coding (the right way with .NET :) ) easier to catch things like that.

Thanks for your help.

Here is my HTML:

<form id="Form1" method="post" runat="server">
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td><div id="leftSide" runat="server">Table Will Go Here</div>
                    </td>
                    <td bgcolor="#a3a3a3" width="1"></td>
                    <td><div id="middle1" runat="server">Table Will Go Here</div>
                    </td>
                    <td bgcolor="#a3a3a3" width="1"></td>
                    <td><div id="rightSide" runat="server">Table Will Go Here</div>
                    </td>
                </tr>
            </table>
        </form>
 
Old October 2nd, 2003, 10:06 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Zoinks!! stu, I think you are missing the purpose of functions. I made a little change to your code, try this out...

[u]VB code</u>:

Dim strConnection As String = ConfigurationSettings.AppSettings("newsdb")

Protected leftSide As System.Web.UI.WebControls.Literal
Protected middle1 As System.Web.UI.WebControls.Literal
Protected rightSide As System.Web.UI.WebControls.Literal

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    leftSide.Text = getNews("Training News", "Training News") & getNews("Updates and Patches", "Updates and Patches")
    middle1.Text = getNews("Tips & Tricks", "Tips and Tricks")
    rightSide.Text = getNews("Current Projects", "Current Projects") & getNews("Other Categories", "Other Category")
End Sub

Private Function getNews(ByVal sNewsTitle As String, ByVal sTitle As String) As String
    Dim objConn As New OleDbConnection(strConnection)
    Dim objDataSet As New DataSet
    Dim strSQL As String
    Dim sResults As New System.Text.StringBuilder
    strSQL = String.Format("SELECT news_title, the_news FROM zNews WHERE (zNews.news_title='{0}');", sTitle)

    'DataAdapter(setup)
    Dim objAdapter As New OleDbDataAdapter(strSQL, objConn)

    ' DataSet & Adapter & Table
    objAdapter.Fill(objDataSet, "dtNews")
    sResults.Append("<table border=0 cellpadding=1 cellspacing=0>")
    sResults.AppendFormat("<tr><td>{0}</td></tr>", sTitle)

    Dim r As DataRow
    For Each r In objDataSet.Tables("dtNews").Rows
        sResults.AppendFormat("<tr><td><a href=#>{0}</a></td></tr>", r("the_news"))
    Next
    sResults.AppendFormat("</table><br><br><br>")
    Return sResults.ToString
End Function
[u]HTML code</u>:

<form id="Form1" method="post" runat="server">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td><asp:Literal id="leftSide" runat="server" /></td>
            <td bgcolor="#a3a3a3" width="1"></td>
            <td><asp:Literal id="middle1" runat="server" /></td>
            <td bgcolor="#a3a3a3" width="1"></td>
            <td><asp:Literal id="rightSide" runat="server" /></td>
        </tr>
    </table>
</form>


I haven't tested this, but I put it together in VS and I didn't get any design time errors, so it should work with few changes. You said that you got your problem figured out, but perhaps this will help solve the original issue.

Peter
 
Old October 2nd, 2003, 10:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Zoinks!! stu, I think you are missing the purpose of functions. I made a little change to your code, try this out...
_________________________


Yeah, I'm trying to teach myself programming and it's hard to learn it without someone to show me.

The CS department where I went to college didn't teach anything. They made us get in teams and draw out how we would deal with a situation. That's helpful but learning a language would have been nice.

Thanks for you help.






Similar Threads
Thread Thread Starter Forum Replies Last Post
question maheshraju ASP.NET 2.0 Basics 3 March 13th, 2008 08:54 AM
Question Ashwini Classic ASP Databases 3 January 10th, 2006 07:20 AM
Question? Calibus Classic ASP Databases 8 August 6th, 2004 08:25 AM
a question gorji C++ Programming 2 August 11th, 2003 07:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.