asp.net - 为什么我会收到基于 Div 颜色样式的 SQL 错误消息?

标签 asp.net css vb.net ado.net

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

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'red'.

这是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 样式,他们应该忽略样式并专注于子文本。有办法解决吗?

最佳答案

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

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

然后在 LinkBut​​tonConfirm_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) 作为 SQL 参数。您在循环中已经有了一个计数器 x,您可以使用它来构造参数名称(“@p0”、“@p1”等)。

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

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

关于asp.net - 为什么我会收到基于 Div 颜色样式的 SQL 错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776782/

相关文章:

css - 样式化 css 列中的第一项

javascript - 使用 uikit 扩展下拉菜单

HTML 和 CSS 没有正确显示内联 block

vb.net - 从子实例调用被覆盖的基类方法

vb.net - 在 vb.net 中将远程图像插入到 Windows 窗体中?

c# - Crystal Report 仅在回发时抛出 'Failed to open the connection.'

asp.net - 域名变更后301重定向

asp.net - 如何在 Global.asax 中正确捕获 Web 应用程序错误

c# - 自定义XML序列化,如何编写自定义根元素?

asp.net - 动态设置超链接控件的 NavigateUrl 属性内联