0

So I have a label which shows the username of the user. I've used this value to return their ID which I then attach to a label. I used execute scalar to do this because I wasn't sure how else to get a single value on a label.

This works fine. I then use the ID from the label and put it in another table. I can do this twice and then the page crashes saying...

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

However I don't understand. I don't pull anything from the second table on the page. I don't know why it would affect it. I feel like I've tried everything. Taking out the line that posts the ID to the label lets the page run but I need it there.

    Label2.Text = User.Identity.Name
    Dim connetionString As String
    Dim cnn As SqlConnection
    Dim cmd As SqlCommand
    Dim sql As String
    connetionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FYPMS_DB.mdf;Integrated Security=True"
    sql = "SELECT SupID FROM Supervisor WHERE (Email = @Email)"
    cnn = New SqlConnection(connetionString)
    Try
        cnn.Open()
        cmd = New SqlCommand(sql, cnn)
        cmd.Parameters.Add(New SqlParameter("@Email", User.Identity.Name))
        Dim supid1 As Int32 = Convert.ToInt32(cmd.ExecuteScalar())
        cmd.Dispose()
        cnn.Close()

        Label1.Text = supid1.ToString

    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try

End Sub
Keith
  • 20,636
  • 11
  • 84
  • 125
evanna
  • 1
  • 5

3 Answers3

0

This should return the first result for you. Also, it's a good idea to employ Using blocks for objects such as connections, commands, and readers.

    Using cn = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FYPMS_DB.mdf;Integrated Security=True")
        cn.Open()
        Using cmd = New SqlCommand("SELECT SupID FROM Supervisor WHERE Email = @Email", cn)
            cmd.Parameters.AddWithValue("@Email", User.Identity.Name)
            Using dr = cmd.ExecuteReader
                If dr.Read Then
                    Label1.Text = CInt(dr("SupID"))
                End If
            End Using
        End Using
    End Using
Craig Johnson
  • 744
  • 4
  • 8
  • Hey, Yes this did return the first result just like it did to begin with but when more than two rows are entered into the Project table (the table that we need supid for) the same same happens. I don't understand because there are not querys on the page that pull data from the Project table, only add to it. – evanna Apr 11 '15 at 12:48
0

If you are not sure there are multiple rows for same email in that table, you can change the query to following, that will work for you with executescalar.

SELECT TOP 1 SupID FROM Supervisor WHERE (Email = @Email)

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
0

Horribly sorry! But yes you were right! There was another query going on in the background that I never noticed that was affecting it all. So sorry

evanna
  • 1
  • 5