Sandeep Reddy Dotnet

Learn About Microsoft Technology

Uncaught SyntaxError: Invalid regular expression flags — October 3, 2018

Uncaught SyntaxError: Invalid regular expression flags

I had a requirement in my code where I need to pass the value to a javascript function RenderFIle(filePath);

Problem

RenderFIle(filePath){

//logic to render the flle content

}

Calling   RenderFIle(Repots/Custom/ReportName)—> got an error “Uncaught SyntaxError: Invalid regular expression flags”

Solution :

try to use RegEx(‘Repots/Custom/ReportName’,’g’)—g stands for global search

generally, javascript will not allow you to pass the “/” directly, rather than, need to follow some regular expression for more information  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_regexp_flag

 

Advertisements
C#.Net Interview Questions — September 27, 2018

C#.Net Interview Questions

INTERVIEW QUESTIONS
——————-
1)what is solution(.sln) ?
A:- solution has a one project or number of projects.

2)what is namespace ?
A:- namespace is a project name.

3)What is Main method?
A:- Main() method is an entry point to the project.a project has only one Main() method that is static method.
if we use two Main() methods(That shoild be static) in one project it leads to compile time error

4)what is compile error?
A:- compile time error is sytax error, when we Compile (build) the project if code has any syntax errors it leads to compile time error.

5)what is Run time error?
A:-it will occure when application is executing. due missing logic

6)what are the output statements in console application?
A:- Console.Write()–it provides o/p and cursor remain in same line
Console.WriteLine()–it provides o/p and cursor moves to next line
Console–built in Static class
Write(),WriteLine()–static methods

7)what is datatype ?
A:- datatype defines the type of the variable

8)what are the types of Datatypes?
A:- datatypes are two types:
Value type–Value type variables assigned values directly ex:integer,float,char,bool.
Reference Type–reference type variables does not have values directly but variables has references Ex: string ,Object

9)float types ?
A:- float types are three types:
float–it gives approximate value ,it gives upto 8 digits after decimal
double–it gives approximate value ,it gives upto 16 digits after decimal
decimal–it gives accurate value,it gives fixed 28 digits after decimal
EX: float num = 10f / 3;
Console.WriteLine(num); o/p:3.3333333

decimal dnum = 10m / 3;
Console.WriteLine(dnum); o/p:3.333333333333333333333333333

double number = 10d / 3;
Console.WriteLine(number); o/p:3.33333333333333

10)How to give comments?
A:- //–single line comment
/* code */–multiline comment

11) what is casting?
A:-converting one datatype variable another datatype variable.

12)how find out casting?
A:- LHS variable and RHS variable has different datatype ic called casting

13)types of casting?
A:- implicit Casting–all posible values expressions are valid for variables ex: byte i=28;int j=i;
Explicit Casting–posible values of expressions are not valid

Note: To know the min and max value of datatype ex.Console.WriteLine((int.MinValue))
Console.WriteLine(int.MaxValue))
int k = 257;
byte l =(byte) k;// if we didnot use (byte) get error i.e needed explicit casting
byte i = 255; Console.WriteLine(k + ” ” + l); o/p: 257 1
int j = i;// managed by clr
Console.WriteLine(i+” “+j); o/p: 255 255

14)does explicit/implicit posible in boolean and string?
A:- explicit/implicit not possible boolean to other and other to boolean– leads to compile time error
explicit/implicit not possible string to other and other to string– leads to compile time error

15)mathematical expressions of byte,short char raised to int datatype implicit is not possible.

16)what is the syntax for convert to float datatype
A:- float variable=Convert.ToSingle(value);

17)what is conversion method?
A:- it converts any datatype to anyb datatype

18)What is parsing method?
A:- it convert to any datatype to string and string to any datatype

19) what is diference between conditional operators and if statement?
A:- when we use conditional operators true and false condition both required but when we use if statement flase not mandatory

20)what is Method?
A:- method is a function it exedcutes functionality.

21)what is class ?
A:-class is a collection of methods and fields

