I have to create a SQL server DB that has a table named people with, people_id int PK IDENTITY, firstName varChar(30), lastName varChar(40).
(completed)
create a windows program that lists the firstName and lastName combined with a space detween them in a listBox
(completed)
provide functionality in code using command objects/parameters (no wizards) to add and delete from database and reflect the changes in the listBox
(i'm using two textBoxes)
add (completed)
I am having trouble with the delete. Can someone maybe make a suggestion..
thanks
this code populates the listBox on page load...it works.
here is my ADD code...it works.Code:
Public Class Form1
Private Sub loadListBox()
ListBox1.Items.Clear()
Dim connStr As String = My.Settings.dbPeopleConnectionString
Dim sql As String
Dim conn As SqlConnection
Dim cmd As SqlCommand
sql = "SELECT people_id, firstName, lastName FROM people"
conn = New SqlConnection(connStr)
conn.Open()
cmd = New SqlCommand(sql, conn)
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim moreresults As Boolean = True
Do While moreresults
While reader.Read
ListBox1.Items.Add(reader("firstName") & " " & (reader("lastName")))
End While
moreresults = reader.NextResult()
Loop
conn.Close()
End Sub
here is my DELETE code, it is the same as the ADD, just changed the SQL statement...it crashes at ExecuteNonQuery()Code:Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim connStr As String = My.Settings.dbPeopleConnectionString
Dim conn As SqlConnection
Dim cmd As SqlCommand
conn = New SqlConnection(connStr)
conn.Open()
cmd = New SqlCommand()
cmd.Connection = conn
' create parameters
Dim Pfname As New SqlParameter("@firstName", SqlDbType.VarChar)
Pfname.Direction = ParameterDirection.Input
Dim Plname As New SqlParameter("@lastName", SqlDbType.VarChar)
Plname.Direction = ParameterDirection.Input
' end parameters
Pfname.Value = txtFirst.Text
Plname.Value = txtLast.Text
cmd.Parameters.Add(Pfname)
cmd.Parameters.Add(Plname)
cmd.CommandText = "INSERT INTO people(firstName, lastName) VALUES (@firstName, @lastName)"
Dim result As Integer = cmd.ExecuteNonQuery()
conn.Close()
If result > 0 Then
MessageBox.Show("Success")
Else
MessageBox.Show("Failure")
End If
loadListBox()
txtFirst.Text = ""
txtLast.Text = ""
txtFirst.Focus()
End Sub
incorrect syntax near '(',
Code:Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim connStr As String = My.Settings.dbPeopleConnectionString
Dim conn As SqlConnection
Dim cmd As SqlCommand
conn = New SqlConnection(connStr)
conn.Open()
cmd = New SqlCommand()
cmd.Connection = conn
' create parameters
Dim Pfname As New SqlParameter("@firstName", SqlDbType.VarChar)
Pfname.Direction = ParameterDirection.Input
Dim Plname As New SqlParameter("@lastName", SqlDbType.VarChar)
Plname.Direction = ParameterDirection.Input
' end parameters
' textBoxes
Pfname.Value = txtFirst.Text
Plname.Value = txtLast.Text
cmd.Parameters.Add(Pfname)
cmd.Parameters.Add(Plname)
cmd.CommandText = "DELETE FROM people(firstName, lastName) VALUES (@firstName, @lastName)"
Dim result As Integer = cmd.ExecuteNonQuery() <<--incorrect syntax
conn.Close()
If result > 0 Then
MessageBox.Show("Success")
Else
MessageBox.Show("Failure")
End If
loadListBox()
End Sub
End Class

