How to select a complex/composite key in C# using combo-boxes? - c#

I have a problem with C#.NET (Visual Studio 2010). I have 2 datatables in SQL2005 express :
Building which its primary key is "building number",
and Aparatment which its primary key is "building number" (foreign key to Building table) + "apartment number". (apartment is weak entity of building and its key is composite).
Both tables are part of a greater database which I've imported into the project as DataSet.
I want to show the apartment details when I choose the building number and apartment number. Building number is set in an textbox (which its values are always building numbers chosen by a different control) and I want a combo-box to show only apartment numbers that the building in textbox has and not all apartment numbers in the table (which results in something like 1, 2, 3, 1, 4, 5, 1, 2, ... (because it selects all the rows)). Not all the buildings share the same apartment numbers or same the same quantity of apartments which I should add/change/delete on the fly.
Choosing the building part is done and running. I just have problem with showing only the current building apartments (at least the apartment numbers I need).
I don't want (more precisely not allowed) to use datagrid. "Detailed" controls only.
How do I do that? I have little experience with C# and I don't know how to work "to the max" with DataSets either.
I've thought of maybe to create a view that will give me the results but I don't know how to import a view and set the building number as a parameter.
Any other sane way is also welcome.
Please help.
Many thanks ahead,
Shay.

I would create two dropdowns for this where their autopostback is set to true. On page load I would select all building ids and place them in its dropdown. When the dropdown is changed, an onchange event is fired which populates the 2nd dropdown with all the apartment ids for that corresponding building id.

OK I digged a little more here and found the answer to the second question. Thanks Ross for the DataRow[] solution and how to populate it (the first step that is).
For future generations here's the relevant code:
DataRow[] apartmentCollection = dbBonusHwDataSet.tblApartment.Select("buildingNum='" + Convert.ToInt16(buildingNum_textbox.Text.ToString()) + "'");
apartmentNum_combox.DataSource = apartmentCollection;
apartmentNum_combox.DisplayMember = "apartmentNum";
I don't know why the code block is not displayed right. Sorry for using IE right now. But I can't see the code in the code tab...

Related

C# Fill Data Grid View after using input list

I really need your expert help :). Expanding on what I have learnt from querying data sets using adapters and filling a Grid View. I need some help on the following task.
I have a SQL Server Database which I am querying using C#. I already have solid working solutions of by a date range, a specific value. However, the business user would like to search by a list of values they provide as input into a form.
Similar to the below:
adapter.SelectCommand.Parameters.AddWithValue("#mindate", textBox1.Text);
The input will be taken from a text box or similar form based element. There should be no defined limit to the number of values e.g. I don't want to prevent the user from inputting 100 values for example.
By way of example.
User input: doc1.num1.value;doc2.num1.value;doc3.num1.value etc
Note: The document number field may contain a full stop. However, each value will be terminated by a ';'
In the above example, we would run the following query: select employee_id, docNumbers from tableName where docNumbers in (inputlist)
And the output would be:
Record 1: 1, doc1.num1.value
Record 2: 2, doc2.num1.value
Record 3: 3, doc3.num1.value
Thanks in advance guys and gals.
I guess what you are looking for is using an IN clause when doing your query.

How can I properly present and write a checkboxlist to a db? (Theory/Logic help)

