无法从SQL Server数据库中读取数据

时间:2016-06-18 09:41:48

标签: asp.net sql-server vb.net

当我从SQL Server数据库中读取数据时,我遇到了问题。主要的是我想从数据库中读取数据并在Label控件中显示数据。但令人担忧的是它无法读取数据。我将向您展示代码段,并且很乐意考虑任何意见/建议。

Option Explicit On

Imports System.Data
Imports System.Data.OleDb

Partial Class ViewDetail
    Inherits System.Web.UI.Page
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim InstructorID As Integer

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    InstructorID = Request.QueryString("Instructor_ID")
    Integer.TryParse(lblID.Text, InstructorID)
    con = New OleDbConnection("Provider=SQLNCLI11;Data Source=ARIES-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=SchoolDB")
    con.Open()
    cmd = New OleDbCommand("SelectData", con)
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@id", InstructorID)
    dr = cmd.ExecuteReader
    If dr.HasRows Then
        While dr.Read
            lblID.Text = dr("Instructor_ID").ToString
            lblFirstname.Text = dr("FirstName").ToString
            lblLastname.Text = dr("LastName").ToString
            lblAddress.Text = dr("Address").ToString
            lblContact.Text = dr("Contact_Number").ToString
        End While
    End If
    dr.Close()
    cmd.Dispose()
    con.Close()
End Sub
End Class

1 个答案:

答案 0 :(得分:2)

这条线似乎完全错了

Integer.TryParse(lblID.Text, InstructorID)

此行采用Page_Load事件中lblID.Text的当前值,并尝试设置InstructorID的值。但是您的代码似乎希望传递的QueryString包含实际值。

如果您确定QueryString包含有效整数,则删除该行并添加

InstructorID = Convert.ToInt32(Request.QueryString("Instructor_ID"))