我不确定为什么会收到此错误。我的问题的描述在代码下方。我在下面的代码中排除了一些 Select Cases 和字段,以使其尽可能短。
Private Sub SetClassGrid(ByVal ClassCategory As String)
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("theDataBase").ConnectionString
Dim con As New SqlConnection(connectionString)
Dim sqlCommand As StringBuilder = New StringBuilder()
sqlCommand.Append("SELECT Name, City, State FROM Classes$ WHERE (ClassesCategoryID = ")
Select Case ClassCategory
Case "Art"
sqlCommand.Append("1)")
Case "Drama"
sqlCommand.Append("2)")
Case "Aquarium"
sqlCommand.Append("2)")
End Select
Dim cmd As String = sqlCommand.ToString()
Dim da As New SqlDataAdapter(cmd, con)
Dim ds As New DataSet()
Try
con.Open()
da.Fill(ds, "Classes$")
Finally
con.Close()
End Try
GridView1.DataSource = ds.Tables("Classes$")
GridView1.DataBind()
End Sub
对于 Select Case- 当ClassCategory = "Art" 时,它工作正常;但是,当ClassCategory等于其他任何值时,我得到一个错误。
同样对于 Select Case - 如果案例是“艺术”并且如果我将sqlCommand从 =“1)”更改为 =“2)”,它会按预期工作。