简体   繁体   中英

SQL Union query error with vb.net

The code was working great for a long time until somebody added new name(SE01) in SQL database that cause web program to crash. The web error shown that

System.InvalidCastException: Conversion from string "SE01" to type 'Integer'
is not valid.

along with other SQL Union query error,

SELECT Substring([Name],@lenSrv, 8) as Name 
FROM [dbo].[ServerOwners] 
where [Name] like @Srv 
and Name not like '%j%' 
union 
SELECT Substring([Server],@lenSrv, 8) as Name 
FROM [dbo].[AuditLog] 
where log='delete' 
and [DATE] > (GETDATE() - 60) 
and [SERVER] like @Srv 
and Server not like '%j%' 
order by [name]

The codes job is to find next available name through database using query for example, The exist servername in database is UXVP001, and the code will find the free one which it will be UXVP002. Now someone added UXVPSE01 in SQL database that seems to cause reading query to crash. I want it to be accepted/ignored new name without error.

Here is snipped vb codes that search through database,

srv = "UXPV"
sqlAddOn =  "and Name not like '%j%'"
sqlAddOnAudit = "and Server not like '%j%'"

        "Logic to find next available name"
        "1. select the numbers to the right of the characters"
        "2.  loop all values and find first missing number"
        Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
        Dim strSql As String = "SELECT  Substring([Name],@lenSrv, 8) as Name  FROM [dbo].[ServerOwners] where [Name] like @Srv " & sqlAddOn
        strSql &= " union "
        strSql &= "SELECT  Substring([Server],@lenSrv, 8) as Name FROM [dbo].[AuditLog] where log='delete' and [DATE] > (GETDATE() - 60) and [SERVER] like @Srv " & sqlAddOnAudit
        strSql &= " order by [name]"
        Dim cmd As New System.Data.SqlClient.SqlCommand(strSql, sqlConn)
        Dim dr As System.Data.SqlClient.SqlDataReader

        LabelError.Text = ""
        Dim x As Integer = 1
        Dim y As Integer = 1
        Dim foundYet As Boolean = False
        Try
            sqlConn.Open()
            cmd.Parameters.AddWithValue("@lenSrv", srv.Length + 1)
            cmd.Parameters.AddWithValue("@Srv", srv & "%")

            dr = cmd.ExecuteReader()

            While dr.Read() And foundYet = False
            LabelError.Text = LabelError.Text & dr("Name") & " | "
            y = CType(dr("Name"), Integer)
            If x = y Then
                "keep going"
                x = x + 1
            Else
                "you found first available number"
                foundYet = True
            End If
        End While

        dr.Close()
        cmd.Dispose()
    Catch ex As Exception
        hide()
        PanelError.Visible = True
        LabelError.Text = ex.ToString() & "<hr/>" & strSql
    Finally
        sqlConn.Dispose()

    End Try

    "make sure leading zeros are present"
    " 000"
    Dim fmt As String = "00#"
    tbAdd_ServerName.Text = srv & x.ToString(fmt)
    tbAdd_ServerName.Enabled = False

    tbAdd_TM.SelectedValue = "*"

Of course this doesn't work. You are taking everything to the right of UXPV and then treating that value like a number. But you no longer have a number for that value for one record.

So what you need to do is get rid of the bad record and change the user interface so that type of data cannot be added in the future. Or you need to fix the code to strip of all letter characters.

I realized that all I had to do is add to ignore "SE" like this,

sqlAddOn = "and Name not like '%j%' and Name not like '%se%'"
sqlAddOnAudit = "and Server not like '%j%' and Server not like '%se%'"

Now it working and back to searching the way it suppose to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM