Thursday, August 22, 2013

Create batch file to run exe file


I want to run my .Net application in the client system. But I don’t know client have .NetFramework or not.  Without .NetFramework  my application will not start. To solve this problem we can use two different types. One is while creating the setup file we need to add the prerequisites,  Another one is creates a batch file and run the necessary installations.

The below Batch file will help you to run the entire exe file in your folder.  In the below example NewFolder have all my pre installation setups. Like My application (MyApp.msi) and SQLExpress2005.exe. So I want to run my entire prerequisite one by one. For that the below Batch file will help me to achieve this.


Install.bat file:
---------------------------------------------------------------------------------
ECHO Installation Started
START  /WAIT  c:\windows\system32\notepad.exe  
CD NewFolder
START  /WAIT  "\NewFolder\"  MyApp.msi
START  /WAIT  "\NewFolder\"  SQLExpress2005.exe
cd..
START  /WAIT  setup.exe

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


Explain:

Run from the direct exe path:
START  /WAIT  c:\windows\system32\notepad.exe  

Run in the root folder: 
START  /WAIT  MyApp.msi

Run inside the folder:
CD   NewFolder
START  /WAIT  "\NewFolder\"  MyApp.msi

Go back to root folder:
CD..


Tuesday, August 20, 2013

Windows application interview questions

Winforms interview questions :
The below are the interview questions for 5 and 6+ experience person's in visual studio.Net..
1. About constrains.
2. WSDL
3. About Assemply
4. How to use Delay in C#
5. What is Aggration, Composition, Association in C#. Net
6. About Application pool
7. Difference between Dock and anchor
8. How to release unmanaged code in C#. Net
9. What is Com Component
10. Difference between Data Reader and Data Adapter
11. How to export data to Excel in C#
12. How to configure Proxy in your application
13. What will happen the below SQL trigger.
Trigger--> Inside one more Trigger --> And one more trigger
If trigger 3 have some expection means what will happen.
14. Tell me Background workers Three events ?
15. Write simple print string function using Oops concept in C#. Net .
16. What is Statict class?
17. With out constructer can i call the class ?.
18. Explain the type of Webservices.
19. Explain SDLC


In MS SQL Interview Questions

TableA
ID Name
1 vijay
2 antonu
3 yuvan
4 sekar

TableB
Id Address
1 Saidapet
2 Matha Nagar
2 Madipakkam
3 DPM


What was the output of Below Query?
select * from TableA,TableB

Result: 16 Rows 

What was the output of below query?
select A.ID,A.Name,B.Address from TableA as A
LEFT OUTER JOIN TableB as B on A.ID=B.Id

Result: 5 Rows 


What was the Result of below SQL query?
select ID Name from TableA

Result: Column Name ID changed to Name



ASP.NET Interview question:

What is JQuery?
What is Lambda expression?
How to do Unit Testing?
Are You using Any Design patterns?
Explain your Project architecture

Monday, August 19, 2013

How to use filter in datatable using C#.Net?

Some time we need to use filter in our normal DataSet or DataTable to get some values.For that the below example will explain how to use filter in DataTable using C#.Net.

 private void dtFilter()
{
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("ID"));
    dt.Columns.Add(new DataColumn("Name"));

    dt.Rows.Add("2", "Vijay");
    dt.Rows.Add("1", "Kumar");
    dt.Rows.Add("3", "Yuvan");
    dt.Rows.Add("3", "Vijay");


Filter the records from a datatable in C#:

    DataRow[] dtRow = dt.Select("Name='Vijay'");
    System.Text.StringBuilder str = new System.Text.StringBuilder();

    foreach (DataRow dr in dtRow)
    {
        str.Append(dr["Name"].ToString()+",");
    }
    Response.Write("Filter Output :: " + str.ToString());


Delete records from a datatable using C#.Net:

    foreach (DataRow dr in dtRow)
    {
        dtRow.Rows.Remove(dr);
    }


}

Call javascript function in code behind using c#.Net


Some time we need to call javascript function in .CS(Code behind) page. The below example will help you to understand the calling javascript function in code behind.  In .CS file you need to register the startup script using script manager. Below example I have register in two ways.


In ASPX.CS File

ScriptManager.RegisterStartupScript(Page, this.GetType(), "callFunctionStartupScripts", "loadMyFunction();", true);

