?
Solved

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

Posted on 2009-06-29
3
Medium Priority
?
433 Views
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.  

HELP?!
 
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)
 
        Try
 
            connection.Open()
 
            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")
 
            Else
 
                MsgBox("no data")
 
            End If
 
        Catch ex As SqlException
 
            Throw ex
 
        Finally
 
            connection.Close()
 
        End Try

Open in new window

0
Comment
Question by:AltaSens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:AltaSens
ID: 24741361
Here is the SqlException detail:


System.Data.SqlClient.SqlException was unhandled
  Class=15
  ErrorCode=-2146232060
  LineNumber=1
  Message="Must declare the scalar variable "@FFEmailAlias"."
  Number=137
  Procedure=""
  Server="ALT-DC02\PROJECTRACK"
  Source=".Net SqlClient Data Provider"
  State=2
  StackTrace:
       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()
  InnerException: 

Open in new window

0
 
LVL 15

Accepted Solution

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

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

selectCommand.parameters.Add(EmailAliasParm)
0
 

Author Closing Comment

by:AltaSens
ID: 31598184
doh!

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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