Monday, July 30, 2007

Accessing Cookies from Javascript

I recently had occasion to access the contents of a cookie stored on the clients computer using Javascript. I found the following javascript function very handy. I wander if something similar is built into javascript:

function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));

Stylesheet Link

Probably the easiest method of adding a link to an additional style sheet in code is this:

Page.Header.Controls.Add(new StylesheetLink("/css/mystyle.css"));

Thursday, July 26, 2007

A Look at GUIDs

Not many people enjoy using Globally Unique Identifiers (GUIDs), partly because they are cumbersome to type and work with for humans. However they fill a need and can provide some interesting benefits. SQL Server expert Andy Warren takes us through what a GUID is and how you can use it easily in your code.

The difference between Where and Group By

"The WHERE clause (# 3) evaluates data before the GROUP BY clause does. When you want to limit data after it's grouped, use HAVING. Often, the result will be the same whether you use WHERE or HAVING, but it's important to remember that the clauses are not interchangeable. Here's a good guideline to follow when you're in doubt: Use WHERE to filter records; use HAVING to filter groups.
Usually, you'll use HAVING to evaluate a group using an aggregate. For instance, the following statement returns a unique list of ZIP codes, but the list might not include every ZIP code in the underlying data source: SELECT ZIP, Count(ZIP) AS CustomersByZIP FROM Customers GROUP BY ZIP HAVING Count(ZIP) = 1
Only those groups with just one customer make it to the results. "

What Query Plans are in SQL Server's Memory?

"SQL Server memory is primarily used to store data (buffer) and query plans (procedure cache). In this article I'll show how much memory is allocated to the procedure cache (RAM). I'll explain how to determine what plans are in the cache and how often they're used.

SQL Server stores the procedure cache in 8KB data pages. You can use the dynamic management view sys.dm_os_memory_cache_counters to provide a summary of how the cache is allocated using this query"

LEFT([name], 20) as [name],
LEFT([type], 20) as [type],
[single_pages_kb] + [multi_pages_kb] AS cache_kb,
FROM sys.dm_os_memory_cache_counters
Order By single_pages_kb + multi_pages_kb DESC

What Data is in SQL Server's Memory?

"SQL Server memory is primarily used to store data (buffer) and query plans (cache). In this article I'll show how much memory is allocated to the data buffer (or RAM). I'll explain how to determine what tables and indexes are in the buffer memory of your server.

SQL Server stores its data in 8KB data pages. As these pages are read off disk they are stored in memory. This is referred to as buffer memory. A list of all the data pages in memory is stored in the dynamic management view sys.dm_os_buffer_descriptors."

Wednesday, July 25, 2007

Some Tips and Tricks for using an ObjectDataSource with a Gridview

Some Tips and Tricks for using an ObjectDataSource with a Gr... "This article attempts to show a few tricks with the new Gridview that we have in 2.0. This article addresses using the ObjectDataSource to populate the Gridview. In this example the ObjectDataSource returns a generic collection of an object. There is some special coding that needs to happen to get the sorting of colums to work. I also show how to use the pager template to do custom navigation. I have also included a simple Gridview printing example." A great reference for using the GridView control with a custom ObjectDataSource so you can do your own paging and sorting; an absolute must when working with large data sets.

using sp_executesql

T-SQL Programming Part 4 - Setting Variables in Calling T-SQ... use
use Northwind
declare @RECCNT int
declare @ORDID varchar(10)
declare @CMD Nvarchar(100)
set @ORDID = 10436
SET @CMD = 'SELECT @RECORDCNT=count(*) from [Orders]' +
' where OrderId < @ORDERID' print @CMD exec sp_executesql @CMD, N'@RECORDCNT int out, @ORDERID int', @RECCNT out, @ORDID print 'The number of records that have an OrderId' + ' greater than ' + @ORDID + ' is ' + cast(@RECCNT as char(5))

The name 'Session' does not exist in the current context

Geekpedia • ASP.NET: The name 'Session' does not exist in th... " This error normally occurs when you are trying to access the value of a session variable such as Session["UserID"] and you're not doing that from a webform, usercontrol or a class that inherits from System.Web.UI. In this situation you can still access the session variable, but using a different path. For example to access a session variable named UserID you would normally use Session["UserID"]; however, if the error The name 'Session' does not exist in the current context is returned, use the following path for retrieving the value of the session variable: " The fix is to use: HttpContext.Current.Session["variable"];

