使用Function显示Label中字段的数据

时间:2015-03-20 20:46:39

标签: asp.net sql-server vb.net visual-studio-2010

我有大约14页,每个都有6-10个问题  真答案的答案,写入数据库为1和0。

我创造了  摘要页面,因此它是可打印的,包含所有14页,全部为6-10  每个问题然后我想通过问题显示问题的答案  标签

我在VB.net和SQL上非常糟糕,所以对此有任何帮助  大

我设法让第一个工作,显示为"是或否"  但第二个不起作用

有人能够吗?  制定一个函数,这样我就可以为每个标签调用一行代码  而不是有大量的if语句?

我在sql和vb上超级糟糕  并且几乎没有任何经验,所以任何帮助都将受到赞赏。

这是  我目前拥有的代码,请根据需要进行编辑(如果它甚至是正确的?)

Imports System.Data
Imports System.Data.SqlClient
Public Class DraftPrint
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection
    Dim comm_Id As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        comm_Id = Request.QueryString("Comm").ToString

        con.ConnectionString = "Data Source=YK15-YKC0201;Initial Catalog=Testing;Integrated Security=true"
        con.Open()

        Dim cmd As New SqlCommand
        cmd.Connection = con
        Dim str As String = "Select Indicator, Question, Answer From maca where community_ID =" + comm_Id.ToString + " order by Indicator ASC"
        Dim adapter As SqlDataAdapter = New SqlDataAdapter(str, con)
        Dim dt_summary As DataTable = New DataTable
        adapter.Fill(dt_summary)
        Dim indicator As Integer = dt_summary.Rows(0)(0)
        Dim question As Integer = dt_summary.Rows(0)(1)
        Dim answer As String = dt_summary.Rows(0)(2)


            If question = 1 And answer = False Then
                DraftPrint1.Text = "No"
            Else
                DraftPrint1.Text = "Yep"
            End If

            If indicator = 1 And question = 2 And answer = False Then
                DraftPrint2.Text = "No"
            Else
                DraftPrint2.Text = "Yep"
            End If

1 个答案:

答案 0 :(得分:0)

这只是解决问题的方法之一。您可以在查询中准备所有内容:

Select Indicator, Question, 
    CASE 
        WHEN Answer = 1 And Indicator = 1 THEN "Yes" /* Not sure what indicator is but lets pretend it must be 1 for success */
        ELSE "No" 
    END as Answer 
FROM
...

现在,在代码中映射它的一种方法是Order By Question(假设问题编号)

 DraftPrint1.Text = dt.rows(1-1)("Answer").ToString()
 DraftPrint2.Text = dt.rows(2-1)("Answer").ToString()
 ......

如您所见,这样一切都来自数据库准备填充您的标签,因此您所要做的就是将右侧行标签映射到右侧。为避免混淆,我使用(1-1)技术。您还可以根据查询结果集创建动态控件 - 这样您就不需要事先了解控件的数量。