0

我正在验证布尔值是真还是假。如果它为假,它会将服务器名称文本更改为红色,如果为真,它不会改变颜色。SQL 能够读取不更改文本颜色的服务器名称,但无法读取服务器名称颜色为红色的文本并收到 SQL 错误消息,

System.Data.SqlClient.SqlException (0x80131904):“红色”附近的语法不正确。

这是VB代码:

Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
Dim strSqlSecondary As String = "SELECT [Name], [Compliance] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"
Dim cmdSecondary As New System.Data.SqlClient.SqlCommand(strSqlSecondary, sqlConn)

cmdSecondary.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())

Dim dr As System.Data.SqlClient.SqlDataReader

Try
   sqlConn.Open()
   Dim root As TreeNode
   Dim rootNode As TreeNode
   Dim firstNode As Integer = 0
   'Load Primary Owner Node
   'Create RootTreeNode
   dr = cmdSecondary.ExecuteReader()

   If dr.HasRows Then
      'Load Secondary Owner Node
      'Create RootTreeNode
      root = New TreeNode("Secondary Owner", "Secondary Owner")
      TreeViewGroups.Nodes.Add(root)
      root.SelectAction = TreeNodeSelectAction.None

      rootNode = TreeViewGroups.Nodes(firstNode)
      'populate the child nodes
      While dr.Read()
         Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))
         Dim complianceFlag As Boolean

         If Boolean.TryParse(dr("Compliance"), complianceFlag) Then
            ' Yes, compliance value is a Boolean, now set color based on value
            If Not complianceFlag Then
               child.Text = "<div style='color:red'>" + child.Text + "</div>"
            End If
        End If
        rootNode.ChildNodes.Add(child)
        child.SelectAction = TreeNodeSelectAction.None
      End While
    dr.Close()

错误来自此行代码,因为它显示为“红色”:

child.Text = "<div style='color:red'>" + child.Text + "</div>"

当我单击链接进行更新时,子节点文本正在传递,

Protected Sub LinkButtonConfirm_Click(sender As Object, e As System.EventArgs) Handles LinkButtonConfirm.Click
hide()
PanelCompliance.Visible = True
PanelDisplayGrid.Visible = True
'display the servers
Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
Dim strSql As New StringBuilder
strSql.Append("Select [Name] , [ApplicationName] , [Environment], [Description], [TechMgmtTeam] , [PrimaryOwner], [PPhone], [SecondaryOwner], [SPhone], [Queue], [Crit] from dbo.ServerOwners where")

'Loops Through all Selected items and appends to sql statement
Dim x As Integer = 0
For Each item As TreeNode In TreeViewGroups.CheckedNodes
If item.Depth = 0 Then
Else
   'append to select statement
    strSql.Append(" [Name]='" & item.Text & "' or ")
    x = x + 1
End If
Next

If x = 0 Then
   hide()
   LabelError.Text = "Please select at least one server in the left pane."
   PanelError.Visible = True
Else
   strSql.Append(" [Name]='Blank' order by [name]")
   Try
      sqlConn.Open()
      Dim cmd As New System.Data.SqlClient.SqlCommand(strSql.ToString(), sqlConn)
      Dim a As New SqlClient.SqlDataAdapter(cmd)
      Dim datTab As New DataTable
      a.Fill(datTab)

      Session("Table") = datTab

      GridViewDisp.DataSource = datTab
      GridViewDisp.DataBind()
   Catch ex As Exception
      hide()
      LabelError.Text = ex.ToString()
      PanelError.Visible = True
   Finally
      sqlConn.Close()
      sqlConn.Dispose()
   End Try
End If
End Sub

如果我摆脱 Div 标签,一切都很好,除了不会变成红色。他们如何能够阅读 Div 样式,他们应该忽略样式并专注于子文本。有没有办法解决?

4

2 回答 2

2

如果您将 Name 存储在子项的 .Tag 属性中,则无论您对子项的 .Text 做什么,都可以使用它:

While dr.Read()
    Dim myName as String = dr("Name")
    Dim child As TreeNode = New TreeNode(myName , myName)
    child.Tag = myName

然后在LinkButtonConfirm_Click

Dim x As Integer = 0
For Each item As TreeNode In TreeViewGroups.CheckedNodes
    If item.Depth <> 0 Then
        'append to select statement
        strSql.Append(" [Name]='" & CStr(item.Tag) & "' or ")
        x = x + 1
    End If
Next

但是您仍然应该添加CStr(item.Tag)as SQL 参数。您已经x在循环中有一个计数器,您可以使用它来构造参数名称(“@p0”、“@p1”等)。

编辑:这将导致 Click 处理程序看起来像

Protected Sub LinkButtonConfirm_Click(sender As Object, e As System.EventArgs) Handles LinkButtonConfirm.Click
    hide()
    PanelCompliance.Visible = True
    PanelDisplayGrid.Visible = True
    'display the servers
    Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
    Dim cmd As New System.Data.SqlClient.SqlCommand
    Dim strSql As New StringBuilder
    Dim qryBase = <sql>
                      SELECT [Name]
                            ,[ApplicationName]
                            ,[Environment]
                            ,[Description]
                            ,[TechMgmtTeam]
                            ,[PrimaryOwner]
                            ,[PPhone]
                            ,[SecondaryOwner]
                            ,[SPhone]
                            ,[Queue]
                            ,[Crit]
                        FROM dbo.ServerOwners
                        WHERE
                  </sql>.Value

    strSql.Append(qryBase & " ")

    'Loop through all Selected items and append to sql statement
    Dim x As Integer = 0
    Dim nLastCheckedNode As Integer = TreeViewGroups.CheckedNodes.Count - 1
    For Each item As TreeNode In TreeViewGroups.CheckedNodes
        If item.Depth <> 0 Then
            'append to select statement
            Dim paramName As String = "@p" & x.ToString()
            strSql.Append("[Name] = " & paramName)
            If x <> nLastCheckedNode Then
                ' we have another node to look at, so add " OR "
                strSql.Append(" OR ")
            End If

            'TODO: set the correct SqlDbType and the correct .Size
            cmd.Parameters.Add(New SqlParameter With {.ParameterName = paramName,
                                                      .SqlDbType = SqlDbType.NVarChar,
                                                      .Size = 20,
                                                      .Value = CStr(item.Tag)})

            x += 1
        End If
    Next

    If x = 0 Then
        hide()
        LabelError.Text = "Please select at least one server in the left pane."
        PanelError.Visible = True
    Else
        strSql.Append(" ORDER BY [Name]")
        Try
            sqlConn.Open()
            cmd.Connection = sqlConn
            cmd.CommandText = strSql.tostring()
            Dim a As New SqlClient.SqlDataAdapter(cmd)
            Dim datTab As New DataTable
            a.Fill(datTab)

            Session("Table") = datTab

            GridViewDisp.DataSource = datTab
            GridViewDisp.DataBind()
        Catch ex As Exception
            hide()
            LabelError.Text = ex.ToString()
            PanelError.Visible = True
        Finally
            sqlConn.Close()
            sqlConn.Dispose()
        End Try
    End If
End Sub
于 2013-11-04T21:44:26.163 回答
0

@Andrew Morton - 你的理论关于 in 中的错误是正确strSql.Append(" [Name]='" & item.Text & "' or ")LinkButtonConfirm_Click。我strSql.Append(" [Name]='" & item.Value & "' or ")通过将文本替换为值来更改为。现在一切正常!

于 2013-11-05T13:35:46.950 回答