Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, June 9, 2014

How to Prevent Blank Page/white space when a Rectangle is hidden in RDLC

Hi All,
   I had a requirement where in I want to hide Rectangle (which had tablix and charts inside it) to show and hide based on query string parameters. The requirement was working fine when it is showing the Rectangle but when the rectangle is hiding it is generating Blank Page.

I searched a lot for a solution and came to know one trick which i would like to share with you .

I took a  new Table(Tablix) . 
Select the Row of the tablix and apply the same visibility expression on the Row visibility that I have applied on Rectangle .
Now drag the rectangle inside the Table row cell , in which you have applied expression .Thats it...!!!
Now check , it will not render Blank page / white space any more... :-)


Point to Note : One thing that only I have noticed and would like to tell you..First apply the Row visibility Expression and then only drag the Rectangle inside the Row cell of the table. This is because , I wasn't able to select the row , if you dragged the Rectangle first.. If you try to select Row, Rectangle gets selected....



Hope it helps!!!

Thanks ,
Nitin Sharma

Saturday, June 7, 2014

Convert input string to Title case in C#

Hi All,
   
     This post is about how to convert your input string into TITLE case in C#.

First , let me tell you what is title case : 

When writing a name or a title, it is a common convention to only use capital letters to start the principal words. This is called Title Case. When using Title Case, do not use capital letters for prepositions, articles or conjunctions unless one is the first word.

Examples:
  • Snow White and the Seven Dwarfs.
  • Newcastle upon Tyne / Brighton on Sea .
  • The DiMaggio Line .
  • The Last of the Mohicans .



You can convert your input string either in C# code  or in SQL function (scalar function) :

Convert to Title case in C# : 

  public static String TitleCaseString(String s)
        {
            if (s == null) return s;

            String[] words = s.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length == 0) continue;

                Char firstChar = Char.ToUpper(words[i][0]);
                String rest = "";
                if (words[i].Length > 1)
                {
                    rest = words[i].Substring(1).ToLower();
                }
                words[i] = firstChar + rest;
            }
            return String.Join(" ", words);

        }


Convert to Title case in SQL Server function :


CREATE FUNCTION udf_TitleCase (@InputString VARCHAR(4000) ) 
 RETURNS VARCHAR(4000) 
 AS 
 BEGIN 
 DECLARE @Index INT 
 DECLARE @Char CHAR(1) 
 DECLARE @OutputString VARCHAR(255) 
 SET @OutputString = LOWER(@InputString) 
 SET @Index = 2 
 SET @OutputString = 
 STUFF(@OutputString, 1, 1,UPPER(SUBSTRING(@InputString,1,1))) 
 WHILE @Index <= LEN(@InputString) 
 BEGIN 
 SET @Char = SUBSTRING(@InputString, @Index, 1) 
 IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&','''','(') 
 IF @Index + 1 <= LEN(@InputString) 
 BEGIN 
 IF @Char != '''' 
 OR 
 UPPER(SUBSTRING(@InputString, @Index + 1, 1)) != 'S' 
 SET @OutputString = 
 STUFF(@OutputString, @Index + 1, 1,UPPER(SUBSTRING(@InputString, @Index + 1, 1))) 
 END 
 SET @Index = @Index + 1 
 END 
 RETURN ISNULL(@OutputString,'') 
 END


   Hope this will help!!
  Regards,
  Nitin  Sharma

Sunday, February 16, 2014

Make RDLC reports as alternate row color

Hi friends,
  Today I will let you know how to make the RDLC rows as alternate colors.
  Just select the row of the report in Design and press F4 , now you can see the properties.
  In the BackgroundColor , click on the Expression hyperlink and write the below expression :

 
=iif(RowNumber(
Nothing) mod 2, "LightBlue", "LightCyan")


Thats it.. you can change the color as you need. I have taken as LightBlue and LightCyan as an example.

Thanks,
Nitin Sharma

Monday, September 16, 2013

How to find 2nd,3rd,4th ..... nth largest / smallest salary in a table

Hi All ,
   Just thought to share with you some simple SQL query that is used very frequently in our projects and mostly asked in interviews as well.
It is " Write a SQL Query to find the 2nd, 3rd, 4th ...........nth largest salary from a table".






Note  here N , N is equal to which largest salary you want to find. Like if you want to find 2nd highest , it will be Top 1 , if you want to find 3rd highest it will be Top 2 , if you want to find 4th highest it will be Top 3 and so on........

So Here is the SQL Query to find out the 3rd largest query from a table :



