Use of Top and Distinct togather

June 5, 2009

If you stuck to the confusion “Can i use top and distinct togather in single query?”

Yes….!! you can do for which you have to trick, i will explain it with both code non working and working.

Non working :

select top(10) distinct can_id from tblCandidate

Working :

select distinct top(10) can_id from tblCandidate

After all its a simple game of changing the sequence of words.

Happy coding.


ConflictMode in Linq

March 31, 2009

when you require to cancel all transactions on first occurance of conflict in Linq,
there are two types of ConflictMode

1)FailOnFirstConflict
2)ContinueOnConflict

you can use if somthing like given below,

db.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);

in above code task will rollback when firstconflict will occurs.

happy coding :)


convert your enum to List

March 20, 2009

In one of scenario i want to return Enum to List with to list datasource, for that here i have drafted method which converts enum type to Generic List

132 /// <summary>

133 /// Converts a enum to a list

134 /// </summary>

135 /// <typeparam name=”T”>string name of type of Enum</typeparam>

136 /// <returns>An enumerable list of Enum Type “T”</returns>

137 /// <remarks>

138 /// Usage:

139 /// var list = StringComparison.CurrentCulture.EnumToList();

140 /// </remarks>

141 public static IEnumerable<T> EnumToList<T>(this T enumName) where T : struct

142 {

143 Type enumType = enumName.GetType();

144 // Can’t use generic type constraints on value types,

145 // so have to do check like this

146 if (enumType.BaseType != typeof (Enum))

147 throw new ArgumentException(enumType + ” is not an Enum.”, “T”, null);

148 var enumValArray = Enum.GetValues(enumType);

149 var enumValList = new List<T>(enumValArray.Length);

150 foreach (int val in enumValArray)

151 {

152 enumValList.Add((T) Enum.Parse(enumType, val.ToString()));

153 }

154 return enumValList;

155 }

156 }

here how you can access it.

128 var list = StringComparison.CurrentCulture.EnumToList();

129 foreach (var enumValue in list)

130 {

131

132 }

Happy coding.


What is FOR XML AUTO,XMLDATA and ELEMENTS

August 12, 2008

Hi,

As biztalk developer, when in begining of developing you may face problem understanding  what is  difference between FOR XML AUTO,XMLDATA and ELEMENTS.

here in this note i’ll explain you one by one.

  • FOR XML AUTO : is used to represent the actual data in xml format.
  • FOR XML AUTO , XMLDATA : is used to represent the actual data in xml format while generatiing schema.
  • FOR XML AUTO , ELEMENTS : is used to represent the actual data in xml element type which is most suggested method of generating of xml from SQL.
  • FOR XML EXPLICIT : is used to generate complicated XML message where you want result from different tables at that time you can format the result set in correct XML structure via FOR XML Explicit.

for simplify to understand the above please feel free to mail me(vkas_in_guj@hotmail.com) or post me.

Happy coding,

Vikas.


Postback in TreeView Select Node Click

July 12, 2008

if you are facing problem of POSTBACK while working with TreeView Selected Node click, here with this post i provide you one alternative which will work on all three browsers.

follow is the sample code which you can change with your respective code.

TreeNode node = new TreeNode();

node.Text = “<span onclick=’return false;’>”+YourText+”</span>”;

this will force browser to <a href=”javascript:webform_doPostback”…….

please drop your commnet how and in what situation above code helpful to you.


?? operator (C#)

April 2, 2008

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand. For example:

int? x = null;

// y = x, unless x is null, in which case y = -1.

int y = x ?? -1;

The ?? operator also works with reference types:

//message = param, unless param is null

//in which case message = “No message”

string message = param ?? “No message”;


PreRequesthandlerExecute and Global.asax

February 26, 2008

PreRequesthandlerExecute event is raised before the Init() method of Page or Service called. but it is not true that each and every need session and do not want to utilize their session on the same event when it is raising..

to solve this problem just single line code will helpful..

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs
e)

{

if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
{
// your code over here
}

}


A software “build” is a lot more than just compiling the solution – By pressing just F5

January 18, 2008

Many developers don’t use source control and don’t use any automated tools.  This is extremely inefficient and troublesome.  Those on teams are forced to use source control in an effort to share the latest code with all members of the team.  In the source control environments, there is a tacit agreement not to commit any code to the repository that will break the build.  If the build breaks, it hinders the velocity of the other developers on the team because they cannot move on while the build is broken.

What does “build” mean?
Some folks use the term “build” to mean compile, and that is incorrect.  On the teams that use no automated tools, the compile might be the only step in their build process, but the two are still different.  The “build” is a process of taking the source of a software system and making it ready for deployment.  Some teams will manually compile the source and stop before deploying to a development environment.  These teams are short-changing themselves because the only feedback they’ve obtained about the current bits is that there are no syntax or linking errors.  There is no verification that any part of the software functions as intended.  Next, they may manually perform some steps to get all the bits and configuration in order to deploy the system to a development environment.  Then after some manual testing, they’ve obtained some level of feedback.