22)what is field?
A:-field is a variable of any data type declared directly in class.

23)what is object and why object?
A:-object is a instance of class ,it used to access the members of a class (depends on access modifiers).
syntax:- classname objectname=new classname();
class Program
{

public void show()
{
Console.WriteLine(“Hello”);
}

}
class demo
{
static void Main(string[] args)
{

Program obj = new Program();
obj.show();
}

}

24)will you access method without creating instance of class?
A:- yes,we can access using staic methods

class Program
{

public static void show()
{
Console.WriteLine(“Helloo”);
}

}
class demo
{
static void Main(string[] args)
{

Program.show();
}

}
–Main methods and normal methods are in same class then also we need instance of class in main method to access members tht class
class demo
{
public void show()
{
Console.WriteLine(“helloo1”);
}

static void Main(string[] args)
{

demo d = new demo();
d.show();
}
}

25)when we create method?
A:-to re-use the same code

25)types of methods?
A:- static methods And no-static methods

syntax:
accessmodifier returntype Methodname()–parameter less return type
accessmodifier returntype Methodname(datatype varName)–parameterised return type
accessmodifier void methodName()—parameterless and non return type
accessmodifier void methodName()—parameterised and non return type
above all are non staic methods if we add static keyword to that and they are all staic methods

26)what is method overloading?
A:-methods having same name but different in method signature is called method overloading
–methods not depend on the return type

27)diffrence between while and do-while ?
A:-do-while is executes once even condition is false
do-while check code then condtion and while is check condition then code.

ex: int i = 0;
while (i== 10)
{
Console.WriteLine(i);

} // 0/p: none
int i=0;
do
{
Console.WriteLine(i);
} while (i==10); // o/p: 0

how to configure log4net.config in seperate file in C#.net? — October 15, 2017

how to configure log4net.config in seperate file in C#.net?

lo4net is an open source logging library where we can trace the code execution and identify the runtime errors.

in this example, I will demonstrate how to configure the lo4net.config in a diffrent file and how we can load while our C#. Application loading.

step 1 : Install-Package log4net -Version 2.0.8

Step 2: Add new Configuration file and name it as “log4net.config” in a C#.Net project where you want to add a log4net library.

add below XML lines code in log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <root>
    <level value="ALL" />
    <!--<appender-ref ref="console" />-->
    <appender-ref ref="file" />
  </root>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="file" type="log4net.Appender.RollingFileAppender">
    <file value="myapp.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
</log4net>

 

Step 3: add below code in web.config or App.config depending on type of  .Net Application type

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net configSource="log4net.config"/>
</configuration>

Step4: Now the configuration part completed, we need to tell to that application to pick the log4net file during runtime. I prefer to add below code in Properties/AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

Step 4: Let’s write some logs in C#.net code using log4net now, but before that, we need to initialize logging object to write in a text file. use below line of code to initialize the log object.

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

 

step 5: Let’s write some stuff in C#.Console

class Program
    {
        
        private static readonly log4net.ILog log = log4net.LogManager.
                                  GetLogger(MethodBase.GetCurrentMethod().
                                             DeclaringType);
        static void Main(string[] args)
        {
           log.Debug(" ---- hello world..my logging started----"); 
        }
}

step 5: generally the output file will be in project/bin/debug folder Project/bin/release folder. as per configuration file, the logs will save in a myApp.log file.

I hope this article will help you to write logs using a log4net configuration file as an external file.

Thanks for everyone and happy coding

useful links

log4net examples

 

 

what are the Constants in C#.Net? — June 11, 2017

what are the Constants in C#.Net?

What are the Constant in C#? why we need constants? when we will use constants?

Constants are immutable (mean does not change).

In  C#.Net constant are declared with “const” modifer.

Example: public const decimal myRateOfIntrest = 10.0m;

Here “myRateOfIntrest” defined as a constant and its value never change throughout the application.

Simple example:

namespace ConstantAndReadOnly
{
    class Program
    {
        public const string myName = "sandeep";
        static void Main(string[] args)
        {
            Console.Write(myName);
			Console.ReadLine();
        }
    }
}

