error "Must declare the scalar variable"@IDNo"." :(

krepy00

New member
Joined
Jan 10, 2014
Messages
1
Programming Experience
Beginner
con.Open()
        Dim cmd As New SqlCommand
        cmd.Connection = con


        Try
            cmd.CommandText = "INSERT INTO Professor(IDNo,LastName,FirstName,MiddleName,Department)VALUES(@IDNo,@LastName,@FirstName,@MiddleName,@Department)"
            Dim inscmd As New SqlCommand
            inscmd.Parameters.AddWithValue("@IDNo", txtsearch.Text)
            inscmd.Parameters.AddWithValue("@LastName", txtln.Text)
            inscmd.Parameters.AddWithValue("@FirstName", txtfn.Text)
            inscmd.Parameters.AddWithValue("@MiddleName", txtmn.Text)
            inscmd.Parameters.AddWithValue("@Department", txtdep.Text)
            cmd.ExecuteNonQuery()
            MessageBox.Show("NEW ACCOUNT HAS BEED ADDED TO YOUR DATABASE!")
        Catch ex As Exception
            MessageBox.Show("YOUR ACCOUNT COULD NOT BE ADDED! " & ex.Message)
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub
 
VB.NET:
con.Open()
        Dim [COLOR="#008000"]cmd[/COLOR] As [B][U]New SqlCommand[/U][/B]
        [COLOR="#008000"]cmd[/COLOR].Connection = con


        Try
            [COLOR="#008000"]cmd[/COLOR].CommandText = "INSERT INTO Professor(IDNo,LastName,FirstName,MiddleName,Department)VALUES(@IDNo,@LastName,@FirstName,@MiddleName,@Department)"
            Dim [COLOR="#FF0000"]inscmd[/COLOR] As [B][U]New SqlCommand[/U][/B]
            [COLOR="#FF0000"]inscmd[/COLOR].Parameters.AddWithValue("@IDNo", txtsearch.Text)
            [COLOR="#FF0000"]inscmd[/COLOR].Parameters.AddWithValue("@LastName", txtln.Text)
            [COLOR="#FF0000"]inscmd[/COLOR].Parameters.AddWithValue("@FirstName", txtfn.Text)
            [COLOR="#FF0000"]inscmd[/COLOR].Parameters.AddWithValue("@MiddleName", txtmn.Text)
            [COLOR="#FF0000"]inscmd[/COLOR].Parameters.AddWithValue("@Department", txtdep.Text)
            [COLOR="#008000"]cmd[/COLOR].ExecuteNonQuery()
            MessageBox.Show("NEW ACCOUNT HAS BEED ADDED TO YOUR DATABASE!")
        Catch ex As Exception
            MessageBox.Show("YOUR ACCOUNT COULD NOT BE ADDED! " & ex.Message)
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub

How many queries are you executing? How many commands do you need to do that?
 
Back
Top