Let’s compare the above with an Agile build.
Here are some steps that are often performed in the build process of an Agile team – these steps are always automated so they run fast and are repeatable:

update latest code _and dependencies_ from source control.  (automated process will get latest code from the SCC repository)

compile solution (standard compile and link)

copy application files to test location (output binaries moved to location to prep for automated testing)

run automated unit tests. (automated tests produced through TDD or otherwise – give immediate feedback on the state of the system)
automated environment setup to prepare for an integrated test of the system

run integration tests. (gives even more feedback that the integration points of the system are functioning correctly – might include a database)
run regression tests (if you have them – verifies that all past functionality is still working as before – this is a type of integration test)
tag source control with build number if successful (only tag successful builds – discard unsuccessful ones)
Notify development team members of success or failure
Some teams add more steps depending on their needs, and some teams don’t have integration or regression tests suites yet.  Each build process should be developed by the team and tailored to the system.  The above are some of the more common steps that Agile teams include in a build process.

The point of an automated build process is to transform the current code into a working system and get feedback on the current quality as fast as possible.  If the entire process is fast, you will run it often and obtain feedback often.  If it’s slow, you won’t do it often.  The only requirement for the developer is to start the build.  Many Agile teams even automate that step by having a program kick off a build after every commit to the SCC repository.  That process is called “Continuous Integration”. 

At the end of a build, the team should be confident that if they deployed these bits to an environment, it would work.  There still may be bugs discovered, but they are confident that old bugs haven’t resurfaced and the system works at least as good as it did on the last build.  The extra testing steps in the build process ensure that the state of the software is always moving forward.  Without these steps, developers have no way of knowing if a change broke an existing feature.

Feedback is key in a build process.  The team should decide what steps can be added to the build process to generate as much feedback as possible.  My team recently inherited a system with a build duration of 25 minutes.  This is way to slow for us, and our initial goal is to reduce that duration to 10 minutes.  We’ll be able to do this by emphasizing fast unit tests more and doing away with some of the really slow integration tests (that have delicate, cumbersome data setup scenarios).


Be Secure form your side with Encrypted Connection Strings in Configuration Files

January 11, 2008

Introduction :

Connection strings contain sensitive resource access credentials such as user names, passwords and server names. Connection strings stored in plaintext are dangerous, because an attacker that can compromise a server will be able to read those connection strings. Even if a machine is not compromised, connection strings stored in plain text are accessible to administrators and any other users with sufficient privileges on the host machine and/or Windows domain.
How to do this everything….!?!

1. Choose the appropriate configuration provider. Under most circumstances DPAPI will suffice, although the RSA protected configuration is the logical choice in web farms where multiple servers are employed.

2. Identify the configuration sections to be encrypted. Encrypting and decrypting data incurs performance overhead. To keep this overhead to a minimum, encrypt only the sections of the configuration file that store sensitive data.  Encrypt the <connectionStrings> element of the Web.config file to protect the database connection string.

3. Choose the machine or user store. The DataProtectionConfigurationProvider supports machine-level and user-level stores for key storage. The choice of store depends largely on whether or not the application shares a server with other applications and whether or not sensitive data must be kept private for each application.

Machine Store

By default, the DataProtectionConfigurationProvider is configured to use DPAPI with the machine store. Use machine-level key storage in the following situations:

The application runs on its own dedicated server with no other applications.
Multiple applications run on the same server and those applications need to be able to share sensitive information.
To encrypt the connectionStrings section with the Machine Store, run the following command from a .NET command prompt:

aspnet_regiis -pe “connectionStrings” -app “/MachineDPAPI” -prov “DataProtectionConfigurationProvider”
User Store

Use user-level key storage if the application runs in a shared hosting environment and the application’s sensitive data should not be accessible to other applications on the server. In this situation, each application should run under a separate identity, and the resources for the application—such as files and databases—should be restricted to that identity.

To encrypt the connectionStrings section with the User Store, run the following command from a .NET command prompt:

   aspnet_regiis -pe “connectionStrings” -app “/UserDPAPI” -prov “MyUserDataProtectionConfigurationProvider”

4.Encrypt the configuration file data.

For those who using IIS

aspnet_regiis -pe “connectionStrings” -app “/MachineDPAPI” -prov “DataProtectionConfigurationProvider”

For those who using physical path.

aspnet_regiis.exe -pef “connectionStrings” C:\Projects\MachineDPAPI -prov “DataProtectionConfigurationProvider”
Note : Encrypting connection strings with Aspnet_regiis does not change the code required to access the string because the decryption occurs automatically.so no need to include any further logic…!!!?


UrlReferrer. what is it…!??!

January 10, 2008

In code you can see from which url the user came in the UrlReferrer property of the Request.. This always show the url of the last roundtrip, whether smartNavigation is switched on or off. So it will be the url of the page itself on a postback. On the first rendering of the page it will contain the intended page the user came from.

Request.UrlReferrer.AbsoluteUri.Contains(“YourpageNameToCheck.aspx”) == true

{

          // your logic for validate the user.

}