Similarly if you want to find the smallest salary , the above will remain same except you have to replace :
1)MAX(Salary) with MIN(Salary)  
2) Order by Salary Desc with Order by Salary Asc

Here's the query to find the 2nd smallest salary from the table ...




Thanks,
Nitin Sharma

Tuesday, September 10, 2013

Interview Questions :- .NET,ASP.NET , SQL Server , C#, SharePoint, Javascript, Jquery

Hey All,
   Below are some Questions ( not answers :-) ) that are asked in interviews that I have faced. Hope they prove useful to you for your preparation at some point of time.

 The Questions are mixed of : ASP.NET , SQL Server , .NET Framework , SharePoint , C# , OOPS


  1. What are magic tables in SQL Server 
  2.  How many and what are temp tables in Sql Server ?
  3.  How will you call trigger in Sql Server ?
  4.  How will you call function in Sql Server ?
  5.   There are 2 tables A and B having 3 columns each...What is the output of " Select * from A,B?
  6.   What is the maximum number of Clustered indexes a table can have ?
  7.  when you update a table in SQL Server management studio .., what exactly happens ?
  8.  What is project life cycle?
  9.  What are the differences b/w SQL Server 2008 and SQL Server 2012?
  10.  What is the difference between Site Template and Site Definition?
  11.  What are Generics in C# ? What is the use of it ?
  12.  Protected vs Protected Internal in C# ?
  13.  can we create master page through sharepoint desginer ?
  14.  what is the difference between creating items through sharepoint designer and sharepoint server?
  15.  ASP.NET page life cycle?
  16.  when can we change page's Viewstate ? on which event ?
  17.  can you create site template ? how ?
  18.  what is the prurpose of SPSecurity class in SharePoint ?
  19.  what is Code access security in .NET ?
  20.  There are 2 tables A and B... A has 3 columns , 5 rows ... B has 2 columns , 3 rows ..What will be the output of  a) Select * from A,B b) cross join between 2 tables (how many rows and columns will a cross join between 2 tables return ?)
  21.  What are Cross joins ?
  22. how will update sharepoint list ? write the code.
  23.  what are extension methods in C#?
  24.  what are anonymous methods in c#?
  25.  why multiple inheritance is not possible in C#?
  26. what is a site column in SharePoint?
  27. Difference b/w sandboxed and Farm solution?
  28.  Difference b/w Web Parts and Visual Web Parts.
  29.  how to make a master page in Sharepoint Site?
  30.  abstract class vs interfaces
  31.   Explain Sharepoint Object Model
  32.  What are SPutility and SPMetal in SharePoint ?
  33.  What is .wsdl file in a web service ? 
  34.  how many ways you handle erors in a web page ?
  35.  HttpUtility class vs Http Module
  36.  Can you run JSP pages in IIS ? How ?
  37.  which exception will first occur ?inside BAL , DAL , or UI layers'?
  38.  What does theme in CSS contains ?
  39.  What are views in SQL Server ? can we perform DML in Views?
  40.  User Defined Functions Vs Stored Procedure?
  41.  Difference b/w SQL variable @ and @@
  42.  Difference between Hidden Field vs Viewstate ?
  43.  Boxing and Unboxing difference
  44. What are extension methods and extension classes in C#?
  45.  Inproc vs Outproc in Stored Procedures?
  46.  HTTP module vs HTTP handler
  47.  HTTP get vs HTTP Post?
  48. Abstraction vs Encapsulation
  49.  overloded methods can have different return type?
  50.  static class vs Sealed classes .. why are they used ?
  51.  polymorphism in C#
  52.  types of triggers in SQL SERVER
  53.  Left outer join in SQL SERVER
  54.  What are indexes in SQL SERVER
  55.  What are cursors in SQL Server
  56.  What is the new keyword in Method overriding?
  57.  What are different types of views in SQL Server?
  58.  what is a $ sign in Jquery ?
  59.  How will you enable a website to run only for indian country?
  60.  what are lambda expressions in C#?
  61.  what is difference between array and arraylist ? when to use what and which one is faster?
  62.  what is datareader ? how is it different from dataset?
  63.  What are out, ref parameter in C# method?
  64.  For vs ForEach loop
  65. what is the benefit of putting the .wsp or solution file inside GAC and inside BIN?
  66.  what is RunWithElevatedPrivileges in SharePoint?
  67.  what is the difference between webpart and Visual WebPart?
  68.  what is the difference between Sytem.web.UI.WebControls.Webparts.WebPart vs Microsoft.Sharepoint.WebPart?
  69.  Abstraction Vs Encapsulation
  70.  What is viewstate ? Can you store collections in Viewstate?
  71.  What is the difference between Stored Procedures and User Defined Functions in Sql Server ?
  72.  What is LINQ and advantages of LINQ ?
  73.  I have a collection of strings , Which will be more efficient LINQ or normal for / for each loop?
  74.  What are the ways of storing information into clients computers?
  75.  What are Selectors in Jquery ?
  76.  What are the advantages of Entity Framework?
  77.  Request vs Response Cycle of a Page?
  78. Limitations of sharepoint designer
  79. how will you create master pages and how will you deploy?
  80.  what is a feature ? how will you deploy feature?
  81.  how will you associate one content type to 3 lists ?
  82.  What is caching ? How many types of caching is there?
  83.  What is the difference between caching and session ?
  84.  Generic list vs arraylist vs array in terms of performance and why ?
  85.  why serialization is done and what are the types of serialization?
  86.  What is the difference b/w caching and Session ?
  87. Difference b/w httphandlers and http modules.
  88.  what are the limitations or disadvandtages of Generics
  89.  how to find the 4th highest salary in table?
  90.  .net 2.0 vs .net 4.0 features
  91.  What is garbage collector ?
  92.  garbage collector dispose vs finalize 
  93.  what are table valued functions in SQL Server
  94.  What are shared assemblies
  95.  What is Isnull and collasce operator in SQL Server?
  96.  interface vs abstract class
  97.  return server date in javascript 
  98.  how to increment each salary of all employees with 500 
  99.  how to work with cookies in asp.net 
  100.  javascript function on html control click and asp.net control click

    ALL THE VERY BEST !!!

