executing SQL within an event routine

Pavilion

Member
Joined
Jan 22, 2014
Messages
5
Programming Experience
10+
Hello Everyone:

I've a lot of experience with php, and other languages. But C# is completely new to me.

I'm working in Visual C# 2010 express and doing some experimentation.

My project uses the Northwind database as its Datasource

As I said, I'm doing some experimentation before recommending C# to a client. Specifically I am trying to do the following:
  1. Create a form with multiple listboxes
  2. User will be dragging and dropping between the various listboxes
  3. Main listbox shows "unassigned employees"
  4. Users will be able to drag an unassigned employee to any of the other listboxes representing different work stations.

I've figured out how to get the listboxes set up and use the drag/drop functionality. Now I'm running into problems updating the database.

Within the Northwind database I added a new field to the Employees table. The new field name is: StationAssignment

At this point, when the user grabs and employee, I want to execute some SQL to update the Employees!StationAssignment field. The SQL is pretty straight-forward:

UPDATE Employees SET StationAssignment = NULL WHERE ([Employee ID] = 6);
Of course - after I figure out how to execute SQL within an event routine (SelectedIndexChanged), I'll want to learn how to use parameters. But right now my biggest question is:

How do I execute sql within an event routine, specifically what is the proper syntax? The project already has a datasource, so do I have to set up a data connection?


Thank you in advance - Pavilion
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
An "event routine" is nothing special. An event handler is simply a method like any other. The only difference is that it gets executed automatically when something happens rather than your calling it directly. That's not anything to do with the method itself though. You could call it directly like any other method if you wanted to. So, if an event handler is a method like any other, that means that data access in an event handler is as it would be for any other method. Learn data access and you have learned how to do what you want to do. If you need to learn data access then the Data Walkthroughs link in my signature below would be a good place to start.

If you already have a Data Source then you already have a data connection. You create an instance of the appropriate table adapter and call the appropriate method. I would think that the best way to go here would be to call Fill on a single table adapter to populate a single DataTable, bind that to multiple BindingSources,, set the Filter of each BindingSource based on the StationAssignment column and bind them to the ListBoxes. All you then have to do in code is change the StationAssignment value of a record and it will automatically be hidden in one ListBox and displayed in the other.
 
Top Bottom