SELECT SERVERPROPERTY('productversion') As Version, SERVERPROPERTY ('edition') As Edition, SERVERPROPERTY ('productlevel') As Updates
The first columns shows current version as number, the second one is the edition (standard, express etc.) and the third one shows which version level the server belongs to (SP1, SP2 etc.)
Trick to start web application with another SQL Server database
The following exception is thrown when you try to use one application with the another database. The problem is caused by asp.net membership provider which cannot recognize its schema.
The 'System.Web.Security.SqlMembershipProvider' requires a database schema compatible with schema version '1'. However, the current database schema is not compatible with this version. You may need to either install a compatible schema with aspnet_reqsql.exe (available in the framework installation directory), or upgrade the provider to a newer version.
First of all be sure the aspnet membership provider is registered on that database using aspnet_regsql.exe.
Then the solution is: taking the web application offline and then online using Web Site Administration Tool. That makes some changes in web.config in the pages tag and globalization if needed. Then you can go!
The 'System.Web.Security.SqlMembershipProvider' requires a database schema compatible with schema version '1'. However, the current database schema is not compatible with this version. You may need to either install a compatible schema with aspnet_reqsql.exe (available in the framework installation directory), or upgrade the provider to a newer version.
First of all be sure the aspnet membership provider is registered on that database using aspnet_regsql.exe.
Then the solution is: taking the web application offline and then online using Web Site Administration Tool. That makes some changes in web.config in the pages tag and globalization if needed. Then you can go!
SQL pagination: get fixed number of rows page by page
See the following examples:
SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [SortNum], * FROM MyTable) As Tmp
WHERE SortNum Between 11 AND 20
or
WITH Tmp As
SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [SortNum], * FROM MyTable)
SELECT * FROM Tmp
WHERE SortNum Between @StartRow AND @StartRow+@NumberOfRows-1
or
WITH Tmp As
SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [SortNum], * FROM MyTable)
SELECT * FROM Tmp
WHERE SortNum Between (@page-1)*10+1 AND (@page)*10
The first one gives the second page of 10 records.
The second example shows the usage of WITH keyword and getting rows passed as parameter of stored procedure.
The third one shows you how to use 1 passed parameter as number of the desired page.
SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [SortNum], * FROM MyTable) As Tmp
WHERE SortNum Between 11 AND 20
or
WITH Tmp As
SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [SortNum], * FROM MyTable)
SELECT * FROM Tmp
WHERE SortNum Between @StartRow AND @StartRow+@NumberOfRows-1
or
WITH Tmp As
SELECT ROW_NUMBER() OVER (ORDER BY Id) AS [SortNum], * FROM MyTable)
SELECT * FROM Tmp
WHERE SortNum Between (@page-1)*10+1 AND (@page)*10
The first one gives the second page of 10 records.
The second example shows the usage of WITH keyword and getting rows passed as parameter of stored procedure.
The third one shows you how to use 1 passed parameter as number of the desired page.
How to set your master page by the nested page
The PreInit Page event n the nested page can be used for setting some Master page parameters.
protected void Page_PreInit(object sender, EventArgs e)
{
(this.Master as MasterType).Property = value;
}
Then the Master page will be loaded and rendered using these settings.
Master is the reference to the Master page. MasterType is the type of master page used.
Property is the public variable which must be set before loading page.
Using Page_PreInit the Master page can be set dynamically.
protected void Page_PreInit(object sender, EventArgs e)
{
(this.Master as MasterType).Property = value;
}
Then the Master page will be loaded and rendered using these settings.
Master is the reference to the Master page. MasterType is the type of master page used.
Property is the public variable which must be set before loading page.
Using Page_PreInit the Master page can be set dynamically.
Sql Date Manipulation
DATEPART (datepart, date) returns the desired part of the date specified by datepart parameter: year,
quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond.
Example: DATEPART(month, GETDATE()) returns current month number
DAY(date), MONTH(date), YEAR(date) return respectively the number of the day, month or year of the passed date.
To add or subtract date parts from a given date use this function:
DATEADD (datepart , number, date)
where datepart is one of the following parameter (datepart): year,
quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond.
Example: DATEADD(day, -7, GETDATE()) return the date week ago.
DATEDIFF (datepart, startdate, enddate) gives you the difference between two dates in desired date part (see DATEPART date parts above). Startdate should be date before end date. Otherwise a negative number will be returned.
Example: DATEADD(day, DATEADD(day, -7, GETDATE()), GETDATE()) return 7 days.
quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond.
Example: DATEPART(month, GETDATE()) returns current month number
DAY(date), MONTH(date), YEAR(date) return respectively the number of the day, month or year of the passed date.
To add or subtract date parts from a given date use this function:
DATEADD (datepart , number, date)
where datepart is one of the following parameter (datepart): year,
quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond.
Example: DATEADD(day, -7, GETDATE()) return the date week ago.
DATEDIFF (datepart, startdate, enddate) gives you the difference between two dates in desired date part (see DATEPART date parts above). Startdate should be date before end date. Otherwise a negative number will be returned.
Example: DATEADD(day, DATEADD(day, -7, GETDATE()), GETDATE()) return 7 days.
Ajax Error Message:The state information is invalid for this page and might be corrupted
The message is shown when a page with partial postback regions (update panels) is refreshed by F5 or browser's refresh button and then a new partial postback request is performed. In this case the information stored in the page has been changed and causes not to be possible to perform partial postback. For pages like these the page parameter EnableEventValidation must be set to false:
<%@ Page EnableEventValidation="false" ....
Another case is described here: http://support.microsoft.com/kb/323744
<%@ Page EnableEventValidation="false" ....
Another case is described here: http://support.microsoft.com/kb/323744
How to execute string (custom built sql query ) in a stored procedure
sp_executesql executes string in a stored procedure. It is used in more complex queries which we need to build concatenating strings. The string is run as a batch. The following example gets the TOP records from a table matching predefined conditions.
CREATE PROCEDURE [GetTOPRecords]
@count varchar(6),
@conditions nvarchar(300)
AS
BEGIN
DECLARE @SQL nvarchar(1000)
SET
@SQL='SELECT
TOP '+ @count + ' * FROM RecordsTable
WHERE ' + @conditions
EXEC sp_executesql @SQL
END
The @count and @conditions are passed as parameters. The execution string is built run-time and executed. If you don't need any conditions here just pass the string ' 1=1 ' as @conditions.
CREATE PROCEDURE [GetTOPRecords]
@count varchar(6),
@conditions nvarchar(300)
AS
BEGIN
DECLARE @SQL nvarchar(1000)
SET
@SQL='SELECT
TOP '+ @count + ' * FROM RecordsTable
WHERE ' + @conditions
EXEC sp_executesql @SQL
END
The @count and @conditions are passed as parameters. The execution string is built run-time and executed. If you don't need any conditions here just pass the string ' 1=1 ' as @conditions.
How to build unsafe C# code with Visual Studio
Unsafe code may only appear if compiling with /unsafe
This appears as an error when you try to compile assembly which contains unsafe code in release mode.
You must change the compiler options using project's Properties page -> Build -> and check Allow Unsafe Code. Consider if you want to use unsafe code :)))
This appears as an error when you try to compile assembly which contains unsafe code in release mode.
You must change the compiler options using project's Properties page -> Build -> and check Allow Unsafe Code. Consider if you want to use unsafe code :)))
Javascript: How to check if the variable is a number?
There are some ways for doing this but I think the following is the most easiest and convenient for use:
if (! isNaN(variable)) {
//code here
}
isNaN comes from is Not A Number and can be used for understanding if the variable is a number.
if (! isNaN(variable)) {
//code here
}
isNaN comes from is Not A Number and can be used for understanding if the variable is a number.
How to maintain the view state of dynamically added control in an asp.net page
First of all you must read these articles:
The viewstate of a dynamically added control can be saved for maintaining the state of the control after postback.
This can be achieved by adding the controls before the Load Viewstate stage (see the page stages in the articles above). Therefore the viewstate information ( control value) from the request will be applied to already added controls as they are defined already in the page.
The right place of adding dynamic controls is Page_Init. Use Page_Init also for defining dynamically master pages and themes.
The viewstate of a dynamically added control can be saved for maintaining the state of the control after postback.
This can be achieved by adding the controls before the Load Viewstate stage (see the page stages in the articles above). Therefore the viewstate information ( control value) from the request will be applied to already added controls as they are defined already in the page.
The right place of adding dynamic controls is Page_Init. Use Page_Init also for defining dynamically master pages and themes.
Understanding AutoEventWireup attribute in Asp.net page. Event handlers.
The AutoEventWireup attribute indicates if the page events are auto-wired up. If the attribute is set to true .Net framework searches automatically for methods which names are created in the way Page_xxxxx (Page_Load, Page_Init etc.) and connects them to the events. Otherwise the developer or the environment must do that using delegates:
this.Load += new System.EventHandler(this.Page_Load);
for all handled page events. Setting AutoEventWireup to false is the better way because of the performance -> the framework uses only the methods with code!
The attribute can be set on several places: in machine.config, web.config and for every page.
Note:
The note is not connected directly to the page events, but to events of postback controls like button, linkbutton etc.
In VB.NET the event handlers can be attached to the events using the keyword "Handles".
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
This method will handle the event Click and will work like adding the attribute
OnClick="LinkButton1_Click" to the LinkButton tag. But NEVER USE both of the ways at the same because this will invoke the method twice!!
Consider using keyword "Handles" OR OnEvent="", but never use them together.
this.Load += new System.EventHandler(this.Page_Load);
for all handled page events. Setting AutoEventWireup to false is the better way because of the performance -> the framework uses only the methods with code!
The attribute can be set on several places: in machine.config, web.config and for every page.
Note:
The note is not connected directly to the page events, but to events of postback controls like button, linkbutton etc.
In VB.NET the event handlers can be attached to the events using the keyword "Handles".
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
This method will handle the event Click and will work like adding the attribute
OnClick="LinkButton1_Click" to the LinkButton tag. But NEVER USE both of the ways at the same because this will invoke the method twice!!
Consider using keyword "Handles" OR OnEvent="", but never use them together.
How to covert System.Drawing.Color to a HTML/CSS color value
The System.Drawing.Color represenation of the color including the alpha color element:
alpha, red, green, blue.
The process is simple - convert the decimal color representation to a hexadecimal, then remove the alpha hexadecimal value from the string and concatenate to '#'.
Dim HTMLColor As String = Hex(color.ToArgb())
HTMLColor = "#" + HTMLColor.Substring(2)
where "color" is an instance of System.Drawing.Color
There is a difference between VB.NET and C# in getting the hexadecimal value as string. The C# fans can use the folloing code:
string HTMLColor = color.ToArgb().ToString("X8");
HTMLColor = "#" + HTMLColor.Substring(2);
The opposite process can be accomplished by ColorTranslator' services:
Dim color As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#e5e5e5")
alpha, red, green, blue.
The process is simple - convert the decimal color representation to a hexadecimal, then remove the alpha hexadecimal value from the string and concatenate to '#'.
Dim HTMLColor As String = Hex(color.ToArgb())
HTMLColor = "#" + HTMLColor.Substring(2)
where "color" is an instance of System.Drawing.Color
There is a difference between VB.NET and C# in getting the hexadecimal value as string. The C# fans can use the folloing code:
string HTMLColor = color.ToArgb().ToString("X8");
HTMLColor = "#" + HTMLColor.Substring(2);
The opposite process can be accomplished by ColorTranslator' services:
Dim color As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#e5e5e5")
Javascript string functions
Let's see how to use replace() function for replacing string with another one. The specialty here is the usage of regular expressions to replace the string if it occurs more than once in the source string.
replace('aa','a'); is gonna replace only the first occurrence of the 'aa'.
replace(/aa/g, 'a') will replace all occurrences of the 'aa' string.
replace(/A/i, 'b') will replace the occurrences of 'A' or 'a' with 'b' - case insensitive.
Very common problems are the special character replacement. These characters should be escaped using \.
replace(/\./g, '') will replace all occurrences . with an empty ('') string.
replace('aa','a'); is gonna replace only the first occurrence of the 'aa'.
replace(/aa/g, 'a') will replace all occurrences of the 'aa' string.
replace(/A/i, 'b') will replace the occurrences of 'A' or 'a' with 'b' - case insensitive.
Very common problems are the special character replacement. These characters should be escaped using \.
replace(/\./g, '') will replace all occurrences . with an empty ('') string.
Unicode and hosted PHP
To make apache to work with unicode you must add into .htaccess the following:
php_value default_charset utf-8
AddDefaultCharset utf-8
.htaaccess geve you the ability to change PHP settings without changing php.ini
How to get the whole url on the page in PHP
Here is a usefull function for getting current url:
function GetCurrentURL() {
$url = '';
if ($_SERVER["HTTPS"] == "on")
{
$url .= "https://";
}
else
{
$url .= "http://";
}
if ($_SERVER["SERVER_PORT"] != "80") {
$url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $url;
}
function GetCurrentURL() {
$url = '';
if ($_SERVER["HTTPS"] == "on")
{
$url .= "https://";
}
else
{
$url .= "http://";
}
if ($_SERVER["SERVER_PORT"] != "80") {
$url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $url;
}
Subscribe to:
Posts (Atom)