Referring to the ConnectionStrings section of Web.Config in code

In ASP.Net 1.1, we used to use the Web.Config file to store the connectionstrings in the AppSettings section, then we referred to each setting in code like this:
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings(“MyDBConnection”))

Now, with ASP.net 2.0, there’s a new ConnectionStrings section. To refer to a setting there, now, it’s a little different:
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(“YourConnStringName”).ConnectionString)

DataFormatString does not work in Boundfield

The new Gridview control in v2.0 take over the formatting of items in the BoundFields. Therefore, using a DataFormatString by itself won’t work. To change the format to, let’s say, Currency, we’d need to, not only set the DataFormatString=”{0:c}”, but we’d need to make use of the HTMLEncode property, setting it to ‘False’

Binding DDL With a DataSource – Then adding Item to Top of List

With the new declarative form of databinding in ASP.Net 2.0, we can use DataSource controls for DataAccess, and then bind the Data Control with the DataSource control, to display the data. But many times, we need to add an item to the top of the DropDownList like ‘Select Item’. You can still do this in v2.0.

Just set the ‘AppendDataBoundItems’ property of the DropDownList to ‘True’, then, create the code to add the item in your Page_Load event, surrounded by a Postback block.

Full Code Sample provided here:
http://aspnet101.com/aspnet101/aspnet/codesample.aspx?code=dbDDL20

AccessDataSource – so easy

As you may or may not know, with ASP.Net v2.0, there are several DataSource controls, like SQLDataSource, and AccessDataSource. The AccessDataSource is even easier to use than the SQLDataSource.

There is no ‘ConnectionString’ property for the AccessDataSource, but there is a ‘DataFile’ property. All it requires is a path to the Access Database you want to use:

<asp:AccessDataSource ID="DS1"
	 Runat="Server"
	 SelectCommand = "SELECT  EmployeeID,  LastName,  FirstName,  Title From  NWEmployees"
	 DataFile="\data\northwind.mdb;">
</asp:AccessDataSource>

Setting Focus on a Control

Sometimes it’s necessary to make sure that a particular control has ‘focus’ on your web pages. This can mean at least two different things for the programmer. If you set ‘focus’ on a TextBox, then, the cursor is actually placed inside that textbox. If you set ‘focus’ on a button, then, that makes it so that, when the user presses the ‘ENTER’ key, the event for the button which has ‘focus’ gets fired.

In ASP.Net v1.x, do accomplish this, Javascript was used, and it wasn’t as straight forward as we’d all liked. In ASP.Net 2.0, however, all that is necessary to do this, is by calling the ‘FOCUS’ method for the control:

ControlID.Focus()

That’s all there is to it!

More on [Your Field Name] is neither a DataColumn nor a DataRelation for table [Your Table Name]

In addition to what David Wier wrote in the tip for this problem, check that the column name is not database generated. If you have a query like

select isnull(some_col, 0)…

then you need to change it to

select isnull(some_col, 0) as some_col

if you want to refer to ‘some_col’ in your code, otherwise you will still get this error.