How to replace a string in a SQL Server Table Column

Scenario:
You have a table (SQL Sever, in this case) which references paths (UNC or otherwise), but now the path is going to change. In the path column, you have many records and you need to change just a portion of the path, but not the entire path. And – you need to change the same string to the new one, in every record.
Here’s a SQL script you can use to handle it (making your necessary specific changes, of course):

UPDATE
[dbo].[YourTableName]
SET [YourColumnName] = Replace([YourColumnName],‘OldPathString’,‘NewPathString’)

Separate User name from domain for Logged in User

If you use the LOGON_USER ServerVariable, or User.Identity, to get the logged in user name, then you’ll know that you get it in the following form:
DOMAIN\USERNAME

To get these separately, do something like this:

Dim USERNAME As String = UCase(Request.ServerVariables("LOGON_USER")) lblIdentity.Text = USERNAME.Substring(USERNAME.LastIndexOfAny("\") + 1)

and

lblDomain.Text=USERNAME.Substring(0, USERNAME.IndexOf("\"))

'EndsWith' is Case-Sensitive

The string function ‘EndsWith’ is case-sensitive, even in VB.Net, so if you’re looking for a certain string (maybe the file extension, in a filename), you need to make sure you’re looking for the correct thing, since ‘xls’, with case-sensitivity, is different than ‘XLS’.

So, if you’re function is looking for ‘xls’, you must make sure that you make adjustments to the filename string, in order to catch things like this. To do this, you would need to change:
if myFilename.EndsWith(“xls”)
to
if myFilename.ToLower.EndsWith(“xls”)

Auto select Gridview Row when Editing

Let’s say you have an editable Gridview, and you’d like to change the backcolor of the row you’re editing, when you click the ‘Edit’ link for that row. It’s very simple, and it’s done in the Gridview’s RowEditing Event.

First, you must set the SelectedIndex of the Gridview to the row you’re editing:

YourGridviewID.SelectedIndex = e.NewEditIndex


And, lastly, you must change the backcolor:

YourGridviewID.SelectedRow.BackColor = Drawing.Color.Pink

You can, of course, accomplish any other row formatting options here, also.

Full code and Subroutine is here:

Protected Sub YourGridviewID_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles YourGridviewID.RowEditing    YourGridviewID.SelectedIndex = e.NewEditIndex    YourGridviewID.SelectedRow.BackColor = Drawing.Color.PinkEnd Sub

How to Validate DataType only with a CompareValidator

Let’s say you have a textbox in which you need to make sure the user puts in a valid date. You can actually use a CompareValidator Control for this – - it’s so simple.

All you need to do is add the ControlToValidate property (the control/textbox in which a date must be entered), the erroressage you need (ErrorMessage property), the Operator property(change to DataTypeCheck), and the Type property (choose Date).

<asp:CompareValidator id=cv1
	Type="Date" Operator="DataTypeCheck"
	ErrorMessage="Must be a valid date" Display="Dynamic"
	ControlToValidate="txtDate"
	runat="server" />

Gridview Paging without a Datasource Control

If you are populating your Gridview with code, and not through a DataSource control, to be able to add paging, merely add a PageIndexChanging event for the Gridview:

Protected Sub YourGridview_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)   YourGridview.PageIndex = e.NewPageIndex   YourGridview.DataBind()End Sub

Templates – the Answer to a Consistent Look and Feel

Let’s say you have a company, and you have several websites within that company, but you need to have a consistent look and feel for each of them.
Create a website with the bare minimum, that you want to look the same in all websites, with Themes, navigation, and whatever else you want.

Then, in VS.Net 2005, click File/Export Template.  That’s all there is to it. The next time you want a website to look like this template, just click File/New, and choose your template (which will be under ‘My Templates’