定义整个页面可访问的变量

时间:2018-08-29 10:16:46

标签: vb.net webforms

早上好,

我正在vb.net上工作。目的是使CodeFile运行一些sql查询,然后在页面加载时将这些值作为sqldatasource查询参数返回到网页。

我有以下vb代码

Partial Class Consult
Inherits Page
Public totalCount As Integer = "0"

'*******Here i declare the variable***************    
Public Property PatNo() As String
    Get
        Return ViewState("PatNo")
    End Get
    Set(ByVal value As String)
        ViewState("PatNo") = value
    End Set
End Property

Public PatNam As String = "abc"
Public ConID As String = "abc"
Public TreatType As String = "abc"
Public HPC As Char = "abc"
Public LvTyp As RadioButtonList



Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    Me.PatNo = Request.QueryString("pNo")
    Session("PatientNo") = PatNo
    Dim ConsultationID As String = Request.QueryString("consultID")
    ConID = ConsultationID
    Response.Write(PatNo)

    Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
    Using con As New SqlConnection(constr)
'**********BELOW LINE CAUSES ERROR**************
        Dim query As String = "SELECT * FROM hPatients WHERE pNO=@PatNo" 
        query += "SELECT * FROM hPatients WHERE pNO='001/000034824'"
        Using cmd As New SqlCommand(query)
            cmd.Parameters.Add("@PatNo", SqlDbType.NVarChar).Value = PatNo
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using ds As New DataSet()
                    sda.Fill(ds)
                    gvPatientInfo.DataSource = ds.Tables(0)
                    'Return ds.Tables(0).Rows(0).Item(2)
                    'Response.Write(ds.Tables(0).Rows(0).Item(2))
                    gvPatientInfo.DataBind()
                    gvClientInfo.DataSource = ds.Tables(1)
                    gvClientInfo.DataBind()
                End Using
            End Using
        End Using
    End Using

    If Not Me.IsPostBack Then

        'Dim di As DataInfo = Me.GetInfo()


        'Populating a DataTable from database.
        Dim dt As DataTable = Me.GetData()
        'Response.Write(ConID + " " + PatNo)
        'Building an HTML string.
        Dim html As New StringBuilder()

        'Table start.
        html.Append("<table border = '1' class = 'table'>")

        'Building the Header row.
        html.Append("<tr>")
        For Each column As DataColumn In dt.Columns
            html.Append("<th>")
            html.Append(column.ColumnName)
            html.Append("</th>")
        Next
        html.Append("</tr>")

        'Building the Data rows.
        For Each row As DataRow In dt.Rows
            html.Append("<tr>")
            For Each column As DataColumn In dt.Columns
                html.Append("<td>")
                html.Append(row(column.ColumnName))
                html.Append("</td>")
            Next
            html.Append("</tr>")
        Next

        'Table end.
        html.Append("</table>")

        'Append the HTML string to Placeholder.
        PlaceHolder1.Controls.Add(New Literal() With { _
           .Text = html.ToString() _
         })
    End If
End Sub

我得到的错误是

Must declare the scalar variable "@PatNo".

在获取变量以执行page_load函数中的查询的第一步中发生错误。

第二步是获取查询结果并在网页上使用。

任何人,请帮助!!

1 个答案:

答案 0 :(得分:0)

我认为问题出在这行:

cmd.Parameters.Add("PatNo", SqlDbType.NVarChar).Value = PatNo

将名称从“ PatNo”更改为“ @PatNo”,它应该可以使用。