Monday, October 5, 2015

Regular expressions to resolve different requirements

Issue 1>
I was having requirement that I want to find all words in two consecutive % characters. Below interesting regular expression I used for that.

%[\w\s\S]*?%

\w for alphabet and underscore
\s for space
\S for special chars
? returns shortest possible match

Edit the% my file % %% %kjghjhgkjg % % (**&*&*&*&*&*&*& % %9089898098% Expression

Highlighted are the matches.

http://regexr.com/3btui see for more details.

The c#.net code used as below to find all these matches.

            MatchCollection allMatchResults = null;
            var regexObj = new Regex(@"%[\w\s\S]*?%");  
            allMatchResults = regexObj.Matches(this.textBox1.Text);

            string output = "";
            foreach (var s in allMatchResults)
            {
                output += s + "\n";
            }

            label1.Text = output;

Friday, August 21, 2015

Getting "Must declare the scalar variable" error while writing SQL statements in script

I was writing bunch of operations in sql, was using SQL Server 2008 R2. I declared one INT variable and was trying to use inside select statement, I was getting error as "Must declare the scalar variable "@numberOfCards"."

SQL scripts looks like something similar to this: -


declare @numberOfCards int;
set @numberOfCards = 3; --some number
;
;
;
GO
select * from CardsRepository where CardsCount = @numberOfCards

Reason of issue:
Culprit was GO statement. The GO command signals the end of a batch of Transact-SQL statements. A local variable is only valid within the body of a batch or procedure.

Solution:

The first option is to remove the GO command between the 2 sets of scripts so that the local variable @numberOfCards is valid and accessible on both scripts. Use semi comma where ever the sql statement ends. 

Wednesday, April 1, 2015

Good serialization deserialization example in c# 4.0 (.Net 4.0)

Code of deserialization


           CarInfoCollection carInfoCollection = null;

            using (StringReader strReader = new StringReader(this.textBox.Text.Trim()))
            {
                using (XmlTextReader reader = new XmlTextReader(strReader))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(CarInfoCollection));
                    carInfoCollection = (CarInfoCollection)serializer.Deserialize(reader);
                    reader.Close();
                }
            }


            MessageBox.Show(carInfoCollection.CarInfoFields.Count().ToString());

----------------------------------------------------------------------------------------

Type classes

  [System.SerializableAttribute()]
    [XmlRootAttribute("CarInfoFields", Namespace = "urn:carinfo-schema:CarInfoCollection")]
    public class CarInfoCollection
    {
        [XmlElement("CarInfo")]
        public CarInfo[] CarInfoFields { get; set; }
    }

    [System.SerializableAttribute()]
    public class CarInfo
    {
        [XmlAttribute]
        public String Name { get; set; }

        [XmlAttribute]
        public CarUsage Usage { get; set; }

        [XmlArray(ElementName = "Features", IsNullable = true)]
        [XmlArrayItem(ElementName = "Feature", IsNullable = true)]
        public List<string> Features { get; set; }
    }

----------------------------------------------------------------------------------------

Xml string entered in textbox, examples




Serialization Example

When we do not want xml declaration / omit xml declaration

OmitXmlDeclaration should be set to True while initializing XmlWriter. Sample :-

  public string SerializeToXML(T typeToSerialize)
        {
            using (var sw = new StringWriter())
            {
                using (var xw = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
                {
                    var serializer = new XmlSerializer(typeof(T));
                    
                    // below code will omit the namespace in serialized xml
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    serializer.Serialize(xw, typeToSerialize, ns, encodingStyle: string.Empty);
                }
                return sw.ToString();
            }
        }