Tuesday, July 24, 2007

SQL Case - Syntax error converting the varchar value

variable order by when mixing datatypes? [Archive] - dBforum... Quote that helped me figure out a case problem with strings and ints: " In T-SQL, a CASE expression has its data type determined before the query is processed, and the type of the CASE expression is the lowest precedence type that is at least as high a precedence of each of the CASE alternatives. This means that a CASE expression with both varchar and int alternatives will be typed as int. The result of this is that whenever any of the alternatives is evaluated, it will be interpreted as an int, which in the case of a non-numeric varchar string, can cause a run-time error. If the value of a parameter is such that the non-numeric varchar is never accessed, this won't cause an error. You might think it would be better if the CASE expression weren't typed, but that would leave undetermined the question of how something like MAX(CASE when ColumnA = 0 then ColumnB else ColumnC end) should be evaluated. There is another alternative to handle this, and that is to cause the CASE expression to be of type sql_variant (needs SQL Server 2000), to which any numeric or varchar value can be cast, and which will preserve the correct ordering of each column according to its base type: "

GridView and the ObjectDataSource

TheDotNetGuy "Since, we are working with ObjectDataSource control we need to make an entity class. Take a look at the simple User class below:" It took me quite a while to figure out some of the subtle features that needed to be set in order to implement a custom paging setup for the GridView. These are the three most important things I learned: 1. an entity class is required for the ObjectDataSource to run off of. 2. If the ObjectDataSource's entity class is inside a namespace then the entire name (namespace included) must be given to the ObjectDataSource.TypeName and not just the class name. 3. It isn't enough to have public fields in the entity class as the GridView will only look at public properties (essentially a public field that acts like a method with get and set statements). Here is the other website that really helped out with this: "Well, it's not that I buy into what JayBaz says about properties. I was just hastily writing some test code without any regard to my flippancy. As soon as a changed the public fields to properties, TADA!" Here were the errors I was gettings: 1. The type specified in the TypeName property of ObjectDataSource 'objItem' could not be found. 2. The data source for GridView with id 'grdContacts' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

Wednesday, July 18, 2007

Temporarily Changing sa password

Temporarily Changing an Unknown Password of the sa Account What if you need to log into a SQL server using a specific sql account? What if you don't know the password to said account, but you can't permanentally change the password because it is used other other processes? I can't really think of a reason this would be needed, however when I saw the article my first thought was "what a hacker hole". I haven't read the article in its entirety but it is a good thing to keep in the back of my mind as a possible security hole.

Tuesday, July 17, 2007

T-SQL Temporary Tables vs. Table variables

Lakeside SQL - Articles — Temporary Tables vs. Table Variabl...

1. There is no universal rule of when and where to use temporary tables or table variables. Try them both and experiment.

2. In your tests, verify both sides of the spectrum – small amount/number of records and the huge data sets.

3. Be careful with migrating to SQL 2005 when you use complicated logic in your stored procedures. The same code can run 10-100 times slower on SQL server 2005!

DataTable.Select problem, not finding rows

DataTable.Select problem in ASP.NET 2.0 I have noticed a problem with the DataTable.Select statement where .NET appears to convert all the data to string format. When I tried to do a select on an integer column passing in an integer value with a length that was different from most of the other integer values in the datatable, it wouldn't return any rows. It was really odd and I think should still work. This is what I call a but in .NET Anyway, the fix is to put ticks around the integer parameter in your select statement. Here is someone else's take on the matter: The data in your table probably isn't in integer form. Depends on you xml schema if you have one. If not they are probably all text type. Depending on how much you are using the data you might also want to look into serializing your xml and putting the data into a class that inherits the arraylist. This article may be of some assistants (read part one and two).

uniqueid clientid not evaluating

The CodeExpressionBuilder - Infinities Loop The unique ID and client ID of a control isn't established until its part of the control tree. Expressions are evaluated before they are added to the control tree, so its too soon. You could just use the databinding syntax instead, <%# button1.ClientID %>, and then be sure to call DataBind.

HtmlLink for javascript in head tag using jsLink

ASP.NET Resources - Code Blocks Inside Master Pages Cause Tr... javascript script tag dynamic in head element. similar to htmllink but for javascript.