Saturday, February 18, 2012

Fetch / Get Current Date from SQL Server in a specific format

Hi All,
      Working on Saturday is really a pain...Phew...!!!!...

I came across a requirement where you have to get a Current Date from Sql Server in the following format :

DD - MM - YYYY (note the hyphen in between)


So, here is the script with the Sql Server Replace function...:



Select Replace(Convert(varchar(11),Getdate(),106),' ',' - ') as 'Today'





And the Result is :





Thanks,
Nitin Sharma

Happy Coding....

Wednesday, November 23, 2011

Apply Custom Style to Gridview using Style class and ApplyStyle method

Hi,
  In this post I will apply my own style to the Gridview Control.


1) Add a new page say "GV.aspx" in your asp.net website/web application.


2) Drag and drop  Gridview and a button control from the toolbox on the page.


3) Bind the Gridview on page load event. I have used LINQ to SQL In this example for binding the Gridview.
 The database used is Northwind. The name of LINQ class here is DB and I have added two tables Product and Category.



DBDataContext db = new DBDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        // Simple LINQ query
        var products = from p in db.Products
                       where p.Category.CategoryName == "Beverages"
                       select p;


        GridView1.DataSource = products;
        GridView1.DataBind();
    }

Note that I have instantiated DataContext class outside the Page Load event making it Global to the page.

4) Now I will create a new style from the Style class and apply it to the Gridview using ApplyStyle() method.On the button Click Event add this piece of code:

    protected void btnStyle_Click(object sender, EventArgs e)
    {
        Style myStyle = new Style();
        myStyle.ForeColor = System.Drawing.Color.Red;
        myStyle.BackColor = System.Drawing.Color.Turquoise;
        myStyle.Font.Bold = true;
        myStyle.BorderStyle = BorderStyle.Solid;
        GridView1.ApplyStyle(myStyle);
    }

5) Run it and click on the button and see the output.Below is the screenshot showing before and after applying style.

Gridview before applying style



Gridview after applying style (Click on Apply Style Button)



That's it..!

Thanks,
Nitin Sharma


Tuesday, August 10, 2010

Database Back up using SQL Query

Hi All,
Today i came through a problem where in i have to take Back up of the SQL server database .
It is a simple query :

BACKUP DATABASE TestDB TO DISK='D:\MyDatabase.bak' with FORMAT


Where
"TESTDB" => Your Database Name
"D:\" => Any local Hard Drive C,D,E,F or any;
"MyDataBase" => Any name by which you want to take Back up.


Thanks,
Nitin Sharma

Monday, February 8, 2010

How to find the Size of Database File and Log File

Hi all,
I just went through Pinal Dave's Blog Today and found a very simple script for finding out the Database File Size and the Size of the Log file.Here it how it goes :

