Using Variables from Databound items

These things must be addressed:
1. The variable must be declared at the page level. (inside the script tags, but not inside a sub/method)
2. You must call DataBind() so the DotNet framework will evaluate any code within <%# %> syntax
3. You must use ASP.Net databinding syntax: <%# %&gt

Select Specific ListBox Item

If you don’t know the exact index number in the list of listbox items (listbox1.selectedindex=x), then you can find an item and select it by using the Value of the item:
ListBox1.Items.FindByValue(MyValue).Selected = true
If you don’t know the
Value of the item, but you know the text, you can find and select the item using the Text of the item:
ListBox1.Items.FindByText(“TextYouAreLookingFor”).Selected = true

Updateable Query Error

Sometimes, when inserting or updating data in an MS Access Database, you get an error like this:
‘Operation must use an updateable query’
To get around this, you must assign ‘Change’ permissions to the directory where the database is located

Page Count Errors

Sometimes, you will be programmatically changing tables or the number of rows, and binding them to an existing datagrid. If you have a higher page number selected when one of these changes happens, you will get a page count error like this:
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

To fix this, do a Try/Catch error routine like this:

With MyDataGrid  Try   binddata  ' where binddata is a databinding routine  Catch    .currentpageindex=0      binddata  End Try end with 

Database Nulls with a DataReader

Many times, when you are returning results from a database, there will be nulls. Here’s how to handle it when it happens while using a DataReader:

While objDR.Read
	sAuthor=objDR("Author")
	sBorn=FixBirthDate(objDR("YearBorn"))
End While
DataBind
End Sub

Here’s the FixBirthDate Function:

Function fixBirthDate(sItem) as String
if sItem is System.DBNull.Value Then
	fixBirthDate="<i><font Color='#FF0000'>No Date Specified</font></i>"
else
	fixBirthDate="<b>" & sItem & "</b>"
End If
End function

Then, on the page, you can refer to the items like this:
<%# sAuthor%> – <%# sBorn%>

Here’s a complete Code Sample, showing this in a slightly varied way, but with the same general concept:
Handling Database Nulls with a DataReader

SelectedItem.text Not Keeping Selection

When using DropdownLists and Listboxes, or any control which uses ‘selecteditem.text’ to get the selected item, the major reason for not getting the correct item is as follows. If you are binding the control at Page_load, you MUST surround the binding code with a page.ispostback/if/then statement.
Something like this:

if not Page.IsPostBack then
‘do your databinding and/or list selection here
end if

Inside Page_load, you need to make sure that, when it posts back, the actual item selected is maintained….
The reasoning is as follows:
If you have as specific item being selected during page_load – it will always be selected upon every subsequent page_load – the page_load sub is exactly that – it tells the page what to do when the page is loading.

If you post back, based on a selection, you naturally, would not want this to happen – you only want it to happen the first time the page loads – not on a postback…

SO — to get around this, you need to qualify whether or not the page is loading for the first time, or it’s posting back.

For further information, check out the tutorial on PostBack

Get error msg in your browser

How to obtain ERROR message into to your browser ?
1. Open file Web.config of your site and set CUSTOM ERROR MESSAGES to
2. Open your IIS manager and config the web site as Application.
Do’not forget to re-set into when
your site goes in production for security reason.
ASP.Net gives more error info than ASP classic.
Enjoy programming with ASP.NET
Best Regards,
AnCo