从数据库创建和填充标签

时间:2014-03-12 12:04:57

标签: vb.net visual-studio-2010 labels

目前我正在开发一个用户可以输入和查看评论的系统。输入部分工作正常,但是显示注释不起作用。

我目前要做的是对于评论表中的每个条目,我希望系统创建一个新标签,然后用日期,用户名和用户评论填充该标签的文本。

我附上了我在下面的代码,非常感谢任何输入或指导。

Try
    Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\IMData.mdb;")
    Dim cmd As New OleDbCommand
    con.Open()
    cmd.Connection = con
    cmd.CommandText = "Select * from Comments where ID=@p1"
    cmd.Prepare()
    cmd.Parameters.AddWithValue("@p1", TrackNum.Text)
    Dim Comments = cmd.ExecuteReader
    Dim labelY = 418
    With Comments
        .Read()
        For Each item In Comments
            Dim newComment As New Label
            newComment.Name = item
            newComment.Left = 983
            newComment.Top = labelY
            newComment.Font = Font
            newComment.Text = .Item("Date") + " " + .Item("User") + " : " + .Item("Comment")
            newComment.Visible = True
            Me.Controls.Add(newComment)
            labelY += 47
        Next
        .Close()
    End With
    con.Close()
Catch
End Try

这是工作代码:

    Try
        Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\IMData.mdb;")
        Dim cmd As New OleDbCommand
        con.Open()
        cmd.Connection = con
        cmd.CommandText = "Select * from Comments where ID=@p1"
        cmd.Prepare()
        cmd.Parameters.AddWithValue("@p1", TrackNum.Text)
        Dim Comments = cmd.ExecuteReader
        With Comments
            .Read()
            Dim labelY = 418
            For Each item In Comments
                Dim newComment As New Label
                newComment.Name = labelY
                newComment.Top = labelY
                labelY += 47
                newComment.Left = 983
                newComment.Font = Font
                newComment.Text = item("Date") + " : " + item("User") + " : " + item("Comment")
                newComment.Width = 900
                newComment.Visible = True
                Me.Controls.Add(newComment)
            Next
            .Close()
        End With
        con.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

1 个答案:

答案 0 :(得分:1)

这里有一些错误,因为它是一个尝试,捕获没有输出你没有看到问题。

请参阅以下评论以进行更正:

Try
    Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\IMData.mdb;")
    Dim cmd As New OleDbCommand
    con.Open()
    cmd.Connection = con
    cmd.CommandText = "Select * from Comments where ID=@p1"
    cmd.Prepare()
    cmd.Parameters.AddWithValue("@p1", TrackNum.Text)
    Dim Comments = cmd.ExecuteReader
    Dim labelY = 418
    With Comments
        .Read()
        For Each item In Comments
            Dim newComment As New Label
            newComment.Name = item 'this needs to be a string such as item("ID") - must be unique!'
            newComment.Left = 983
            newComment.Top = labelY
            newComment.Font = Font
            newComment.Text = .Item("Date") + " " + .Item("User") + " : " + .Item("Comment")
                              'these 'Item's should not have the period in front of them'
            newComment.Visible = True
            Me.Controls.Add(newComment)
            labelY += 47
        Next
        .Close()
    End With
    con.Close()
Catch
'add in something to catch the error such as console.writeline'
End Try