(VB2008) SqlException was unhandled: must declare the scalar variable "@FFEmailAlias"

Posted on 2009-06-29
Last Modified: 2012-05-07

I am using Visual Studio 2008 (VB2008) and trying to code a procedure which will return a record from the database using a parameter.  When I execute this code, I get a SqlException error.  I have tried a number of things from articles found here and still cannot get this to work.  

Sub ValidateUser()
        'get strUserName from environment
        strUserName = Environ("USERNAME")
        'look for strUserName in Employee Table
        Dim EmailAliasParm As New SqlParameter("@FFEmailAlias", strUserName)
        'EmailAliasParm.ParameterName = "@FFEmailAlias"
        'EmailAliasParm.Value = strUserName
        connection.ConnectionString = connectionString
        selectStatement = "SELECT * FROM Employee WHERE EmailAlias = @FFEmailAlias"
        Dim selectCommand As New SqlCommand(selectStatement, connection)
            Dim reader As SqlDataReader = selectCommand.ExecuteReader(CommandBehavior.SingleRow)
            If reader.Read Then
                intEmployeeID = reader("ID")
                intManagerID = reader("ManagerID")
                strFirstName = reader("FirstName")
                strLastName = reader("LastName")
                strEmailAlias = reader("EmailAlias")
                MsgBox("no data")
            End If
        Catch ex As SqlException
            Throw ex
        End Try

Open in new window

Question by:AltaSens
  • 2

Author Comment

ID: 24741361
Here is the SqlException detail:

System.Data.SqlClient.SqlException was unhandled
  Message="Must declare the scalar variable "@FFEmailAlias"."
  Source=".Net SqlClient Data Provider"
       at ProjecTrack.frmTimeEntry.ValidateUser() in C:\VSProjects\WindowsApplication1\WindowsApplication1\Form1.vb:line 146
       at ProjecTrack.frmTimeEntry.TimeEntry_Load(Object sender, EventArgs e) in C:\VSProjects\WindowsApplication1\WindowsApplication1\Form1.vb:line 49
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at ProjecTrack.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Open in new window

LVL 15

Accepted Solution

mohan_sekar earned 500 total points
ID: 24741391
You were almost there.

After the statement, Dim selectCommand As New SqlCommand(selectStatement, connection) add


Author Closing Comment

ID: 31598184

yes, that worked.  THANK YOU THANK YOU THANK YOU!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
disk usage reporting tools 27 57
T-SQL Query - Group By Year 3 32
Access #Deleted data 20 43
Estimating my database size 7 21
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question