I am trying to delete rows from a table through a list box. Below is the code that I used to display data in the data box from the database. FYI - I'm using a Calendar Search function - By which a user selects a date on the calendar, click 'Search', and the list box displays the appropriate result dates found in DB based on
Con.Open (): SQL Commands CMD = New SQL Commands ("Member Selection, Clerical, Cell, Time with Events, Date = Date = @ Search", Con); Cmd.Parameters.AddWithValue ("@ searchDate", SearchDate.SelectedDate); Idta reader reader = CMD Spare reader (); While (reader.Read ()) {string member = (string) reader ["member"]; String Clark = (string) reader ["clerk"]; String room = (string) reader ["room"]; String time = (string) reader ["time"]; LstEvents.Items.Add ("Member:" + Member + "-" + "Clerk:" + Clerk + "-" + "Room:" + Room + "-" + "Time:" + Time); // lstEvents list is the ID of the box} reader.Close (); Con.Close ();
The above code works just fine. What I was hoping was to find a way to remove this line in DB. So a user selects one of the items returned in the list and clicks on 'delete' I found that as far as ...
SqlCommand cmd = New SqlCommand ("Delete from the event Where member, clerk, room, date, time = @member, @clerk, @room, @date, @time ", cone); Cmd.Parameters.AddWithValue ("Member", lstEvents.SelectedValue); Cmd.Parameters.AddWithValue ("@clerk", lstAppointments.SelectedValue); Cmd.Parameters.AddWithValue ("@ room", lstEventss.selectedValue); Cmd.Parameters.AddWithValue ("@date", Convert.ToDateTime (SearchDate.SelectedDate); cmd.Parameters.AddWithValue ("@time", lstEvents.SelectedValue); cmd.Connection = Thief; cmd.ExecuteNonQuery ();
This attempt, however, seems to be taking a little away, how can any suggestion be done? I do not particularly understand how I have more than one value in the list box You can choose from members and clerks.
After
When you load the rows from the database, Themic key load (even if you do not display it).
When you delete something selected in your listbox, you can only delete using the primary key.
If your table does not have a primary key, then it is very good that you should add one.
No comments:
Post a Comment