简体   繁体   中英

excluding specific results from sql query and displaying the rest in a combo box - visual studio 2013

This has probably been asked somewhere before, but I have been searching for a while and cant find anything. I'm basically trying to create a sort-of internal messaging system in VB and am having trouble with a function that I'm working on. I already have a user database and secure login system and I'm now working on a form to send a message from user to user.

What I want to do is to run this query on form load:

SELECT usr_id, usrname FROM dbo.users 
WHERE usrname NOT IN 
    (
     SELECT ALL usrname 
     FROM dbo.users 
     WHERE usrname = '" & //textbox containing username that's logged in// & "'
    )

I want to output the items to a Combobox. The purpose of this is so that (since it's an internal system for, say, employees to communicate) a user wouldn't necessarily have to know the username of the receiver in order to send them a message. I will be changing the function eventually to display the actual name of the user rather than the username, but I can add that in later and as of right now it's not important. Here is my code so far:

Private Sub NewMsg_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim con As New SqlConnection
    con.ConnectionString = //connection string for database
    Dim query As String = //query mentioned above
    Try
        con.Open()
        Using sqlcmd As New SqlCommand(query, con)
            Dim sqldr As SqlDataReader = sqlcmd.ExecuteReader
            Dim dt As DataTable = New DataTable
            dt.Load(sqldr)
            sendtoBox.ValueMember = "usr_id"
            sendtoBox.DisplayMember = "usrname"
            sendtoBox.DataSource = dt
            con.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Information)
        con.Close()
    End Try
End Sub

The query itself works perfectly when I run it on the SQL Server, and sure enough when the form loads users are displayed in the Combobox just like I want them to. The only problem is it's still including the username that I am trying to exclude. So I have reluctantly decided to ask for a little help because I can't figure out why it's not excluding the specified username :(

Any help will be appreciated. Thanks.

Problem was my own stupidity lol. Nothing wrong with the code itself, may be useful for someone so I will leave it up here :)

For the person who thinks this does not answer the question, I would like to initially state that I was the one who asked the question.

This was being used for a "Send Message" form, and the desired function was as follows:

GET CURRENTLY LOGGED IN USERS USERNAME FROM A TEXTBOX ON A DIFFERENT FORM! >
Connect to database >
Run a query against database to gather all users EXCEPT the logged in user >
Display results in a ComboBox.

The query was fine, the code was fine, everything was fine. The PROBLEM here was that when I was debugging the form I was NOT LOGGING IN and hence the TextBox that contained the username that I wanted to exclude was EMPTY, so instead of excluding say "admin" it was excluding "" because nobody was logged in.

So tell me, how is this not an answer? It was me being dumb that caused the problem in the first place, but the VB code and SQL query may help someone else, that is why I have left it up and answered by explaining that there is nothing wrong with the code. Get off your high-horse man...

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