【问题标题】:Incorrect syntax near ',' (dbnull issue)',' 附近的语法不正确(dbnull 问题)
【发布时间】:2014-08-08 21:48:33
【问题描述】:

假设有图表要出现。但是,它不是因为有关于 dbnull 问题的问题。当三个 select 语句中的任何一个没有数据时,就会发生这种情况。

Dim user As String = Session("NoMatrik")
        Dim resultId As Object = Session("max")

        Dim idQuery = "select max(resultid) as id from tblResult where result_nomatric = @matric and result_quiz_id = 1 UNION All " +
                      "select max(resultid) as id from tblResult where result_nomatric = @matric and result_quiz_id = 2 UNION All " +
                      "select max(resultid) as id from tblResult where result_nomatric = @matric and result_quiz_id = 3"
        conn.Open()
        Dim cmdGetId As New SqlCommand(idQuery, conn)
        cmdGetId.Parameters.AddWithValue("@matric", user)

        Dim maxIDs As SqlDataReader = cmdGetId.ExecuteReader
        Dim IDs As String = ""
        While maxIDs.Read
            IDs += maxIDs("id").ToString() + ", "
        End While
        maxIDs.Close()
        IDs = IDs.Substring(0, IDs.Length - 2)

        Dim cmdString = "Select tblResult.result_quiz_id as Quiz,count(TblAnswer.AnswerType) as  answerCount , TblAnswer.AnswerType " +
                        "from TblResultDetail inner join TblAnswer on TblResultDetail.ResultDetail_Answer_Id = TblAnswer.AnswerId " +
                        "inner join tblResult on tblResult.resultid = TblResultDetail.ResultDetail_Result_Id " +
                        "where TblResultDetail.ResultDetail_Result_Id in (" + IDs + ") " +
                        "group by TblAnswer.AnswerType, tblResult.result_quiz_id order by TblAnswer.AnswerType"


        Dim cmd As New SqlCommand(cmdString, conn)

        If IsDBNull(resultId) Then
            Label1.Visible = True
            chrtResult.Visible = False
        Else

            Dim dr1 As SqlDataReader
            dr1 = cmd.ExecuteReader

            While dr1.Read()
                Dim tempArr(0) As Double
                Dim count As Double = dr1("answerCount")
                tempArr(0) = count
                Dim Type As String = dr1("AnswerType").ToString()
                Dim level As Integer = dr1("Quiz")
                chrtResult.Series(Type).Points(level - 1).YValues = tempArr
            End While
        End If
        conn.Close()
    End If

End Sub

一个错误,'(' 附近出现错误的语法出现在dr1 = cmd.ExecuteReader 行。那么,我想如何解决这个错误?

【问题讨论】:

  • 你的id是字符串还是数值?
  • varchar。我有根本没有数据的情况,然后将显示消息(标签1)。但是,如果数据不完整,'(' 附近的错误语法会出现在 dr1 = cmd.ExecuteReader.

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


【解决方案1】:

这应该是

While maxIDs.Read
    IDs += "'" + maxIDs("id").ToString() + "', "
End While

然后你需要删除最后一个单引号。

in 子句应遵循以下格式: where x.id in ('id1', 'id2', 'idN')

【讨论】:

  • 如果没有dbnull,没有问题。当 idquery 中的 select 语句之一未完成时发生错误。
  • 然后我想在继续之前检查 maxIDs 并在它为空时处理它......
【解决方案2】:

使用HAVING 子句更新您的第一个查询以排除任何Null 值,如下所示:

Dim idQuery = "select max(resultid) as id from tblResult " + 
              "where result_nomatric = @matric and result_quiz_id = 1 " +
              "having max(resultid) is not null " +
              "UNION All " +
              "select max(resultid) as id from tblResult " + 
              "where result_nomatric = @matric and result_quiz_id = 2 " + 
              "having max(resultid) is not null " +
              "UNION All " +
              "select max(resultid) as id from tblResult " + 
              "where result_nomatric = @matric and result_quiz_id = 3 " +
              "having max(resultid) is not null"

having max(resultid) is not null 将排除 UNION ALL 中的任何空值。

如果没有返回IDs,您只需在执行下一个代码块之前对其进行检查,并按照@DmitriE 的建议添加引号。重新组织它看起来像:

While maxIDs.Read
    IDs += "'" + maxIDs("id").ToString() + "', "
End While    

If IDs = "" Then
    Label1.Visible = True
    chrtResult.Visible = False
Else
    IDs = IDs.Substring(0, IDs.Length - 2)
    Dim cmdString = "Select ....."

    Dim dr1 As SqlDataReader
    dr1 = cmd.ExecuteReader

    While dr1.Read()
        ' YOUR WHILE LOOP CODE HERE'
    End While
End If

【讨论】:

  • 行 IDs = IDs.Substring(0, IDs.Length - 2) '长度不能小于零'有错误
  • 我在上次发表评论后更新了帖子,将substring 部分移到else 子句中,您测试最新版本了吗?所以它只会在你有一个字符串来执行操作时评估那个子字符串
猜你喜欢
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 2018-08-14
  • 2011-03-11
  • 2014-02-16
  • 2017-01-11
相关资源
最近更新 更多