SELECT DB_NAME(database_id) AS DatabaseName,
Name AS Logical_Name,
Physical_Name, (size*8)/1024 SizeMB
FROM sys.master_files
WHERE DB_NAME(database_id) = 'AdventureWorks'
GO

For detailed post you can visit here


Thanks Pinal for such a wonderful script..



Thanks ,

Nitin Sharma


Tuesday, December 29, 2009

Get Date of All Weekdays or Weekends of the Year

Hi guys,

I was going through the Pinal Dave's Blog today...He has posted a wondeful post today ....You guys may find it very very useful...

The topic is Get Date of All Weekdays or Weekends of the Year.

http://blog.sqlauthority.com/2009/12/29/sql-server-get-date-of-all-weekdays-or-weekends-of-the-year/



Thanks,
Nitin Sharma

Tuesday, May 5, 2009

SQL SERVER CURSOR

SQL Server cursors are database objects used to manipulate data in a set on a row-by-row basis. You can fetch cursor rows and perform operations on them in a loop just like using any looping mechanism found in any other programming language. Before you can use a cursor, you need to declare it.


A Simple Example of CURSOR

USE [Test]

GO

/****** Object: StoredProcedure [dbo].[ResetSlNoInPart] Script Date: 05/05/2009 10:59:14 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

Create PROCEDURE [dbo].[ResetSlNoInPart]

(

@Filter_AC int -- For which AC to perform action

)

AS

BEGIN

DECLARE

@ID int, -- To identify record in which SLNOINPART is to be updated

@AC int, -- AC

@Part int, -- Part

@OldAC int, -- Old AC (for comparison in loop)

@OldPart int, -- Old Part (for comparison in loop)

@SerialNo int -- Serial No.

-- Create a new cursor that gets the ID, AC and Part for selected AC ordered by requirements

DECLARE SerialLoop CURSOR FOR

SELECT id, ac_no, part_no

FROM E_Draft_2

WHERE ac_no=@Filter_AC

ORDER BY ac_no ASC, part_no ASC, section_no ASC, house_no_en ASC, age DESC

-- Initialize the comparison variables

SET @OldAC=0

SET @OldPart=0

-- Open and start the loop

OPEN SerialLoop

FETCH NEXT FROM SerialLoop

INTO @ID, @AC, @Part

-- Repeat the loop till records exist

WHILE @@FETCH_STATUS=0

BEGIN

-- Compare and see whether Part or AC have changed

IF (@OldAC<>@AC) OR (@OldPart<>@Part)

BEGIN

-- If AC or Part changed, reset serial to 1 and update the comparison varialbes

SET @SerialNo=1

SET @OldAc=@AC

SET @OldPart=@Part

END

-- Update the current record with whatever serial no is current

UPDATE E_DRAFT_2

SET SLNoInPart=@SerialNo

WHERE ID=@ID

-- Debug print

PRINT 'AC='+Convert(varchar, @ac)+', part='+Convert(varchar, @part)+', sl='+Convert(varchar,@serialno)

-- Increment Serial no

SET @SerialNo=@SerialNo+1

-- Move to next record

FETCH NEXT FROM SerialLoop

INTO @ID, @AC, @Part

END

-- Cleanup

CLOSE SerialLoop

DEALLOCATE SerialLoop

END



Thanks,
Nitin Sharma

Monday, March 9, 2009

Multiple Joins in LINQ



Hi All,
This Post will show how to implement multiple Joins in LINQ.



using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

public partial class JoinsInLINQ : System.Web.UI.Page

{

DemoDataContext DC = new DemoDataContext();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

Bind();

}

}

private void Bind()

{

// How to Apply Joins in LINQ

var items = from P in DC.Emps

join Ep in DC.EmpProducts on P.SINo equals Ep.SINo

join TT in DC.TextTables on P.SINo equals TT.SINo

select new

{

Name = P.Name,

Age = P.Age,

Country = P.Country,

State = P.State

};

GVInJoins.DataSource = items;

GVInJoins.DataBind();

}

}


On Debugging the Query we will find it like as........



{SELECT [t0].[Name], [t0].[Age], [t0].[Country], [t0].[State]
FROM [dbo].[Emp] AS [t0]
INNER JOIN [dbo].[EmpProduct] AS [t1] ON [t0].[SINo] = [t1].[SINo]
INNER JOIN [dbo].[TextTable] AS [t2] ON [t0].[SINo] = [t2].[SINo]
}




Now finally Bind the Output to the Gridview and its ready now...




Thanks to LINQ :)


Thanks,

Nitin Sharma