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’)
Category Archives: Tips
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" />
Why is my button event firing twice?
The most common reason for double click events are situations in which the button markup includes an OnClick event, AND, the event handler (in your code) also has a ‘Handles’ statement, at the end of the event handler signature – -
Should I uninstall VS.Net 2005 before installing VS.Net 2008?
According to Scott Guthrie:
(http://weblogs.asp.net/scottgu/archive/2007/11/19/visual-studio-2008-and-net-3-5-released.aspx)
“Note that VS 2008 runs side-by-side with VS 2005 – so it is totally fine to have both on the same machine (you will not have any problems with them on the same box).”
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
Gridview Sorting – Only Certain Columns
To allow sorting on only certain columns in a Gridview, set the AllowSorting Property to True, but, for those columns you do NOT want to be sorted, using BoundFields, or TemplateFields, remove the SortExpression property.
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’