Cleanest way of filling a disabled input field via DB - asp.net-mvc

I have created a form which allows the user to add to an inventory of items, stored in a database. Each item in the Inventory table is associated with a UID which is auto-incrementing, non-editable, and effectively auto-generated.
The top field in the form is ItemID, which has input disabled and is merely there to inform the user of the ID of the new item they are about to add.
Ideally, here's the pseudo-code:
<input type="text" name="email" value="<% (SELECT Max(ItemID) FROM dbo.Inventory).ToInt32 + 1 %>" disabled="disabled"/>
I already have a Linq to SQL ORM set up. This is all being done in MVC, so the idea occurred to me to figure out the correct value inside the form's controller and then just passing it to the view directly.
What would be the cleanest, simplest way to achieve this? I think ideally, the "value" field should be no more than 1 line.

This approach won't work.
If ItemID is auto-incrementing field what components increase its value? Anywhere but the database you will run into trouble. Assuming you do use a database assigned field, when you populate your input field with a value you take the next available value at that time. What happens though if two forms are read and submitted out of order? The Id you populate in your value field will not match up with id the database will assign. So this only would potentially work for a single user at a time.
Best is to let the DB assign the value when you do the actual DB insertion using an identity column and not worry about doing this manually or even trying to predict the id - don't populate the value field this way.

Related

How do I get MS-Access form entries to save to a table (or query)?

We have two teams working on system related issues and my Access database has one table for each teams open issues. I have created a query that matches issues from each list based on the system name. The query is returning potential matches correctly.
From there, I need to be able to review each "match" and select yes or no on a dropdown to confirm if both teams are working the same issue in order to quickly and easily reduce redundancies. I would prefer to review each result in a form format and have figured out how to add a dropdown box with the yes/no options; however, once I've validated each item, I'd like to filter the results down to only show the items that have been designated as "yes".
That's where I'm stuck. The form is not currently saving the results, including the dropdown info, to a table from what I can see.
How can I get the form to load to a table (or query) in order to filter out the "no" responses?
Try converting your query to an append query or an update query. Create a new table that this query adds data to, and in that table create a new field called "YesNo" or something like that. Put that new field from your table as a control source for your dropdown and should add a value in the "YesNo" field in your new table based on what you select. I'm not sure what value will be added to the table, I did something similar with a check box, when it's clicked it adds a "-1" and when it's unclicked, it adds a 0. Then I run a report that selects records based on the value in that field. I'm assuming the dropdown would work in a similar way but not a 100% sure. Hope I understood your issue, I'm no expert at access but I ran into the opposite issue recently where I didn't want the form to save the results.

Delphi one unique form for insert and update database record

In my app does I need to have separate forms for:
Form for showing data i grid
Form for insert
Form for update
Or I just need two: one for displaying data, and one unique for insert and update.
In unique insert form just pass selected id from row and with query load it in fields. First in insert form will declare boolean variable like editMode false. If passed id exist in db activate query and fill fields with values.
Is this good or bad practice? How big companies or skilled developers separate this?
I don't need someone to write code for me. I just want to know proper algorithm for this. Thanks

Loop over all model fields in Rails/MongoDB

I am using mongoid/Rails for my application server. I defined my own field typ 'formula'. This is a field I don't want to have persistent in the database since this is not a data field but a formula which is consistent across all records of a model. So I stored this formula in my metadata and whenever I create dynamically a model where I need this formula I create a field with my custom type 'formula' where the formula is the default value. This works so far - but as soon as I save a record which includes my formula field it doesn't.
So before I create or update any records I want to loop over all fields of my model and depending on the type the field I allow it to be updated - or not ( so for every formla field I don't allow updates)
I can loop over all field names (model.fields.keys.each) and check the name of the field - But I haven't found a method to loop over all fields and access the type. Any idea?
But perhaps there is another way to avoid my formula field is saved in the database? I create a mongoize (object) method for my new field type 'formula' and this method simply returns nil - but this still writes an empty field to the database.

Database security issue with Rails using select field

As a paranoid entry-level developer I was wondering if it is possible for a user to use some kind of query to insert data into a database through a form with a select field.
More specifically lets say in my db I have a Gender column (data type text) and in my form I am using the select tag and passing in 2 options Male and Female. Although in the html the user only has 2 options to select from but the db doesn't know that. The gender column will pretty much accept anything. I just wanted to know if a nuisance user can disregard the select options and somehow insert a silly answer into the Gender column? If so, how can I protect from that.
The thing you are talking about here is the validations need to be done before entering data to the database. Invalid data can come from anywhere ( directly from controller or from GUI) and its the responsibility of the model to validate the data before it commits to the database.
Go through these links.
http://api.rubyonrails.org/classes/ActiveRecord/Validations.html
http://guides.rubyonrails.org/active_record_validations_callbacks.html

Is it possible to sort a TDBGrid on a lookup field?

I have a DBGrid with a column based on a lookup field.
How can I set it up so that when a user clicks on the column title, it will sort by that field.
My problem here is that I can't figure out a way to create an index on a lookup field.
I'm using Absolute Database for this, but most things that work with the BDE or TClientDataSet will work with Absolute.
Thanks!
I don't think it is possible to create an index on a lookup field. It is possible to create an index on an internally calculated field of a ClientDataSet though. In the OnCalcFields event handler set its value to the value of the lookup field. And set the visible property of the lookup field to false. Now you can sort on the internally calculated field.
What you could do (especially if the data is readonly, and does not have zillions of rows) is use a ClientDataSet to display data in your grid.
Roughly the steps would be like this:
Load the data from your regular into the ClientDataSet,
add a calculated field to the ClientDataSet that contains the value obtained from the lookup,
then add an index to that calculated
field.
--jeroen
You cannot sort by a lookup field. But you can 'fake' this. Let's suppose that you have the following tables: (PK means Primary Key)
Contacts
ID - Integer (PK)
NAME - Varchar(40)
COUNTRYID - Integer
Countries
ID - Integer (PK)
NAME - Varchar(40)
Then you can have the following query in the dataset which is linked to the TDBGrid:
SELECT C.ID, C.NAME, C.COUNTRYID, CO.NAME
FROM CONTACTS C
JOIN COUNTRIES CO ON C.COUNTRYID=CO.ID
(Not tested but I think that you got the idea)
Also you can put this in a view.
Then you'll display in your TDBGrid (as columns) only the ID, NAME and the desired lookup field which you already have (let's call it COUNTRYLOOK).
When one clicks on the Title Header you can change the query by adding in the 4th line an ORDER BY . For the specific column of the lookup field (COUNTRYLOOK), instead of using the 1:1 mapping you can put in the 4th line of your query ORDER BY CO.NAME. Reopen the query and that's it. In practice is much more simpler than my description here.
DevExpress ExpressQuantumGrid can do it, check it out:
http://www.devexpress.com/products/vcl/exquantumgrid/

Resources