How to save UITableView data to DB? - c#

I started to use Xamarin.iOS, and the first app i'm trying to build is a Notes App, just like default Apple Notes app.
I'm using the recipe from Xamarin Recipes, and used UITableView to show the data, and TableSource class to manage cells and items.
The problem start when I want to save the data, so I will be able to load the notes after reopen the app. I'm using ADO.Net, and its working well. I tried to save the data to table contains to columns: ID & Text.
The issue is how to work with the DB table and at the same time with the UITableView.
For example: I loaded few items, let say that they will get the ID from Db: 1, 2, 3, same as they are in UITableView IndexPath. Now i'm deleting item number 2, so than item that was num 3, is now 2 in UITableView.
When trying to access and edit item number 2 (old 3) - Can't find him in the DB.
The reason for the problem is known, but how to fix it - I really dont know.
Any suggestion will be great.
Thanks, Tal.

you are doing little wrong lets start say you have your record id is unique may be 1 - 2 - 3
you now forget about indexPath just focus on your record id,
load the data and now you have deleted the record 2, no problem edited your data id 3 and updated into DB, but for this you have to use array of dictionary so that eavery record have there property like
id and message so you always update the Right dataset

Related

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

Adding a new field or adding a new value?

If i have a field in my db which clarify the type of the application .
takes two values 0 or 1
0 for web app and 1 for win app
and now there is some requirement in my business:
There are some win applications available to all users and some of
them belong to specific users .
What 's the best solution to handle this case .
adding new field to state whether it's public or private
or just adding new value to the same field say 2 to state it's private win app
If you haven't already it would probably be best to slip in a user, role, permission based security model to the database/system, thereby giving you the ability to specify a group of users that have access to a particular application, whether it be web or windows based
I'd say add a new column next to your AppId called PublicIndicatior
Oh and be sure to have a lookup table so people can see what 0 or 1 means, and foreign key it to your data table
Lookup Table:
AppTypeId, AppTypeDescription
0, WebApp
1, WinApp
Data Table:
Id, AppTypeId, PublicIndicator
1,0,1
etc
As Pope suggested above (I +1 him), the best scenario is to add in a new user table (or tables for roles etc if possible) and then link to that through either a new foreign key, or using the appid (assuming it is on your table and unique). Then when the boss comes back 3 weeks later and say, "that's great, but now can we restrict App99 to just the Accounts Dept" you are not going back to the drawing board.

Entity Framework hacking

I have entity named DocumentItem. It has a natural key that consists of two columns : DocumentId (id of the document) and Index (the position of an item on a document).
This is a natural key so it has to change sometimes. Entity Framework prevents changes to the key. However I've managed to bind Inserts/Updates/Deletes to stored procedures. They get one extra parameter - NexIndex which becomes a new index for a DocumentItem when the sproc is executed.
There's however one problem: managing the object state manager so it has the current and valid information. Imagine a situation like this:
Document with document items:
1.
2.
3.
4.
5.
User deletes number 3 and adds a new position (always at the end, user cannot reorder items).
Changelist:
Delete 3 (ok)
Update 4 -> 3 (ok)
Update 5 -> 4 (ok)
Insert 5 - this is where it breaks
The problem is that Entity Framework is not aware of index changes. I've tried to bind Index back from database, but it always results in an exception "Cannot determine valid order of operations...".
Now the situation is as follows: EF believes that it already has item with index 5 so the last insert breaks.
I need to do something to clear the object state of the updated items so that I can add new or attach or download them from the db.
What can I do?
Doing this with database is pain in the ass generally. You can have your order column in the database but don't use it as a "real order" without gaps. If you delete item with order = 3 so be it. There will be a gap. When you insert new item add it with order = 6. Your items will be still ordered in correct order.
If you don't like this idea revert back to ADO.NET or don't use order as part of key.

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

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...

Categories

Resources