Please note that the database design I have now is fully in sandbox mode. Nothing is finalized. Everything (again this is in sandbox mode) is in one single table. Also, I'm in now way looking for coding help. I'm looking for the right theoretical/logical approach to this puzzle so I can go in and play with the coding myself. I learn better that way. If I need coding help, I'll come back here for further assistance.
I'm in the process of creating the first of a few CheckBoxList for a manually created form submittal. I've been looking over multiple ways to not only create said CheckBoxList but to enter it into the database. However, the challenge I'm facing (mainly because I haven't encountered this scenario before and I'd like to learn this) is that I not only need to worry about entering the items correctly in the database but I will eventually need to produce a report or make a printable form from these entries.
Let's say I have an order form for a specific type of Barbeque grill and I will need to send this form out to distriution centers across the nation. The distribution centers will need to pull said barbecues if they are highlighted on the form.
Here's what the CheckBoxList for the distibution centers will look like:
All
Dallas
Miami
Los Angeles
Seattle
New York
Chicago
Phoenix
Montreal
If the specific city (or all the cities) are checked, then the distribution center will pull the barbecue grill for shipment.
The added part is that I want to:
be able to create a grid view from this database for reporting to note which distribution center got orders for barbecues and
be able to create reports to tell what distribution center sent out barbecue orders in a given month (among other reporting).
Here's what I'm playing around with right now.
In my aspx page I have a checkboxlist programmed with all the distribution centers entered as a listitem as well as an option for 'ALL' (of the distribution centers).
I also created a dedicated column in this table that holds all the information in the listitem and programmed a sqldataconnection to this table to play with the programmability of leveraging the database for this purpose.
When it comes to writing the selections to the database, I originally created a column for each destination city including the 'All' option. I was toying around with just putting the selections into one single column but with some of the information I've been reading today about Database Normalization, the former options seems to be a better one than the latter. Is this correct practice for situations such as this especially if I need to think about reporting? Do I put the CheckBoxList data in one cell in a specific column? Do I create seprate columns for each distribution center? Am I even on the right track here?
Depending on the amount of cities that you want to store, I've used bitwise operators to store small amounts of data. Effectively, it would store it in the table like this:
CityID Location
2 Dallas
4 Miami
8 New York
16 Chicago
32 Montreal
and keep going in base 2 for additional cities.
When your user selects multiple cities for the order, the value that gets inserted into the database for cities is a bitwise OR calculation. So if they select Dallas, New York, and Chicago, you would be doing the following:
2 OR 8 OR 16
Which would equal 26
Now, you can use bitwise AND on the resulting value. So if checking for Miami the following is the evaluation:
26 AND 4 = 0
which indicates that Miami was not selected. Any value that was selected in the evaluation, it would return its ID like this:
26 AND 8 = 8
Again, I've only used this for small subsets of data, and to make the data storage as compact as possible. Computationally, it may be a trifle more expensive that some other methods, but I'm not 100% certain.
Note: This might not be the best approaches but I have seen them used.
1) Having one column of comma-delimited string
This should work well if the options don't have IDs in the database (having a separate referenced table)
You will need to loop through the checkbox list, obtained the selected options and concatenate them with String.Join()
You will need to split the string upon receiving it from the db and use it to check the checkboxes if there text is found in the resulting array
Problem: You might need a split function in the DB that converts the comma-separated string into rows. There split function implementation on the web/stackoverflow
2) You can have a separate table for the locations e.g. xxxxtable_location where the FK to the main table is referenced. This will be a one-many table
ParentID, Location
1 Dallas
2 Miami
2 New York
2 Chicago
3 Miami

Using two sequences for one table in SQL Server 2008 r2 with Entity framework

I´m developing a loan administration application. I have a table/entity named Loan where I save them all. What I need, is to save the invoiced loans with an autoinc (1, 2 , 3, 4, 5, 6, etc) and to have another autoinc sequence to save all those loans not invoiced (example: 20000, 20001, 20002, etc). The idea is to have both loans in the same table, but with different IDs, then, based on a state, invoiced or not invoiced loan will appear. The application is already in its final stage, so using 2 different tables to save each one of them is not an option. I need advice to find a solution and see if this can be done. By the way, I´m using Entity Framework 4 with C#.
Basically, in a few words, I need to create 2 autoinc sequences for one field of the table. The 2nd autoinc number must start in a high number so as to not be reached by the first one.
Hope somebody can help me out..
EDIT:
Guys, I want to elaborate more about the problem because I don´t know if you are getting the point of doing this. The idea of the not invoiced loans is to let the user to give loans "illegaly" without paying taxes for it. Thats why it should be differentiated. I just cant use one id sequence number because if some inspectors looks at the id number would say, "Ok, here we have loans 1, 2, 3, 7, 8, but where is it 4, 5, 6?" The idea of having two sequences one a lot higher than the other, high enought to not be able to reach it by the first, was to achive that. Using the same sequence, but having another field to differenciate if it´s invoiced or not can be a possible solution, but as I have many users, I liked how SQL automatically sets the id of the loan.
As Greg has mentioned, first of all, you will need to turn off auto incrementing on the table. Then create a stored proc that you will call from the data access layer, which will create two records on the table by calculating the correct Ids. This way the data access layer and the business logic will be agnostic to the db-level task.
And as again pointed out by Greg, have a strategy in place to handle the situation when the number of applications reaches 20K mark.
If you don’t mind me asking, why did you select the approach of basing state on a primary/candidate key? Why not maintain a column just to say whether the loan is invoiced or not? Since you are using EF, if you take this approach, you should be able to use Table per Hierarchy to project two sets of data based on the discriminator key (http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-framework-4-0-tph-part-2/ and
http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx)
Or.. why not query the invoice table, if available (sub query or join) to figure out whether a loan is invoiced.
Cheers

