Search This Blog

Wednesday, July 20, 2016

SQL SERVER – Clear Dropdown List of Recent Connection from SQL Server Management Studio








For SQL 2005, delete the file:

C:\DocumentsSettings\<USER>\ApplicationData\Microsoft\MicrosoftSQL Server\90\Tools\Shell\mru.dat

For SQL Server 2008, delete the file::

C:\Documents and Settings\<user>\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\mru.dat
C:\Documents and Settings\[user]\Application Data\Microsoft\Microsoft SQL Server\100\Tools\ShellSEM\mru.dat




Resolving view state message authentication code (MAC) errors

Server Error in '/' Application.

Validation of viewstate MAC failed. If this application is hosted by a web farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
Source Error: [No relevant source lines]
Source File: ... Line: 0
Stack Trace:

[ViewStateException: Invalid viewstate.
Client IP: ::1
Port: 40653
Referer: http://localhost:40643/MyPage.aspx
Path: /MyPage.aspx
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
ViewState: ...]

[HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

See http://go.microsoft.com/fwlink/?LinkID=314055 for more information.]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +190
System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) +46
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString, Purpose purpose) +861
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter2.Deserialize(String serializedState, Purpose purpose) +51
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter2 formatter, String serializedState, Purpose purpose) +67
System.Web.UI.HiddenFieldPageStatePersister.Load() +444
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +368
System.Web.UI.Page.LoadAllState() +109
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7959
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +429
System.Web.UI.Page.ProcessRequest() +125
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +48
System.Web.UI.Page.ProcessRequest(HttpContext context) +234
ASP.mypage_aspx.ProcessRequest(HttpContext context) in ...:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1300
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +140

Resolve this issue you can add a <machineKey> element, in the Web.config file. The <machineKey> element is only valid in the Web.config file at the root of your application and is not valid at the subfolder level.

<configuration>
  <system.web>
    <machineKey ... />  (add below machineKey in system.web section)
  </system.web>
</configuration>

<machineKey
validationKey="20A1D0BF6A9C13FC54FAFAE5534D586F78A50005223F08C0C8AD0704CCF27C29E6F41C30FA03D9E418F7EBB38BFDAC698545C39200C39A622BE22EBC6C10BEE8"
decryptionKey="CD0F6DC238FD884B009B3C5E3163726F58C4D51FE82908E9F1504BF50D95075D"
validation="SHA1" decryption="AES"/>


Friday, May 27, 2016

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

Error in 2012 VS when using validation control :

Server Error in '/it' Application.

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: WebForms UnobtrusiveValidationMode requires 
a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping 
named jquery(case-sensitive).]
   System.Web.UI.ClientScriptManager.EnsureJqueryRegistered() +2170706
   System.Web.UI.WebControls.BaseValidator.RegisterUnobtrusiveScript() +10
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +9576593
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean 
includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) +974

Solution:
Add in web.config :

 <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
 </appSettings>


Tuesday, May 17, 2016

Differences between Hash table and Dictionary.



Differences between Hash table and Dictionary
Sno.
Dictionary
Hash table
1
It returns error if we try to find a key which does not exist.
It returns null if we try to find a key which does not exist.
2
It is faster than a Hashtable because there is no boxing and unboxing.
It is slower than dictionary because it requires boxing and unboxing.
3
Only public static members are thread safe.
All the members in a Hashtable are thread safe.
4
Dictionary is a generic type which means we can use it with any data type.
Hashtable is not a generic type.

What is Hashtable ?

Hashtable optimizes lookups. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type.

A hash table is made up of a mapping function and an array. The array contains your data, while the mapping function is used to assign numerical values (keys) to the data. This helps in categorizing the data, which speeds up search times when you search for it.

using System.Collections;
using System;
class Example
{
    static void Main()
    {
                Hashtable hashtable = new Hashtable();
                hashtable [1] = "One";
                hashtable [2] = "Two";
                hashtable [3] = "Three";
                foreach (DictionaryEntry entry in hashtable)
                {
                    Console.WriteLine("{0} : {1}", entry.Key, entry.Value);
                }
    }
}

Output:
3: Three
2: Two
1: One

HashTable Method()

