SQLException - ExceuteReader could not find the stored procure fals

Moordoom

Member
Joined
Nov 12, 2013
Messages
23
Programming Experience
1-3
Having an issue passing some information between forms.
On a login form, I passed and Employee ID number to a hidden label on the next form (form1).
And it shows up if I remove the hide label.
I am trying to using the employee id number I passed to that hidden label, to pull First and Last name from that database table, and pass it concatenated to Label2 on this form.

Executing my code I am getting a SQL Exception "Could not find stored procedure False" when it gets to the ExecuteReader method.
The Table I am pulling info from is as follows...
Column1 id, Column 2 EmpID, Column 3 FirstName, Column 4 Lastname, Column 5 password, Column 6 Admin (0 - 1 to denote a user or admin)

My code for Form1 so far is...

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Windows.Forms
Imports System.Windows.Forms.Form
Imports System.Windows.Forms.Label
Imports System.Windows.Forms.TextBox
Public Class Form1
    Public Sub Form1toLabel(ByVal Txt As String)
        Label1.Text = Txt
        Label1.Hide()
    End Sub
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dbFirstName As String
        Dim dbLastName As String
        Dim EmpID1 As String
        EmpID1 = Label1.Text

        Using connObj As New SqlClient.SqlConnection("server=SQL01;database=Emp_App;uid=sa;pwd=password;")
            Using cmdObj As New SqlClient.SqlCommand("select FirstName, LastName from Emp_Employees where EmpID" = EmpID1, connObj)
                connObj.Open()
                Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader   ' Here is my thrown error
                    'This will loop through all returned records 
                    While readerObj.Read
                        dbFirstName = readerObj("FirstName").ToString
                        dbLastName = readerObj("LastName").ToString
                        'handle returned value before next loop here
                        Label2.Text = String.Concat(dbFirstName, " ", dbLastName)
                    End While
                End Using
                connObj.Close()
            End Using
        End Using
    End Sub

End Class


Thanks in Advance
 
Last edited by a moderator:
This is a perfect example of why you should always use parameters to insert values into SQL code. The issue is here:
VB.NET:
Using cmdObj As New SqlClient.SqlCommand([B][U]"select FirstName, LastName from Emp_Employees where EmpID[COLOR="#FF0000"]" =[/COLOR] EmpID1[/U][/B], connObj)
What you're actually doing there is comparing the String "select FirstName, LastName from Emp_Employees where EmpID" with the String contain in the EmpID1 variable and then assigning the result, a Boolean, to the CommandText of the SqlCommand. Those two Strings are not equal, hence the value is False and that's what the error message is telling you it can't find. The equality operator should be INSIDE the String literal and you should be using a concatenation operator:
VB.NET:
Using cmdObj As New SqlClient.SqlCommand([B][U]"select FirstName, LastName from Emp_Employees where EmpID[COLOR="#FF0000"] = " &[/COLOR] EmpID1[/U][/B], connObj)
That would work as you expect but it is still not the right way to do it. Follow the Blog link in my signature and check out my post on Parameters In ADO.NET to learn how to do it properly.

Also, if you had Option Strict On then the compiler would have told you that it would not accept a Boolean where a String is expected. It wouldn't have fixed the issue but it would have flagged it at compile time instead of waiting until run time for the code to fail. Option Strict is Off by default, which many people think is a bad idea. You should turn it On and leave it on for the rest of your life. You will have to fix some compilation errors but that will help you learn to do things the right way.
 
Also, you shouldn't be using a Label as the source of the ID. The value in the Label has to have come from somewhere else and that is the source that you should be using. A Label is for displaying text to the user.
 
Back
Top