Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please i am having an error from my demo airline website i am designing for learning purpose.The error is must declare the scalar variable "@Dep"
Below is my code to use user selected values to display result from a database. was caught

Protected Sub buttFindFlight_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttFindFlight.Click
        Try

            Dim conn As New SqlConnection
            Dim cmd As New SqlCommand
            Dim adp As New SqlDataAdapter
            Dim ds As New DataSet
            conn.ConnectionString = ConfigurationManager.ConnectionStrings("AirportReservationSystemConnectionString").ConnectionString
            cmd.Connection = conn
            conn.Open()
            'cmd.CommandType = CommandType.Text


            adp = New SqlDataAdapter("select LeavPlace as Departing, DestPlace As Destination FROM  DailyFlightInfo where LeavPlace =@Dep And DestPlace =@Dest ", conn)

            cmd.Parameters.Add(New SqlParameter("@Dep", SqlDbType.VarChar, 50))
            cmd.Parameters.Add(New SqlParameter("@Dest", SqlDbType.VarChar, 50))

            cmd.Parameters("@Dep").Value = drpLeaving.SelectedItem.Value
            cmd.Parameters("@Dest").Value = drpGoing.SelectedItem.Value

            adp.Fill(ds)
            gridView.DataSource = ds
            gridView.DataBind()
Posted
Updated 4-Jun-12 23:36pm
v2

What does @Dep stands for?
Are you trying to use parameterized query.
If so, use sqlcommand instead of SqlDataAdapter. Here is an Example[^]

Regards
Sebastian
 
Share this answer
 
v4
Comments
codeBegin 5-Jun-12 5:47am    
Is this your answer??
jamiebones 5-Jun-12 5:49am    
yes i am using parametrized query and it represent a user selected value
Sebastian T Xavier 5-Jun-12 7:31am    
Did you solve this?
I can't find connection between your command and dataAdapter therefore try this:
VB
'...

cmd.CommandText = "select LeavPlace as Departing, DestPlace As Destination FROM  DailyFlightInfo where LeavPlace =@Dep And DestPlace =@Dest "

cmd.Parameters.Add(New SqlParameter("@Dep", SqlDbType.VarChar, 50))
cmd.Parameters.Add(New SqlParameter("@Dest", SqlDbType.VarChar, 50))
cmd.Parameters("@Dep").Value = drpLeaving.SelectedItem.Value
cmd.Parameters("@Dest").Value = drpGoing.SelectedItem.Value

adp = New SqlDataAdapter(cmd);

adp.Fill(ds)
gridView.DataSource = ds
gridView.DataBind()

'...
 
Share this answer
 
Comments
jamiebones 5-Jun-12 8:46am    
Thank you God bless you,i have been looking for a solution for a very long time i am very grateful. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900