Posts

Showing posts from 2008

Variable Value in Top Statement

i want create a Procedure that behave like this: declare @cnt int set @cnt =10 select top @cnt * from sw_user Where sw_user contains 20 records, and i want the variable to be the criteria of Top statement. so i can use this for a solution for this type of problem CREATE PROC Top10 @cnt int AS SET ROWCOUNT @cnt SELECT * FROM sw_user but some cases ROWCOUNT might not be work... so what, Note : This statement however is possible in SQL 2005, just add "()" in your variable like so: declare @cnt int set @cnt =10 select top (@cnt) * from sw_user

LINQ to XML : XElement and XAttribute

var _CountryLists = (from c in SW_Countries select new { c.ID, c.Name, c.Abbreviation }); var xml = new XElement("root",from c in _CountryLists select new XElement("nodes", new XAttribute("id", c.ID), new XAttribute("abb", c.Abbreviation), new XAttribute("name", c.Name))); return xml.ToString(); xml will return result like this... <root> <nodes id="13" abb="AUS" name="Australia" /> <nodes id="28" abb="MMR" name="Burma" /> <nodes id="33" abb="BRA" name="Brazil" /> <nodes id="35" abb="BTN" name="Bhutan" /> <nodes id="40" abb="CAN" name="Canada" /> <nodes id="43" abb="LKA" name="Sri Lanka" /> <nodes id="46" abb="CHN" name="China&qu

LINQ - C# Programming

Get DataSource In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source (customers) and the range variable (cust). //QEmp is an IEnumerable var QEmp = from e in Emp select e; Filter Probably the most common query operation is to apply a filter in the form of a Boolean expression. The filter causes the query to return only those elements for which the expression is true. The result is produced by using the where clause. The filter in effect specifies which elements to exclude from the source sequence. var QEmp = from e in Emp where e.Country == "India" select e ----AND Condition---- where e.Country == "India" && e.Name == "Ashvin" ----OR Condition---- where e.Country == "India" || e.Name == "Ashvin" Ordering The orderby clause will cause the elements in the

Parsing the comma separated values into a temporary table

(Works in both SQL Server 7.0 and 2000) Declare @OrderList varchar (500) set @OrderList = 'Order1,Order2,,Order3,,,Order4,Order5,' ; drop TABLE #TempList CREATE TABLE #TempList ( OrderID varchar (20) ) DECLARE @OrderID varchar (10), @Pos int SET @OrderList = LTRIM(RTRIM(@OrderList))+ ',' SET @Pos = CHARINDEX( ',' , @OrderList, 1) IF REPLACE(@OrderList, ',' , '' ) <> '' BEGIN WHILE @Pos > 0 BEGIN SET @OrderID = LTRIM(RTRIM( LEFT (@OrderList, @Pos - 1))) IF @OrderID <> '' BEGIN INSERT INTO #TempList (OrderID) VALUES (@OrderID) END SET @OrderList = RIGHT (@OrderList, LEN(@OrderList) - @Pos) SET @Pos = CHARINDEX( ',' , @OrderList, 1) END END SELECT * FROM #TempList t +-+-+-+-+-+ OUTPUT +-+-+-+-+-+ OrderID -------- -- Order1 Order2 Order3 Order4 Order5

AdRotator control in ASP.Net

Adrotator control is available in ASP.Net to make the task of rotating the advertisement images in a web form quickly and easily. It also enables you to display different types of images whenever you refresh the page. Adrotator control takes the information about the images that are to be displayed from an XML file which also have many other elements in it that are used by this control. Adrotator control is available in ASP.Net to make the task of rotating the advertisement images in a web form quickly and easily. It also enables you to display different types of images whenever you refresh the page. Adrotator control takes the information about the images that are to be displayed from an XML file which also have many other elements in it that are used by this control. First let us have a look at a sample advertisement file that is used by the adrotator control in ASP.Net. Let us assume that the advertisement XML file is named as advt.xml. This file might look like, < A

Differents between ExecuteReader, ExecuteNonQuery & ExecuteScalar

ExecuteReader : Use for accessing data. It provides a forward-only, read-only, connected recordset. ExecuteNonQuery : Use for data manipulation, such as Insert, Update, Delete. ExecuteScalar : Use for retriving 1 row 1 col. value., i.e. Single value. eg: for retriving aggregate function. It is faster than other ways of retriving a single value from DB.

SQL Server 2005 with XML Parameters

We frequently have requirement like Passing IDs in comma saperated value to the sql server for further processing. Generally what we do is just create loop and get the value from it and ... But SQL 2005 comes up with XQuery. Using this you can easly pass such type of data also some complex thing too. For example... DECLARE @productIds xml SET @productIds ='<Products><id>9</id><id>18</id><id>27</id></Products>' SELECT ParamValues.ID.value ('.','VARCHAR(20)')FROM @productIds.nodes('/Products/id')as ParamValues(ID) Which gives us the following three rows: 9 18 27

Page get postback twice in Mozilla!!!!

I face one problem in Mozilla but with other browser it works fine. The problem was like... the page get called twice!!! There is case when the server side code called twice, not lient side. After doing some good work found the solution, and was simpley good. The following line was causing the problem. <asp:image id="imgTest" runat="Server"/> In first look you can see there is no problem with it. But there is still problem; the missing ImageUrl. Mozilla send request to current page asking for an image, which we havent set yet; as the value get assign from server side, by this reason the only server side code is get executed twice!!!! Interesting, isn't it!! So here is the solution: <asp:image id="imgTest1" runat="Server" imageurl="abc.jpg"></asp:image>

Case Sensitive or Insensitive SQL Query

Suppose you need to perform a SQL query and you need for it to be case sensitive or case insensitive, and either your database is set up the opposite way or you're smart and you're trying to write your query so that it will work regardless of how the database may or may not be configured. For instance, consider this query: SELECT UserId, email FROM UserInfo WHERE email = 'ashvin@padhiyar.com' If your database contains an email for Bill like this one: ASHVIN@padhiyar.com then whether or not your query will return any rows will depend on the COLLATION for the database. If you want to ensure that you DO get results, you can force it to use a CASE INSENSITIVE collation like so: SELECT UserId, email FROM UserInfo WHERE email = 'ashvin@padhiyar.com' COLLATE SQL_Latin1_General_CP1_CI_AS Similarly, if you don't want it to return any rows unless you specify the correct case for the email, you would change the collation to replace _CI_ with _CS_ where CI is shor

Print Function By JavaScript

function PrintContent( Obj) { PrintWin=window.open('','PrintWin','directories=no, location=no, menubar=no, resizable=no, status=no, toolbar=no, scrollbars=no, width=10, height=10, left=1050, top=800'); PrintWin.document.write(" "); PrintWin.document.write(document.getElementById(Obj.id).outerHTML); PrintWin.location.reload(); PrintWin.print(); PrintWin.close(); } /************* Function call ***************/ btnPrint.Attributes.Add( "OnClick", "PrintContent(" + TblPrint.ClientID + ");");

Email Validation by Javascript

function EmailCheck() { var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var str =document.getElementById('<%= TxtTo.ClientID %>').value; if(reg.test(str) == false) { alert('Invalid Email Address'); document.getElementById('<%= TxtTo.ClientID %>').focus(); } }

Getting Top N Rows in SQL2K and SQL2K5 [Non Tech Ppl Plz Excuse]

SQL 2K declare @topN int set @topN = 10 set rowcount @topN select * from [TABLE] /***************************/ SQL 2K5 declare @topN int set @topN = 5 select top ( @topN ) * from [TABLE]