When you compile above program, the C# compiler generates below MSIL Code

// {
IL_0000 nop
// System.Console.WriteLine(myName);
IL_0001 ldstr  “sandeep”
IL_0006 call   System.Console.WriteLine
IL_000B nop
// }
IL_000C ret  If we notify IL 0001 instruction, the “Sandeep” literal stored in stack as a string and which will never change the value till closing the application or exiting the application.

Few interview question which were asked

For what types we can define as a constant?

Ans : For only C# built in types, we can define as constants.

For What types we cannot define as constants?

Ans : For Methods, Events and Properties.

What are the scenarios we ca use constants in projects?

If we think that certain values will never change in an entire project, we can use the constants.

For e.g.:  I have used constant to define stored procedure names which will never change while running the application.

How you access the Constant?

Constants are implicitly static, when you define a constant in C# class, we can access the constant using classname.constantName 

namespace ConstantAndReadOnly
{
    class Program
    {

        public const string myName = "sandeep";
        static void Main(string[] args)
        {
            MyProgram myprogramobject = new MyProgram();
            myprogramobject.Display();
            System.Console.Read();
        }
    }
    class MyProgram
    {
        internal void Display()
        {
            System.Console.WriteLine(Program.myName);
        }
    }

}

O/p:

Sandeep

Though myName constant defined in the Program class, we used in another class called” MyProgram” class, in Display () method.

we will discuss about the Readonly -C# in Next article.

 

How to open XAML file in Blend — March 14, 2016

How to open XAML file in Blend

“This Article dedicated to my best brother Rushi who always  encouraged me to learn UI Technologies, this is my first article on Expression Blend. 🙂 ”

Problem : How to Open XAML file in Blend from Visual studio 2013

Solution :As usual Microsoft development products will work in such a way that they reduce the Development time and give time to focus on business, in the same way  , it’s kind  time taking procedure to design the XAML UI with coding in Visual Studio (like alignment, color selection, Animation), to avoid code based UI design development  process , Microsoft released new tool called ‘Microsoft Belnd‘ ,this tool will help to develop RICH UI and fantastic animation features

Download Blend : Blend Down load from Here

Steps to open existing WPF Application in Visual studio

Step 0: Make sure that we installed Blend in the computer and proceed further, if its not installed , download and install.

Step 1 : Open visual Studio with Run as Administrator

Step 2 : Open the WPF project which you want open XAMl files in Blend

Step 3: Browse for Tool->Customize

Step 4 : Click On Customize option->click on Command Tab-> Choose “Project and Solution Context Menus | Item”

Customize Screen_VS
Image 1. Visual Studio Customize Screen

Step 5: If the Open In Blend option is visible meaning that the option is already enabled,if not , then Click on Add Command Button as shown Image 1 ,Select View category -> Select open In blend option  and Close all dialog (refer below screen shot)

OPen_In_Blend_option
Img2 : Select View->Open in Blend

Step 6: if we observe , we can able to see 3 Open in Blend option, select any one of them and click  OK .

Step 7 : Once we enabled , we can open XAML files in Blend , Select XAML File->Right click and see Open In belnd option enabled.as Shown in below figure

Open_In_Belnd_rightclick

Step 7 : Once the XAML files is opened in Blend, we can leverage all features of Blend .

Conclusion : Using Microsoft blend we can build rich XAML based UI Applications and we can open existing WPF Application XAML files in Blend from Visual studio.

 

Finding a CLR Version using C#.Net — March 9, 2016

Finding a CLR Version using C#.Net

one of my friend asked about that how to find a CLR version which installed local Computer , Even i too don’t have an idea about that, but as we all know that Google  helped us many ways, now also i got help from Google and i plotted below points.

What is CLR :-CLR is a common language run time engine which will convert intermediate language to Machine understandable language and its main important part of .Net frame work component.

there are many ways you can find out CLR version , here we will discuss about using C# we will discuss.