【问题标题】:Conversion from type 'DBNull' to type 'Short' is not valid. .net从“DBNull”类型到“Short”类型的转换无效。 。网
【发布时间】:2015-02-24 22:38:18
【问题描述】:
Imports System.Data.OleDb

Namespace wbdsproject

    Partial Class frmUserPrvMsg
        Inherits System.Web.UI.Page

        Dim topicDb As OleDbConnection
        Dim topicCmd As OleDbCommand
        Dim topicReader As OleDbDataReader
        Dim topicId As Integer
        Dim AuthorName, Username As String

    #Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub


        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

    #End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
            'Put user code to initialize the page here
            Dim uid As Int16
            topicId = Request.QueryString("topicId")
            topicDb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\discussion\SampleForum.mdb")
            topicDb.Open()
            If Session("loginuser") = "" Then
                Response.Redirect("frmlogin.aspx")
            End If
            Username = Session("loginuser")
            topicCmd = New OleDbCommand("select uid from tbllog where uname='" & Username & "'", topicDb)
            uid = topicCmd.ExecuteScalar
            topicCmd = New OleDbCommand("Select uname,pMsgDate,pMsgSubject,pMsgDetail from tblprivatemessage, tbllog where tblprivatemessage.pMsgTo=" & uid & " and tbllog.uid=tblprivatemessage.pMsgFrom", topicDb)
            topicReader = topicCmd.ExecuteReader
            DataGrid2.DataSource = topicReader

            DataGrid2.DataBind()
            topicReader.Close()
        End Sub

        Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        End Sub

        Private Sub btninbox_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btninbox.Click
            Response.Redirect("frmUserPrvMsg.aspx")
        End Sub

        Private Sub btnOutBox_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnOutBox.Click
            Response.Redirect("OutBox.aspx")
        End Sub  
   End Class
End Namespace

.net 初学者,只是我无法理解未知问题。任何解决方案

【问题讨论】:

    标签: .net vb.net


    【解决方案1】:

    你的命令

    select uid from tbllog where uname='UserName'
    

    正在返回 DBNull。

    这个话题应该会有所帮助:DBNull check for Execute Scalar

    【讨论】:

    • 而一个DBNull结果意味着没有指定名称的用户。
    猜你喜欢
    • 2014-06-10
    • 2012-11-19
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多