Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Imports System
Imports System.Data
Imports System.Data.SqlClient


Public Class AccOpeningPage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       

    End Sub

    Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsave.Click
        Dim connetionString As String
        Dim cnn As SqlConnection
        Dim cmd As SqlCommand
        Dim sql As String

        connetionString = "Data Source=abc-PC;Initial Catalog=aaadb;User ID=sa;Password=sa123"
        cnn = New SqlConnection(connetionString)

            cnn.Open()
            MsgBox("Connection Open ! ")
            sql = "insert into CUSTHEAD(CUSTID, BRCODE, SALUTEFLG, GENDER, CNAME, DOB, AGE, FORHNAME, ADD1, ACCTYPE, ACCNO) VALUES (@CUSTID,@BRCODE, @SALUTEFLG, @GENDER, @CNAME, @DOB, @AGE, @FORHNAME,@ ADD1, @ACCTYPE, @ACCNO)"

        cmd = New SqlCommand(sql, cnn)
            With cmd.Parameters
                .AddWithValue("CUSTID", txtcustid.Text)
                .AddWithValue("BRCODE", txtbrcode.Text)
                .AddWithValue("SALUTEFLG", txtsalcode.Text)
                .AddWithValue("GENDER", optmfj.SelectedItem.ToString())
                .AddWithValue("CNAME", txtcname.Text)
                .AddWithValue("DOB", txtdob.Text)
                .AddWithValue("AGE", txtage.Text)
                .AddWithValue("FORHNAME", txtforhname.Text)
                .AddWithValue("ADD1", txtadd1.Text)
                .AddWithValue("ACCTYPE", txtacctype.Text)
                .AddWithValue("ACCNO", txtaccno.Text)
                cmd.ExecuteNonQuery()
            End With
            cnn.Close()
      

    End Sub
End Class


I get Error As

Must declare the scalar variable "@".

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
Posted
Updated 20-Jan-15 19:56pm
v2

1 solution

This:
C#
cmd.Parameters.AddWithValue("CUSTID", txtcustid.Text)

Should be like this
C#
cmd.Parameters.AddWithValue("@CUSTID", txtcustid.Text)
 
Share this answer
 
v2
Comments
Member 11316594 21-Jan-15 2:03am    
for all keys or only for CUSTID..?
Deepu S Nair 21-Jan-15 2:03am    
for all..
Member 11316594 21-Jan-15 2:09am    
I get same error...
Jan Trachta 21-Jan-15 5:41am    
why you just dont make a full SQL command ?

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