Results 1 to 5 of 5

Thread: [RESOLVED] [2008] ASP.NET VB - Must Declare Scalar Variable "@EmployeeNo"

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Resolved [RESOLVED] [2008] ASP.NET VB - Must Declare Scalar Variable "@EmployeeNo"

    Hi All,

    Below is the code that I have tried to implement but it always returns "Must Declare the scalar variable "@EmployeeNo". I have gone through the code countless times but I can't figure out what is wrong.

    vb Code:
    1. <%@ Page Language="vb" %>
    2. <%@ Import Namespace="System"  %>
    3. <%@ Import Namespace="System.IO" %>
    4. <%@ Import Namespace="System.Data" %>
    5. <%@ Import Namespace="System.Data.OleDb" %>
    6. <%@ Import Namespace="System.Web.UI.WebControls" %>
    7. <%@ Import Namespace="System.Configuration" %>
    8. <%@ Import Namespace="System.Threading" %>
    9. <%@ Import Namespace="System.Globalization" %>
    10. <%@ Import Namespace="System.IO" %>
    11.  
    12. <HTML>
    13.  
    14. <HEAD>
    15.  
    16. <title>Add Employee To Database</title>
    17.  
    18. <script runat=server>
    19.  
    20.                         Public Sub AddPerson(sender As Object, e As EventArgs)
    21.  
    22.  
    23.  
    24.                                     Dim intImageSize As Int64
    25.  
    26.                                     Dim strImageType As String
    27.                                     Dim ImageStream As Stream
    28.  
    29.  
    30.  
    31.                                     ' Gets the Size of the Image
    32.  
    33.                                     intImageSize = PersonImage.PostedFile.ContentLength
    34.  
    35.                                    
    36.  
    37.                                     ' Gets the Image Type
    38.  
    39.                                     strImageType = PersonImage.PostedFile.ContentType
    40.  
    41.                                    
    42.  
    43.                                     ' Reads the Image
    44.  
    45.                                     ImageStream = PersonImage.PostedFile.InputStream
    46.  
    47.  
    48.  
    49.                                     Dim ImageContent(intImageSize) As Byte
    50.  
    51.                                     Dim intStatus As Integer
    52.  
    53.                                     intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
    54.  
    55.  
    56.  
    57.                                     ' Create Instance of Connection and Command Object
    58.  
    59.  
    60.                                     Dim connStr As String = "Provider=SQLOLEDB;Data Source=WIN2K3R2EE\SQLEXPRESS;Initial Catalog=EmployeeDB;User id=;Password="
    61.                                     Dim SQLInsert As String = "INSERT INTO STAFF (EmployeeNo, GivenName, Surname, Telephone, Email, Picture, WrkGrp, SectionID, Gender) VALUES (@EmployeeNo, @GivenName, @Surname, @Telephone, @Email, @Picture, @WrkGrp, @SectionID, @Gender)"
    62.  
    63.  
    64.  
    65.                     Dim myCommand As New OleDbCommand(SQLInsert, New OleDbConnection(connStr))
    66.  
    67.                                     myCommand.Connection.Open()
    68.  
    69.                                     Try
    70.  
    71.                                     With myCommand.Parameters
    72.  
    73.                                           myCommand.Parameters.Add("@EmployeeNo", OleDbType.VarChar).Value = txtEmployeeNo.Text
    74.                                   myCommand.Parameters.Add("@GivenName", OleDbType.VarChar).Value = txtGivenName.Text
    75.                                   myCommand.Parameters.Add("@Surname", OleDbType.VarChar).Value = txtSurname.Text
    76.                                           myCommand.Parameters.Add("@Telephone", OleDbType.VarChar).Value = txtTelephone.Text
    77.                                           myCommand.Parameters.Add("@Email", OleDbType.VarChar).Value = txtEmail.Text
    78.                                           myCommand.Parameters.Add("@Picture", OleDbType.Binary).Value = ImageContent
    79.                                           myCommand.Parameters.Add("@WrkGrp", OleDbType.VarChar).Value = txtWorkGroup.Text
    80.                                           myCommand.Parameters.Add("@SectionID", OleDbType.VarChar).Value = txtSection.Text
    81.  
    82.                                           Dim prmSex As String = ""
    83.  
    84.                                           If sexMale.Checked Then
    85.  
    86.                                                
    87.  
    88.                                                 prmSex = "M"
    89.  
    90.                                           Else
    91.  
    92.                                                 prmSex = "F"
    93.  
    94.                                           End If
    95.                                          
    96.                                           myCommand.Parameters.AddWithValue("@Gender", OleDbType.VarChar).Value = prmSex
    97.  
    98.                                      End With
    99.  
    100.  
    101.                            If CInt(myCommand.ExecuteNonQuery()) = 0 Then
    102.  
    103.                                       Response.Write("Insert Failed")
    104.  
    105.                            Else
    106.                                       Response.Write("New person successfully added!")
    107.                            End If
    108.  
    109.                                                 myCommand.Connection.Close()
    110.  
    111.                                                
    112.  
    113.                                     Catch ex As Exception
    114.  
    115.                                              Response.Write(ex.Message())  
    116.  
    117.                                     End Try
    118.  
    119.                         End Sub  
    120.  
    121.    
    122.  
    123.     </script>
    124.  
    125.    
    126.  
    127.   </HEAD>
    128.  
    129.   <body style="font: 10pt verdana">
    130.  
    131.  
    132.  
    133.     <form enctype="multipart/form-data" runat="server">
    134.  
    135.             <asp:Table Runat=server Width=50% BorderWidth=1 BackColor=Beige>
    136.  
    137.                         <asp:TableRow>
    138.  
    139.                                     <asp:TableCell ColumnSpan=2 BackColor="#ff0000">
    140.  
    141.                                     <asp:Label Font-Name="verdana" Font-size="12px" ForeColor="#ffffff" font-bold="True" Runat=server Text="Add New Person" />
    142.  
    143.                                     </asp:TableCell>
    144.  
    145.                         </asp:TableRow>
    146.  
    147.                         <asp:TableRow>
    148.  
    149.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Employee No" /></asp:TableCell>
    150.  
    151.                                     <asp:TableCell><asp:TextBox id=txtEmployeeNo Runat=server /></asp:TableCell>
    152.  
    153.                         </asp:TableRow>
    154.  
    155.                         <asp:TableRow>
    156.  
    157.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Given Name" /></asp:TableCell>
    158.  
    159.                                     <asp:TableCell><asp:TextBox id=txtGivenName Runat=server /></asp:TableCell>
    160.  
    161.                         </asp:TableRow>
    162.  
    163.                         <asp:TableRow>
    164.  
    165.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Surname" /></asp:TableCell>
    166.  
    167.                                     <asp:TableCell><asp:TextBox id=txtSurname Runat=server /></asp:TableCell>
    168.  
    169.                         </asp:TableRow>
    170.  
    171.                         <asp:TableRow>
    172.  
    173.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Telephone" /></asp:TableCell>
    174.  
    175.                                     <asp:TableCell><asp:TextBox id=txtTelephone Runat=server /></asp:TableCell>
    176.  
    177.                         </asp:TableRow>
    178.  
    179.                         <asp:TableRow>
    180.  
    181.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Email" /></asp:TableCell>
    182.  
    183.                                     <asp:TableCell><asp:TextBox id="txtEmail" Runat=server /></asp:TableCell>
    184.  
    185.                         </asp:TableRow>
    186.  
    187.                         <asp:TableRow>
    188.  
    189.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Image" /></asp:TableCell>
    190.  
    191.                                     <asp:TableCell><input type="file" id="PersonImage" runat=server /></asp:TableCell>
    192.  
    193.                         </asp:TableRow>
    194.  
    195.                         <asp:TableRow>
    196.  
    197.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="WorkGroup" /></asp:TableCell>
    198.  
    199.                                     <asp:TableCell><asp:TextBox id="txtWorkGroup" Runat=server /></asp:TableCell>
    200.  
    201.                         </asp:TableRow>
    202.  
    203.                         <asp:TableRow>
    204.  
    205.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Section" /></asp:TableCell>
    206.  
    207.                                     <asp:TableCell><asp:TextBox id="txtSection" Runat=server /></asp:TableCell>
    208.  
    209.                         </asp:TableRow>
    210.  
    211.  
    212.                         <asp:TableRow>
    213.  
    214.                                     <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Sex" /></asp:TableCell>
    215.  
    216.                                     <asp:TableCell>
    217.  
    218.                                                 <asp:RadioButton GroupName="sex" Font-Name="Verdana" Font-Size="12px" Text="Male" ID="sexMale" Runat=server />
    219.  
    220.                                                 <asp:RadioButton GroupName="sex" Font-Name="Verdana" Font-Size="12px" Text="FeMale" ID="sexFeMale" Runat=server />
    221.  
    222.                                     </asp:TableCell>
    223.  
    224.                         </asp:TableRow>
    225.                        
    226.  
    227.                         <asp:TableRow>
    228.  
    229.                                     <asp:TableCell ColumnSpan=2 HorizontalAlign=Center>
    230.  
    231.                                     <asp:Button Text="Add Person" OnClick="AddPerson" Runat=server />
    232.  
    233.                                     </asp:TableCell>
    234.  
    235.                         </asp:TableRow>
    236.  
    237.             </asp:Table>
    238.  
    239.     </form>
    240.  
    241.  
    242.  
    243.   </body>
    244.  
    245. </HTML>

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [2008] ASP.NET VB - Must Declare Scalar Variable "@EmployeeNo"

    I see a couple things you may be doing wrong.

    in this code here:

    vb Code:
    1. With myCommand.Parameters
    2. myCommand.Parameters.Add("@EmployeeNo", OleDbType.VarChar).Value = txtEmployeeNo.Text

    you have a With statement and then you're still writing out the entire object. You don't have to do that if you're using a with.

    Next you can try .AddWithValue instead of .Add

    vb Code:
    1. With myCommand.Parameters
    2.      .AddWithValue("@EmployeeNo", txtEmployeeNo.Text)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question Re: [2008] ASP.NET VB - Must Declare Scalar Variable "@EmployeeNo"

    Thanks Besoup for the suggestion,

    I modified the With statement as you suggested but it is still returning the same error message. Any other suggestions?

    Bit of an update.

    It looks like my form values are not being passed to the parameters.

    After removing the With Statement, the values are now being passed but the error message still persists.
    Last edited by nolim8ts; Jun 9th, 2008 at 08:24 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Talking Re: [2008] ASP.NET VB - Must Declare Scalar Variable "@EmployeeNo"

    Changing from OleDB to SQLClient seemed to fix the issue
    Read on another forum that OleDB uses "?" as the annotation for defining parameters but that kept throwing an error message when implemented.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2008] ASP.NET VB - Must Declare Scalar Variable "@EmployeeNo"

    OleDb can use @ for parameter names, just a few providers don't use @. For example MySql may expect ? instead of @

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width