(OR)

string command = "loadMyFunction();";
string script = "";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showsmyfunction", script, false);


In the design page (.ASPX) you have to create Javascript function loadMyFunction. While running the program the loadMyFunction will fire.

In ASPX file
<script type="text/javascript">
function loadMyFunction(container) {
try   {
      alert('Test');
} catch (ex) {  }

</script>

Friday, August 16, 2013

Dynamically change iframe src query string using javascript

Some timewe need to assign iframe src query string dynamically. For that we can use document.writeln javascript method. Using window.location.search will give you the query string value. So you need to assign that value to iframe in page load.

HTML Code:


<html>
<script type="text/javascript">
document.writeln('');
</script>
</html>

Wednesday, August 14, 2013

Application Exception Handler in WPF application

In WPF application I want to get all the exception in one place for that the below code will help you to capture all the exception in one place. It’s a global exception handling page. In the App.xaml file you need to specify the Startup and DispatcherUnhandledException method you need to give in XAML file. Global application exception handler or maintain application Error log to use below code.

This is the App.XAML file:



<Application x:Class="LiveSourceSample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             DispatcherUnhandledException="Application_DispatcherUnhandledException"
    Startup="Application_Startup"
             StartupUri="PreviewUI.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>




In the App.XAML.CS file you need to add the Application start event and other exception event method need to create to capture the exceptions. The below code will help you to get application startup exception and other all the exception will be handled.



using System.IO;
using System.Windows.Forms;


public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // UI Exceptions
            this.DispatcherUnhandledException += Application_DispatcherUnhandledException;

            // Thread exceptions
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            var exception = e.Exception;
            HandleUnhandledException(exception);
        }

        private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
        {
            HandleUnhandledException(unhandledExceptionEventArgs.ExceptionObject as Exception);
            if (unhandledExceptionEventArgs.IsTerminating)
            {
                Comman.ErrorLog("Application is terminating due to an unhandled exception in a secondary thread.");
            }
        }

        private void HandleUnhandledException(Exception exception)
        {
            string message = "Unhandled exception";
            try
            {
                AssemblyName assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName();
                message = string.Format("Unhandled exception in {0} v{1}", assemblyName.Name, assemblyName.Version);
            }
            catch (Exception exc)
            {
                Comman.ErrorLog("Exception in unhandled exception handler - "+ exc.Message);
            }
            finally
            {
                Comman.ErrorLog(message+ exception.Message);
            }
        }
    }


Error Log Function:

 public class Comman
    {
 public static void ErrorLog(string sMessage)
       {
           StreamWriter objSw = null;
           try
           {
               string sFolderName = Application.StartupPath + @"\Logs\";
               if (!Directory.Exists(sFolderName))
                   Directory.CreateDirectory(sFolderName);
               string sFilePath = sFolderName + "Error.log";

               objSw = new StreamWriter(sFilePath, true);
               objSw.WriteLine(DateTime.Now.ToString() + " " + sMessage + Environment.NewLine);

           }
           catch (Exception ex)
           {
               Comman.ErrorLog("Error -" + ex.Message);
           }
           finally
           {
               if (objSw != null)
               {
                   objSw.Flush();
                   objSw.Dispose();
               }
           }
       }
}


Friday, August 9, 2013

How to delete duplicate rows from table in sql server?


One of my interviews asked this question. I have two columns Name and Age in my Table(MyTable). From the MyTable I want to delete only duplicate records. So the below Query will help you to delete or remove the duplicate rows from the MyTable.

Below is MyTable output:


SELECT * FROM MyTable

Current output:

Name    Age
vijay    30
antony    40
Aruna    28
antony    40
chander35
mark    42
vijay    30


Remove duplicate rows from the table:
The below query will help you to delete duplicate row from your table. Here we used Row_Number() function and PARTITION BY key words to remove the duplicate rows.



DELETE SUB FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY Name, Age ORDER BY Name) cnt
 FROM MyTable) SUB
WHERE SUB.Cnt > 1



After the query execution the output is:


Name    Age
Aruna    28
antony    40
chander    35
mark    42
vijay    30


How to use Row_Number in SQL Select query:



SELECT ROW_NUMBER() OVER(ORDER BY Age) FROM mytable