using System.Collections;
using System;
class Example
{
static Hashtable GetHashtable()
    {
                // Creating a simple hashtable called hashtable.
                Hashtable hashtable = new Hashtable();
                hashtable.Add("chandra", 10);
                hashtable.Add("prakash", 11);
                hashtable.Add("vipin", 12);
                return hashtable;
   }
static void Main ()
{
Hashtable hashtable = GetHashtable ();
Console.WriteLine(hashtable.ContainsKey (“chandra”));
}

Output:

True

Saturday, May 14, 2016

Example of Dictionary Class

A Dictionary class is a data structure that represents a collection of keys and values pair of data. The key is identical in a key-value pair and it can have at most one value in the dictionary, but a value can be associated with many different keys.
This class is defined in the System.Collections.Generic namespace, so you should import or using System.Collections.Generic namespace.

Dictionary<TKey,TValue>

TKey - The type of the keys in the dictionary.
TValue - The type of the values in the dictionary.

Input Occupancy String format:
Room_Adult_Children_Age  : 1_2_1_10  [room always in string 1]
1_2_1_10 ; 1_2_1_8   or 1_2_1_10  ; 1_2_1_8  ; 1_2_1_5 
Output: 2_1_2_10_11
Input: 1_1_0_-1; 1_1_0_-1
Output: 2_1_0_-1
Input: 1_2_2_6_8  ; 1_2_2_4_6  ; 1_2_2_6_9 
If you want to search for 2 single room with 2 adult and 0 child you pass the occupancy like that:
Input: 1_2_0_-1 ; 1_2_0_-1



If 2 or 3 room then Input Occupancy string format:

Scenario 1: Search for 1 Adult
If you want to search for 1 single room with 1 adult and 0 child you pass the occupancy like that: Input: 1_1_0_-1
Where -1 stands for an age of an adult which is not required so we pass it -1.

Scenario 2: Search for 1 Adult and 2 Children
If you want to search for 1 single room with 1 adult and 2 child you pass the occupancy like that:
Input: 1_1_1_10;1_1_1_11

 where 10 stand for the first age of the first child and 11 stands for the second age of the child.

Scenario 3: Search for 2 rooms one for 1 Adult and 0 Children and the Second for 2 Adults
If you want to search for 2 rooms with 1 adult and 0 children and another room for 2 adults and 0 children you pass them like that:
Input: 1_1_0_-1;1_2_0_-1

Output: 1_1_0_-1;1_2_0_-1

Scenario 4: Search for 2 rooms for the same 1 Adult and 0 Children.
It will be like that

Scenario 5: Search for 3 rooms for the same 2 Adult and 2 Children.
It will be like that
Output: 3_2_2_6_8_4_6_6_9
Scenario 6: Search for 2 Adult 0 Children
Output: 2_2_0_-1


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string GroupOccupancy(string occupancy)
        {
            string abc = "";

            string[] arrOccupancy = occupancy.Split(';');
            List<string> objage = new List<string>();

            /*The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings.*/

            Dictionary<object, int> DifferentCarcategory = new Dictionary<object, int>();            

            foreach (Object obj in arrOccupancy)
            {
                /*ContainsKey method. It returns true if the key was found.*/
                string sssdd = Convert.ToString(obj).Substring(0, 5);
                //sssdd = Regex.Replace( "@"+sssdd , "@\\s*[\\d]+?_", "");
                if (!DifferentCarcategory.ContainsKey(sssdd))
                {
                    int totallength = Convert.ToString(obj).Length;
                    String age = Convert.ToString(obj).Substring(5, totallength - 5);
                    objage.Add(age);

                    DifferentCarcategory.Add(sssdd, 1);
                }

                else
                {
                    DifferentCarcategory[sssdd]++;

                    int totallength = Convert.ToString(obj).Length;
                    String age = Convert.ToString(obj).Substring(5, totallength - 5);
                    objage.Add(age);

                }
            }

            /* use foreach syntax and KeyValuePair generics in the foreach loop. With collections like Dictionary, 
             * we must always know the value types. With each KeyValuePair, there is a Key member and Value member.*/

            foreach (string kvp in DifferentCarcategory.Keys)
            {
                string strkv = kvp;
                if (Convert.ToInt32(DifferentCarcategory[kvp]) == 1)
                {
                    abc = abc + ";" + kvp;
                }
                else
                {
                    /*Regex.Replace static method with a string replacement.The "\d" metacharacter matches digit characters.  */
                    abc = abc + ";" + Regex.Replace(kvp, "^[\\d]*?_", Convert.ToString(DifferentCarcategory[kvp]) + "_");
                }
            }
            int spiltindex = 0;
            abc = abc.Trim().Trim(';');
            String sssnew = "";
            string xxxx = "";
            foreach (string kvp in objage)
            {
                if (!abc.Contains(";"))
                {
                   
                   abc = abc + kvp;
                }
                else
                {

                    sssnew = sssnew + abc.Split(';')[spiltindex] + kvp + ";";
                    
                }
                spiltindex++;
            }

           //this condition for no child case in this case only pass (-1) one time in string.
            xxxx = Convert.ToString(abc).Substring(3, 2);
            if (xxxx == "_0")
               abc = Convert.ToString(abc).Substring(0, 8);
            else
            
            abc = String.IsNullOrWhiteSpace(sssnew) ? abc : sssnew;
            abc = abc.Trim().Trim(';');
            return abc.ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = GroupOccupancy(textBox1.Text);
        }
    }
}