Is it good to store static data in db - C#?

I have this problem and I don't know what is the best solution for it.
I have table called Employees and there is column called LastWork, this column should only have custom values I choose for example:
value 1
value 2
and I want the user to select the value from ComboBox control so I have 2 ideas for it but I don't know what is the best for it.
A - add these value to Combobox as string in Items property and store them as string in DB.
B - create separate table in my db called for example 'LastWork' with 2 columns 'LastWorkID', 'LastWorkName' and insert my values in it, and then I can add binding source control and I can use data bound items to store the id as integer in my main table and show the LastWorkName for users.
I prefer to use the B method because in some forms I have DataGridView control with edit permission, and I want to display Combobox in it instead of Textbox to select from these custom values.
I hope you understood my questions.
Normally data normalization is a good thing, so I too would go with your option B.
By having a separate table and a foreign key relationship to it, you can enforce data integrity; easily get a list of all available (not just all selected) options; have a single place in which to change the text of an option (what if someone decides to call it "value one" instead of "value 1", for example?); and so on and so forth.
These might not be huge benefits in a small application and with only two possible options, but we all know that applications very often tend to grow in scope over time.
In a normalized database, your "option B" is usually the way to go because it eliminates duplicate data. It will potentially introduce an additional join into your queries when you need the name (and not just the ID), but it also allows you to rename lookup names easily without altering their underlying IDs.
For performance reasons, it's often a good idea to cache lookup values such as you describe in the business tier so that your lookup table is not hit over and over again (such as when building many rows of a grid).
I would always save them in the db. If you have to localize your app, this helps alot. Additonally, it let you to apply the referential integrity checks of the database.

Sorting BindingSource using two different columns

Im currently trying to fix some bugs in a system using C# in VS 2008.
The problem goes as follows:
The client wants some controls to be sorted. The form consists of four controls. Two of which are binded with BindingSourceA lets say and the other two with BindingSourceB.
One of the controls binded with BindingSourceA displays a code and the other one a name. Same goes for BindingSourceB.
Control1 needs to be sorted using the Code display member/column while Control2 needs to be sorted using the Name display member/column. Same goes for controls 3 and 4.
After some poking around i found that BindingSourceA.Sort = "Code ASC and BindingSourceB.Sort = "Code ASC do the job.
BUT i need something along the lines of BindingSourceA.Sort = "Code ASC, Name ASC" which also was the result of my poking around. The problem is that it doesnt do the trick for me.. Its either one or the other. I even tried BindingSourceA.Sort = "Code ASC"; BindingSourceA.Sort = "Name ASC"; but that didnt work either..
Let me know if you need anymore info.
Thanks in advance
The sorting support of data-bindings actually depends entirely on the underlying data implementation, and whether it supports IBindingList.SupportsSorting, IBindingListView.SupportsAdvancedSorting, neither, or both. Personally: just sort the data separately (via LINQ, perhaps), and then data-bind it. Avoids the entire issue and works for any data source.

Categories

Resources