I have a table with three columns that I need to display on a ASP.NET page. (SQL Server 2005, ASP.NET 2.0)
id int
value varchar(50)
tstamp timestamp
I use the timestamp field to handle concurrency validation so it's for internal use only and will never be displayed to the end user. But I need to store it somewhere in order to do proper updates.
Here's my update sproc.
UPDATE ValueTable SET value = #value
WHERE (id = #id) AND (tstamp = #tstamp)
SELECT #tstamp=tstamp FROM ValueTable
WHERE id=#id
I use a SqlDataSource to connect to my database and the schema has all three columns. My grid view will only display two fields since the timestamp field is hidden (Visible=False)
When I profile my asp page it looks like it doesn't store the timestamp anywhere even though I have a "hidden" field in the table.
How would you store a timestamp value in general on a web page? It should never be displayed, but it is needed for any updates.
After a lot of playing around I think I found a half-decent solution.
The problem seems to be that fields that are "Visible=false" are not included or bound in any update or delete commands.
The HiddenField works fine for Update commands, so if you are not doing any Delete commands you should be fine by including a hidden field which is bound (Bind(TStamp)) to the timestamp column in a templated field.
The problem is that if you are doing Deletes as well it doesn't look at any bound, hidden fields.
What I came up with was to add the timestamp to the data keys of the grid view. That way it will be considered a composite key together with the ID.
So, in short, add the timestamp to the DataKeyNames of the GridView/DetailsView etc, and remove any visible fields. That seemed to do the trick.
Related
I have 2 combo boxes bound to a data-source, in order to make a search web page in ASP.NET. Both combo boxes are bound to database tables. The data-source determines its SQL SELECT statement based on the values of the combo boxes, so results are filtered. But, I want to include the case where the user doesn't want to set a value in some of the combos and wants to retrieve all database records regarding this combobox. I have included static entries in both combos (e.g. ), but how can I program the datasource, so as when is selected by user, no WHERE statement will be applied?
I found this answer a bit useful, but there are security issues and is not so straightforward.
Changing SqlDataSource.SelectCommand at runtime breaks pagination
Thanks in advance
Assuming that the value in the comboboxes when the user selects nothing is -1 you can modify your sql like this:
SELECT *
FROM TABLE
WHERE ((COLUMN_1 = #PARAM_1) OR #PARAM_1 = -1)
AND ((COLUMN_2 = #PARAM_2) OR #PARAM_2 = -1)
if you pass -1 as value for both parameters the query will return all the records in the table
I am implementing an online parking reservation system and I need to bind a table with 2 Controls.
for example the user selects a Reservation start date and the parking location from a RadioButtonList and then a button (Search Availability) is pushed to fetch the parking from the database according to the Date selected and Location.
the question is: How can I bind the (Reservation Start Date Control) with (RadioButtonList) to both search in the database? and what would be the Sql Query?
Regards.
This is pretty basic stuff so you've got a lot of work ahead of you.
On your aspx page, you will want to use a SqlDataSource and add two ControlParameters to the SelectParameters, one for the RadioButtonList, one for the TextBox/Calendar with the date. Then create a GridView control to display the results and set the DataSource of the gridview to be the SqlDataSource.
Depending on your database schema, the SQL Statement will look something like this:
SELECT * FROM [Parking] WHERE [LotID] = #LotID AND [Date] = #Date AND [Reserved] = FALSE;
However, I have done reservation systems in the past, and queries to find available spots for a particular day are rarely simple. I would suggest worrying about writing the SQL query first and then getting the web page to run the query later. If you post information about your table schema and tag it as a SQL question you'll probably have better luck.
Hope this helps.
I have a table with primary key whose importance is technically high and none business wise.
So, when I display the table data to user, I would like not to show him the primary key.
Even if I load it in sqldatasource and dont show (I did this by removing the cloumn in "Columns" tag of GridView), I am not able to update or delete row using the built-in UpdateCommand & DeleteCommand.
I have created the delete paramters but it does nothing when I hit the delete button.
How to get around it?
Thanks
R S Reddy
Use the primary key as the CommandArgument for the delete button. This say, you still have server side access to the value you need without having to expose it on the front end.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
Well, I got this fixed by using DataKeyNames attribute in GridView.
Just added primarykey of my table into it and everythings working like charm.
Thanks for your prompt help Babak.
I have a table in the database that store 4 category and the structure of the table is ID (GUID), description. I load the category into a dropdown list (asp.net webform) to allow people to select a category and based on what they selected. I'll then display info associated with their selection and hide the others.
Currently, i do a "select case" based on the GUID that i hard coded in code behind to display the associated info. Is there a better way to do this without hard code in GUID on the code behind?
What is the Data that's associated with the Guid/Description...
The data you've hardcoded sound's like a candidate for being added to the database itself.
If it's one piece of information per Category/Guid, then consider extending your Database Table to store that info to.
If it's multiple piece of information per Category/Guid, then consider creating a new Table With a CategoryID on it, and a foreign key relationship between your Category Table and your ExtraInfo table
You could query the database for the GUIDs when the app starts and cache them in a static Dictionary.
you could store the GUID in your web-config and load it at run time. then, you can easily replace that GUID with another w/o having to recompile.
You should have a Categories table and a Posts table (or whatever it is that you will tag with your categories). In the Posts table you have a column for the CategoryID (assuming each post can only belong to one category), so that you only have the category name in one place (normalize your data).
When you render the dropdownlist, you select the GUID:s from the database. No hard coding, and if you add another category (or remove one) the dropdownlist will automatically reflect the available categories.
If you bind the dropdown list to the Category row or a Tuple that contains the category name and the value you can load the Guid in your codebehind using the SelectedValue property. You will then set the DataTextField and DataValueField on the Dropdownlist.
I have a table that contains three columns.
"UserId" type-nvarchar
"PostAuthorId" type-nvarchar
"Post" type-text
This table will contain "wall" posts like in facebook for each user's page. I am going to use a gridview on each user's page to display the posts. The issue is I want to display them with the latest(most current) post being first and the earliest post being last.
I have never used autoincrement before and I am not sure if that is the answer. If it is, I do not know how to use it. I thought about adding a date posted column and then ordering by date.
If I end up using the date column, I could also display the date on the post. Is there a way to convert the date to a readable format?
What is the best way of implementing this type of ordering?
If you use AutoIcrement the first record will start with 1 and each record will increment from there. (default setting)
If you want to sort them by newest first do an ORDER BY ID DESC
I would suggest making a column called wallPostID then setting that to AutoIncrement and also your Primary Key
Date Formating:
If you are displaying this data in a gridView
Go to Edit Columns on your grid view
CLick on the Date field under "Selected Fields" on the bottom left
Under "BoundField properties" on the right Go to Data -> DataFormatString
{0:d} will display as 1/1/2010
This site has more info in string formatting
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
A datetime column would definitely work for something like this. Assuming you are using MS-SQL, you can also attach a default value to the column using a built-in function like GETDATE(). That way, you only have to input the data that matters and the database will take care of adding the datetime column.
For converting a datetime to a readable format try:
DateTime postDate;
string value = postDate.ToShortDateString();
You should always use an ID field that auto increments. Can also be used as your PK
I would suggest the DateTime field rather than the autoincrement simply because it will not only serve as an effective Sort field, it also preserves information that you may well want to display. If you want the most recent first you'll sort using the Date and a "DESC" modifier:
Select ... Order By [Date] DESC;
When you retrieve the data, you can retrieve it as a DateTime and modify it using C#. You can use "ToShortDateString()" as suggested by mdresser if you just wish to show the date or ToString("...") if you wish to show the time as well. You can also use SQL to convert it into a string before retrieving it:
convert(Varchar(10), #mydatetime, 101)
If you look in MSDN you'll see the various conversion codes (101 is the code used above) that can be used to translate the date in various ways.
UPDATE: You may want to use an autoincrementing field for your application for reasons other than your expressed need to sort wall entries. They are easy to use - just mark the field as an Identity if using SQL Server (other DBs are similar). As far as using them in your program, just think of the field as an Int field that you never have to set.
Now, why would you use a auto-incrementing field? Perhaps the most straightforward reason is so that they give you have an easy way to identify each record. For example, if you permit people to alter or delete their wall entries, the auto-incrementing field is ideal as it gives you a way to easily look up each record (each record will be assigned its own, unique value). You might put an "x" next to the record like StackOverflow does and make it a call back with the UID (auto-increment) value. Note that you should set up your primary key on the UID field if you'll be doing this.
Now, if you find them useful for this reason then you could also sort by the UID. I would still store the date so that you can provide Date and Time feedback as to when an entry was made on the wall but this would no longer be your indexed or sorted field.