DropDownList doesn't work when clicking currently selected item

Let’s say you populate your dropdown list with five items.By default, the top item is ‘selected’ or showing, when the population finishes. Then, if you choose that item in a that is currently showing, it won’t fire the onselectedindexchanged event. That’s because it’s not a click event. The item in the DropDownList actually needs to change to fire this event.

The way to get around this is to add a ‘dummy’ item to the top of the list, once it’s populated, like:
” — Choose — “
Then, any valid item you choose from there should work. The only drawback is that you need to code around this item in your event procedure, like this:

If ddl.SelectedItem.text <> ” — Choose — ” then
    ‘put code for event here
End if

Dynamic Meta Tag Keywords

If you have a database with Dynamic Meta Tag Keywords, just query the database, assign them to a variable (like strKeys) and then, using an ASP.Net Literal Control, you can display them on the page like this:

litMeta.text=”<META NAME=’KEYWORDS’ CONTENT=’” & strKeys & “‘>”

To see a tutorial on the basics of creating dynamic content for a website, check out:
Adding Dynamic Content to Your Pages

Function to Kill Leading Zeros

If you have a string of numbers that has a bunch of leading zeros, and you want to get rid of them, just displaying the rest of the number, here’s a Function that will do it in DotNet:

Function KillEm(sItem)
Do While sItem.IndexOf("0") >-1
	if sItem.StartsWith("0")
		sItem=sItem.Substring(1)
	End If
Loop
Killem=sItem
End Function

Here’s a way to implement it in code:

Sub doZee(Source as Object, E as EventArgs)
	label1.text = KillEm(text1.text)
End Sub
<Form id="PutIDNameHere" runat="server">
<asp:TextBox id="text1" runat="server" />
<asp:Button id="Button1" Text="Kill the Zeroes" onclick="doZee" runat="server" />
</Form>
<asp:Label ID="label1"  runat="server" />

Syntax error in INSERT INTO statement

If you get this error, make sure your field names aren’t reserved words when you create your tables. To get around after it’s fully created, if one of your fieldnames IS a reserved word, just surround it with sqare brackets, like this:
[YourFieldName]

To see a list of reserved words for SQL, search the Tips and Tricks for ‘Reserved’ or ‘Reserved Words’.

Login failed for user /

If you are using SQL Server or MSDE and you get this error, it’s most likely due to the fact that you’re using Integrated Security in your login to the database. For this to work, the ASPNet account needs to have access to the database. This means the ASPNet account must be a user assigned in the database.

You can go to the command prompt and enter the following, based on your own machine’s configuration:
osql -E -S (local)\NetSDK -d puthereyourdatabasename -Q “sp_grantdbaccess ‘YourServer\ASPNET’”

AND THE SECOND ONE

osql -E -S (local)\NetSDK -d puthereyourdatabasename -Q
“sp_addrolemember ‘db_owner’, ‘YourServer\ASPNET’”

One other way to get around it is to use an explicit login and NOT use Integrated Security:
(“server=YourServer;uid=YourUID;pwd=YourPWD;database=YourDatabase”)

Server.Mappath

Of course, with Classic ASP, it was possible to ‘emulate’ the exact path of the MS Access Database in the connection string by using ‘Server.Mappath’.

In ASP.Net, it can still be used, however, you won’t be able to use ‘..\’ to go up a directory from the root, like was possible with Classic ASP – directly. To do that, there are two different ways to accomplish this. One way is – create a virtual directory pointing to that directory and reference it accordingly inside your ASP.Net page. The second way is by doing it this way:

MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("/") & "../data/myDB.mdb;"

AllowCustomPaging must be true and VirtualItemCount must be set

If you are getting this error:

‘AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID MyDataGrid when AllowPaging is set to true and the selected datasource does not implement ICollection’

Most likely, you are trying to implement paging in a DataGrid while binding the DataGrid with a DataReader.

To fix this, instead of using a DataReader to bind data to the DataGrid, use a DataSet

Operation must use an updateable query

If you’re getting an error when Inserting or Updating an MS Access Database (‘Operation must use an updateable query’), that’s because the ASPNet user account needs the correct permissions on the directory where the database resides
To fix this in Windows 2K:
(Go into Accessories/Computer Management/Local Users & Groups/users
You will see that there’s an ASPNet user. )

Right click on the directory where your database is – choose properties – Then the security tab – then click on permissions.
Add the ASPNet user to the shared section
Then, make sure it has ‘Change’ rights –
Click Apply and you should be working fine then

Clear All The Textbox Values (Reset Function)

In Classic ASP, to clear all the textboxes in a form, to start over, we just had to use a simple html ‘Reset’ button in the form. Sometimes that works in ASP.Net;sometimes it doesn’t.

Here are a couple of ways to do this, iterating through the ASP.Net TextBox controls in a form —
Just create a Reset type subroutine – in that routine, use the following code:
Dim myForm As Control = Page.FindControl(“Form1″)
Dim ctl As Control
For Each ctl In myForm.Controls
If ctl.GetType().ToString().Equals(“System.Web.UI.WebControls.TextBox”) Then
CType(ctl, TextBox).Text = “”
End If
Next ctl

in C# – it would be:
Control myForm = Page.FindControl(“Form1″);
foreach (Control ctl in myForm.Controls)
if(ctl.GetType().ToString().Equals(“System.Web.UI.WebControls.TextBox”))
((TextBox)ctl).Text = “”;

This will clear EVERYTHING from the textboxes – even if you had them pre-populated with data. A VERY simple way to just reset it to the condition at Page_Load time, just do this in the Reset SubRoutine:
Server.Transfer(“YourPageName.aspx”)

Thanks to Andre Colbiornsen and Ian Payne in the ASPFriends Lists.

Force Button Click by Pressing Enter Key

Sometimes, you will notice, that, in an ASP.Net form, depending on the circumstances, pressing the ‘Enter’ key to submit the form does not work.

To force this to happen for a particular button on your page, just put this in the Page_Load routine:

Page.RegisterHiddenField(“__EVENTTARGET”, “button1″)

Then, change ‘button1′ to the ID of your particular button. Understand, of course, if your cursor is inside of a MultiLine textbox, the default action of the enter key is to create a new line in the textbox, so, if this basically works anywhere outside of that scenario.