sqlParameters

mmarkym

Member
Joined
Aug 20, 2005
Messages
13
Programming Experience
3-5
I have this in my button_click procedure. The syntax for the command text property is producing an error.

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim cn As New SqlConnection
Dim cmd As New SqlCommand

Try
cn.ConnectionString = "integrated security=true;data source='mark\netsdk';persist security info=False;initial catalog=Northwind"
cn.Open()

cmd.Connection = cn
cmd.CommandText = "Insert into Login Username = @username, Password = @password, Fname = @fname, Lname = @lname"

cmd.Parameters.Add("@username", SqlDbType.VarChar, 20, "Username")
cmd.Parameters.Add("@password", SqlDbType.VarChar, 20, "Username")
cmd.Parameters.Add("@fname", SqlDbType.VarChar, 20, "Username")
cmd.Parameters.Add("@lname", SqlDbType.VarChar, 20, "Username")

cmd.Parameters.Item(0).Value = txtUserName.Text
cmd.Parameters.Item(1).Value = txtPassword.Text
cmd.Parameters.Item(2).Value = txtFname.Text
cmd.Parameters.Item(3).Value = txtLname.Text

cmd.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show(ex.Message)

Finally
cn.Close()

End Try

End Sub
 
The insert sql is incorrect, it should look like this:

VB.NET:
Insert into Login 
  (Username,
  Password,
  FName,
  LName)
VALUES
  (@username, 
  @password, 
  @fname, 
  @lname)

You can do it all on one line, that's just happened to be how I format things.

-tg
 
cmd.Parameters.Add("@username", SqlDbType.VarChar, 20, "Username")
cmd.Parameters.Add("@password", SqlDbType.VarChar, 20, "Password")
cmd.Parameters.Add("@fname", SqlDbType.VarChar, 20, "Fname")
cmd.Parameters.Add("@lname", SqlDbType.VarChar, 20, "Lname")
 
Back
Top