Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hello guys, im new to this programming language. here is my code; i want to manipulate database using sqlstring

<script language="VB" runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim costcenter As String = Session("costcenter")
        lbl1.Text = costcenter
        If lbl1.Text = "12941" Then
            Dim strSQL As String
            Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Integrated Security=SSPI;" & "Initial Catalog=TestSQL")
            strSQL = "SELECT * FROM [tblTrainingPlan] WHERE ([CostCenter] = @CostCenter)"
            connection.Open()
            Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
            GridView1.DataSource = myCommand.ExecuteReader()
            GridView1.DataBind()
            connection.Close()
        End If
    End Sub


My question is when i run it an error occured
Must declare the scalar variable "@CostCenter".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@CostCenter".

Source Error:

Line 14:             connection.Open()
Line 15:             Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
Line 16:             GridView1.DataSource = myCommand.ExecuteReader()
Line 17:             GridView1.DataBind()
Line 18:             connection.Close()


Source File: C:\Documents and Settings\oca14625\Desktop\WebSite3l\connectdatabase.aspx    
Line: 16
I need to set parameters but dont know the sytax
could you provide me a sample code.. Thanks in advance.
Posted
Updated 28-Apr-11 16:40pm
v2
Comments
Tarakeshwar Reddy 28-Apr-11 22:40pm    
Fixed pre tags

1 solution

You can add this code before the ExecuteReader method.
VB
myCommand.Parameters.AddWithValue("@CostCenter", [value of your cost center])
 
Share this answer
 
v2
Comments
janwel 29-Apr-11 1:22am    
thanks for the help ^^

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