(ASP.NET) SQL 数据读取器返回 Null 值

Posted

技术标签:

【中文标题】(ASP.NET) SQL 数据读取器返回 Null 值【英文标题】:(ASP.NET) SQL Data Reader returning Null Values 【发布时间】:2012-10-12 03:14:29 【问题描述】:

我正在连接到数据库,然后使用 SQLDataReader 解析这些结果,然后将这些结果放入我以后可以使用的变量中。问题是我所有的“results.items”都返回空值。但是,当我调试时,DataReader 会显示正确的字段计数。

Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

    If e.CommandName = "editPost" Then
        'Remove DataGrid'''''''''
        GridView1.Visible = False
        '''''''''''''''''''''''''
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim row As GridViewRow = GridView1.Rows(index)
        Dim ID As String = GridView1.Rows(index).Cells(0).Text
        ''''''''''''''''''''''''''''''''''''''''CREATE Controls for Placeholder
        Dim editEditor As New CuteEditor.Editor
        Dim hiddenID As New HiddenField
        hiddenID.ID = "hiddenID"
        hiddenID.Value = ID
        editEditor.ID = "editEditor"
        Dim subjectTXT As New TextBox
        subjectTXT.ID = "editorSubject"
        Dim br As New Literal
        Dim submitChanges As New Button
        Dim sbjLabel As New Label
        submitChanges.ID = "submitChanges"
        submitChanges.Text = " Submit Changes "
        submitChanges.Height = 40
        submitChanges.Width = 300

        sbjLabel.Text = "Subject:   "

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        editEditor.AutoConfigure = CuteEditor.AutoConfigure.Simple
        br.Text = "<br/><br/>"
        plcEditor.Controls.Add(hiddenID)
        plcEditor.Controls.Add(sbjLabel)
        plcEditor.Controls.Add(subjectTXT)
        subjectTXT.Width = "100"
        subjectTXT.Height = "25"
        subjectTXT.CssClass = "editInput"
        plcEditor.Controls.Add(br)
        plcEditor.Controls.Add(editEditor)
        plcEditor.Controls.Add(br)
        plcEditor.Controls.Add(br)
        plcEditor.Controls.Add(submitChanges)
        submitChanges.OnClientClick = UpdatePost()
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim connStr As String = ConfigurationManager.ConnectionStrings("oakfratnewsConnectionString").ConnectionString
        Dim nCon As New SqlConnection(connStr)
        Dim addCon As New SqlConnection(connStr)
        Dim addCom As New SqlCommand("SELECT * FROM [News] WHERE ([ID] = @ID)", addCon)
        addCom.Parameters.AddWithValue("@ID", ID)
        addCon.Open()
        addCom.ExecuteNonQuery()
        Dim results As SqlDataReader
        results = addCom.ExecuteReader
        While results.Read()
            Dim editText As String = results.Item("Content")
            Dim Subject As String = results.Item("Subject")
            editEditor.Text = editText
            subjectTXT.Text = Subject


        End While



    End If
End Sub

【问题讨论】:

删除addCom.ExecuteNonQuery() 数据库的字段中是否有空数据? 【参考方案1】:

您从哪里获得 ID 值? "addCom.Parameters.AddWithValue("@ID", ID)"

删除“addCom.ExecuteNonQuery()”

为什么要使用

Dim editText As String = results.Item("Content")

我在循环之前定义了变量,然后这样读取

Dim editText As String 
  While results.Read() 
      editText = results("Content")   ' without .items
     ...
 End While 

【讨论】:

以上是关于(ASP.NET) SQL 数据读取器返回 Null 值的主要内容,如果未能解决你的问题,请参考以下文章

在 Asp.NET 中返回“语法错误”的有效 SQL 语句

获取 OleDbDataReader ASP.NET (VB) 返回的行数

ASP.NET SQL 查询未返回预期结果

无法使用 SQL 数据激活消息框 - ASP.NET

将 SQL 数据库导出到 Access - ASP.NET

从 ASP.NET 中的 Oracle DB 函数读取数据表