Posts Tagged ‘Programming’
GridView with DropDownList – Server tag is not well formed.
Ran across an issue today when trying to get my DropDownList embedded within a GridView. Here is an extract of the code (the error is in the red text):
<ItemTemplate>
<asp:DropDownList
ID="ddlMaker"
runat="server"
DataSourceID="odsMakers"
DataTextField="makerName"
SelectedValue="<%#Bind("MakerID") %>"
DataValueField="makerID">
</asp:DropDownList>
</ItemTemplate>
When I compiled I received an error “The server tag is not well formed.”
After a bit of trial and error I realized it was because there were quotes nested within quotes. To make this work all that had to be done was to change the outside double quotes on the SelectedValue parameter to be single quotes as shown below.
<ItemTemplate>
<asp:DropDownList
ID="ddlMaker"
runat="server"
DataSourceID="odsMakers"
DataTextField="makerName"
SelectedValue='<%#Bind("MakerID") %>'
DataValueField="makerID">
</asp:DropDownList>
</ItemTemplate>
Hope I saved someone a bunch of wasted time.
Creating Insert Statements for SQL Server
Ran across an interesting approach to generating insert statements for SQL Server 2005 (and other variants). If uses a less well known function called master.dbo.fn_varbintohexstr. The approach that was taken was to use the fn_varbintohexstr function to encode the data so you did not need to use cursors or any fancy parsing to handle unicode and quotes in the generated insert statements. I thought it was a bit clever.
Note: you might need to register at SQLServerCentral.com to gain access to the site to read the above linked article.
ASP.NET AJAX Control Toolkit Samples
Most of you probably have already experimented with the Control Toolkit, but just in case you never ran across it, take a look at these Toolkit samples
If you have any questions about them, drop me a comment here.
Calculating Age in T-SQL
Here’s a decent article on calculating the age of a person (or anything else) written by Lynn Pettis. Discusses some pitfalls with leap years.
Using and Managing SQL Server Aliases
I know that not many people I have met in my SQL Server experience have used SQL Aliases. In many ways it is one of the most useful features of SQL Server. Roman Rehak wrote a great article about SQL Server Aliases. You have to be registered to see the full article, but some of the highlights are discussed below.
Creating an alias all depends on which client tools are installed on the computer you are using. Here are the common methods for running the SQL Client Configuration Tool:
- If SQL Server 2000 client tools are installed, use the SQL Client Network Utility and select the Alias tab.
- If SQL Server 2005 client tools are installed, use the SQL Server Configuration Manager and expand the SQL Native Client Configuration node to find the node for Aliases.
- If you do not have any client tools installed, you can still get at the Client Network Utility by executing “cliconfg” from any command prompt.
Aliases are useful in the following scenarios:
- Create an alias that is a friendlier name than the server name itself.
- In a Development/QA/Production environment. Using aliases allow you to keep the code base across your servers the same without having to change source code (sort of related to the last bullet).
- Enforce the use of a particular protocol. If you get the “Cannot generate SSPI context” error you can try creating an alias to force the use of the Named Pipes.
- A distributed environment with multiple servers that reference each other using the Linked Servers feature. You can define the linked servers with the aliases instead of the actual names. This makes future moving of databases and such much easier since you will not need to chnage the linked server names in your code.
- In a high-availability environment to allow you to quickly change the server without changing all your code (such as web applications and such).
I’m sure there are other useful reasons to use aliases, but that covers a few of them that show the power of the alias function.
If you have any other good ideas, please comment on the post.
Resetting the IDENTITY counter in SQL Server T-SQL
You’ve been there before. You are busily testing your import routines and running up the Identity column in your main import table. Before you know it the value is in the multi-millions and you just want to reset it back to 1.
The way to accomplish this is to use the DBCC CHECKIDENT command.
For example:
DBCC CHECKIDENT (SalesForce, reseed, 0)
This assumes your table is called SalesForce and you want to start the numbering at 1. If you wanted to start the number at 2000 you can substitute 0 with 1999. There has been some discussion that has said if the table never had records added to it the reseed value itself will be used instead of the reseed + 1 value. You should not need to use this statement for a “new” table.
Credit for this post is given to Pinal Dave over at http://sqlauthority.com/. The specific post this I am referring to is the one at this page.
Quick method to backup SQL Server database to workstation
On the last project I was working on I did not have physical access to the SQL Server disks in order to get a copy of the backup file created using the SSMS software. I needed to make a copy to my local SQL Server in order to do some testing before I blew away all the data on the “real” SQL Server. SSMS does not appear to have a method to allow you to specify a network path, but T-SQL has that capability with the BACKUP command.
If you complete the following steps you can get a backup copy of the database on your local workstation (assuming you have SQL Server installed locally):
- Get the IP address of your local computer using the ipconfig command or some other method.
- Make sure you have created a share on your computer in the location where you want the backup file to be stored. For this example I am going to assume there is a share called BACKUP that points to C:\BACKUP (again, this share is on your local workstation, not the remote SQL Server)
- Connect to the remote database with SSMS
- Open a Query Window
- Execute the following command (replace 1.1.1.1 with the IP address determined from step 1 and replace YourDatabase with the name of the database you want to backup)
BACKUP DATABASE YourDatabase
TO DISK = '\\1.1.1.1\Backup\YourDatabase.bak'
WITH FORMAT,
NAME = 'Full Backup of YourDatabase'
Now you can use SSMS to restore the database to your local copy of SQL Server or just keep it around in case you mess up the production database.
Another useful thing you can do is to schedule this and use the OSQL command line tool to execute the script as part of a development release or any other process. There are lots of options.
Disabling buttons on click for ASP.NET forms
Found some great tips in the simple-talk.com blog about disabling buttons on asp.net forms.
If you ever created a form and had your users click on the submit button multiple times this is the code you have been looking for. Be sure to check out Robyn Page’s comments for a method to block the whole form during the form submit as well.
Complete details can be found here.