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

时间:2012-10-12 03:14:29

标签: asp.net sql datareader

我正在连接数据库,然后使用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

1 个答案:

答案 0 :(得分:0)

你在哪里获得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