Tuesday, October 29, 2013

The process cannot access the file because it is being used by another process


Problem-


The process cannot access the file because it is being used by another process

This is common problem, when we create file using File.Create() method.




Theprocesscannotaccess


Reason-


When we use File.Create() method to create file then the object of FileStream class is created and no other process can use or access the created file until the file is closed.

Solution -

This problem can be easily resolved by modifying your code like this-

File.Create (@"c:\Text.txt").Close();

Means use Close () with File.Create () method. It will create the file and after that it will close the related file.



Tuesday, October 1, 2013

How to make DatePicker control as null or empty in Windows Application?


This is common problem with DatePicker control - We cannot assign null or empty value to its value property. In this case, showing DatePicker control as empty is bit difficult. I have done one work around for this and now I am able to show empty DatePicker control.

In this article, I will explain - How to make DatePicker control as null or empty?

Check the below code implementation.

Workaround-

To make it possible, I have used "CustomFormat" property of DatePicker control.

//To set DatePicker Control format as "MM/yyyy"
private void dtPickerDOB_ValueChanged(object sender, EventArgs e)
{
   dtPickerDOB.Format = DateTimePickerFormat.Custom;
   dtPickerDOB.CustomFormat = "MM/yyyy";
}


In above code, I am setting CustomFormat = "MM/yyyy". So the date will be displayed like this- "10/2013"

Now to make this DatePicker cotrol as empty-

//To make DatePicker Control as empty
private void btnClearDate_Click(object sender, EventArgs e)
{
  // Setting CustomFormat as Space.           
  dtPickerDOB.Format = DateTimePickerFormat.Custom;
  dtPickerDOB.CustomFormat = " ";




In above code, I have set CustomFormat = " " so empty value will be displayed in DatePicker control.

Try this code


Thanks

Monday, July 22, 2013

How to get index of node based on node value using XSLT?


While working with XML in some of the cases, we want to get the index or position of particular value. This can be easily done by using XSLT.

In this article, I am explaining - How to get index of node based on node value using XSLT?

Example-

Suppose, I have this XML data-

<ResponseData>
    <value>C</value>
    <value>A</value>
    <value>B</value>
</Response>

Now if I want to get the position of ‘A’ then It should return '2' .

How to get it-

This is small XSLT code to get this position-

<!--Declaring Variable with value-->
<xsl:variable name="TempData" select="A"/>
 

<!--XSLT code to get position of 'A', output will be 2 -->
<xsl:value-of select="count(//abc:ResponseData/abc:value[text()=$TempData]/preceding-sibling::*)+1" />


In above line I have declared one variable - 'TempData'
and using select I have assigned value -'A'


Now to get the position of 'A', I have used count() method.

Here this expression -

count(//abc:ResponseData/abc:value[text()=$TempData]/preceding-sibling::*)+1

Will return the count of preceding sibling of A, which will be position of 'A'.

Like 'A', we can also get the position of other Values ('B' and 'C'), for this we need to assign this value to 'TempData' variable.



Thanks


Wednesday, April 24, 2013

How to copy data from Virtual machine to our local machine?


This is common task to work in virtual machine, while working in virtual machine sometime we have a requirement that we need to copy data from virtual machine to our local machine.

There is no direct method to copy the data from virtual machine and paste it to our local machine.

But this can be easily done by simple trick.

In this article I am explaining - How to copy data from Virtual machine to our local machine?


Follow these steps-

1. First create one folder in your local machine.

2. Make that folder as shared; means share this folder for everyone. You can just use sharing tab in properties window of that folder.

3. After share that folder now accesses that folder from Virtual machine.

4. Once you will access that shared folder you will be able to copy data from virtual machine to that shared folder.


Now you are done.

Now you can see data in your local machine.


Thanks

Thursday, March 21, 2013

How to use JQuery Accordion in ASP.Net?



Asp.net Ajax Control Toolkit contains Accordion control, but it is server side control which is heavy weight. We have another alternative of Ajax Accordion Control. We can use JQuery Accordion. Jquery Accordion control is client side control so it is faster that Ajax Accordion control.

In this article I am explaining - How to use JQuery Accordion in ASP.Net?

To use JQuery Accordion first we need to include Jquery plug-in. You can easily download those .CSS and .JS files from jqueryui.com. After downloading those files create your directory structure like this-

http://steptodotnet.blogspot.in/2011/12/jquery-popup-window-in-aspnet.html

In this link you can see the solution structure.

Now here is the code to use Jquery Accordion control-

<head runat="server">
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui-1.8.20.custom.min.js" type="text/javascript"></script>
    <link href="Css/jquery-ui-1.8.20.custom.css" rel="stylesheet" type="text/css" />
    <title></title>
    <script>
        $(function () {
            $("#accordion").accordion();
        });
    </script>

</head>


<body>
    <form id="form1" runat="server">
    <div>
        <div id="accordion" style="width: 400px">
            <h3>
                <a href="#">Section 1</a></h3>
            <div>
                Section 1 Description
            </div>
            <h3>
                <a href="#">Section 2</a></h3>
            <div>
                Section 2 Description
            </div>
            <h3>
                <a href="#">Section 3</a></h3>
            <div>
                Section 3 Description
            </div>
        </div>
    </div>
    </form>
</body>


In above code I have included Source files(.js files) and CSS files. Now to make Accordion to work I have written this lines of code-

<script>
        $(function () {
            $("#accordion").accordion();
        });
   </script>




Here "accordion" is the name of Div control which contains Child divs, These Child divs will be displayed as Accordion sections.

Output will be like this-



Accordion


Note-

This example needs JQuery plug-in so after downloading include these files .js and .cs files in your project .



Thanks


Using #Region in Dot Net


The #region directive allows organizing code files. Using #region directive we can separate the code. We can create separate region for each code block. Means to declare the variables, constants, functions etc we can use different code block by separating using #region directive.

Here is the example-

// region 1
#region Public Variables
Public int value1
Public int value2
#endregion

//region 2
#region Methods
public string  GetData()
{
--------
--------
}
#endregion


in above example we can see that I have created 2 region.

region1 for Public variables
region2 for methods


Like this we can create other regions also.


There are some benefits of using #region directive.

1. Separation of code
2. Readability
3. Easy to maintain
4. Easy to expand or collapse the code block
5. Easy to move entire code block


It is good to use #region directive while doing coding.



Thanks



Thursday, March 7, 2013

RichTextBox losing formatting : Workaround


Problem-

This is the common problem with RichTextBox, when we set any setting like - bold, Italic or Underline, then RichTextBox loses previous setting/formatting. Means it doesn't preserve previous settings.

Example-

We have Text -   Jitendra Faye

after making bold it should be - Jitendra Faye

But output will be - Jitendra Faye     

means here RichTextBox not maintaining previous setting UnderLine.

In this article I am explaining - How to maintain RichTextBox setting while applying new settings?

I also faced same problem and came with one work around.

Solution-

This problem can be solved by setting new formatting to each character instead of going for whole string.

Here is the code-

To make Text as Bold

 private void btnBold_Click(object sender, EventArgs e)
        {
            int selStart = rtfBox.SelectionStart;
            int selLength = rtfBox.SelectionLength;
            int selEnd = selStart + selLength;

            if (rtfBox.SelectionFont.Bold)
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style & ~FontStyle.Bold);

                }
            }
            else
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style | FontStyle.Bold);
                }
            }

        }


To make Text as Italic

  private void btnItalic_Click(object sender, EventArgs e)
        {
            int selStart = rtfBox.SelectionStart;
            int selLength = rtfBox.SelectionLength;
            int selEnd = selStart + selLength;

            if (rtfBox.SelectionFont.Italic)
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style & ~FontStyle.Italic);
                }
            }
            else
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style | FontStyle.Italic);
                }
            }
        }




To make Text as UnderLine

  private void btnUnderLine_Click(object sender, EventArgs e)
        {
            int selStart = rtfBox.SelectionStart;
            int selLength = rtfBox.SelectionLength;
            int selEnd = selStart + selLength;

            if (rtfBox.SelectionFont.Underline)
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style & ~FontStyle.Underline);

                }
            }
            else
            {
                for (int x = selStart; x <= selEnd - 1; x++)
                {
                    rtfBox.Select(x, 1);
                    rtfBox.SelectionFont = new Font(rtfBox.Font, rtfBox.SelectionFont.Style | FontStyle.Underline);
                }
            }
        }


In above code you can toggle for Bold, Italic and UnderLine by maintaining previous setting also.


Try this code.


Thanks

Wednesday, February 27, 2013

How to download file using C#?


In some of the cases we need to download file from the Internet, In this article I am explaining-

How to download file using C#?

For this I have used WebResponse, WebRequest Classes so we need to include System.Net Namespace.To download the file from Internet we have to make request to particular object so that I have used webRequest class object. After getting the response from the server we need to handle that response so I have used webResponse class object.

For displaying downloading process I have used ProgressBar control.

Here is the code-

 private void btnDownLoadFile_Click(object sender, EventArgs e)
        {
           
            WebRequest requestObj ;
            WebResponse responseObj;
            Stream readerData ;
            Stream writerData ;
            byte[] data = new byte[1024];
            int count = 0;
            int total = 0;
            this.Text = "Downloading file .....";
           
            string DestinationFileName= "TargetLocation";
            string SourceLocation= "SourceLocation";

            Application.DoEvents();

            try
            {
                requestObj = WebRequest.Create(
SourceLocation);
                responseObj = requestObj.GetResponse();
                readerData = responseObj.GetResponseStream();
                progressBar1.Maximum = Convert.ToInt32(responseObj .ContentLength);
                progressBar1.Value = 0;
                total = 0;
                writerData = File.Create(DestinationFileName);
                while (true)
                {
                    count = readerData .Read(data, 0, 1024);
                    if (count <= 0)
                    {
                        break;
                    }
                    writerData.Write(data, 0, count);
                    total += 1024;
                    if (total < progressBar1.Maximum)
                        progressBar1.Value = total;
                    Application.DoEvents();
                }
                readerData .Close();
                writerData.Close();
                MessageBox.Show("Download completed...");
            }
            catch (Exception ex)
            {  
                MessageBox.Show("Error: " + ex.Message);
            }
        }



In above code, I am making request to download file using WebRequest Class object, for that I am passing source file url to download file.

 requestObj = WebRequest.Create(TargetLocation);

After making request we will get response, using this line of code-

  responseObj = requestObj.GetResponse();

Now using using Stream class object I am reading and writing data to the target file.

 count = readerData .Read(data, 0, 1024);

 writerData.Write(data, 0, count);


Meanwhile for displaying the download process, I am showing progressbar. Based on data read and write progressbar value will be increased which will show downloading process.


Output-


Downloading file



Thanks


Sunday, February 17, 2013

How to update record without primary key?


If we have any primary key in table then it is very easy to update the records  in the database table, but generally we face problem when we don't have primary in table because on that time we can not use any particular column to update records because that column can contain duplicate values.

But there is work around for this problem, we can use” ROW_NUMBER()” function to generate identity value for each record and then based on that “ROW_NUMBER” we can delete record from the table.

In this article, I am explaining - How to update nth record of the table?
Or
How to update record without primary key?



For this I am using “ROW_NUMBER()” function in query, Here is syntax -

Syntax-

WITH Record AS (SELECT *, ROW_NUMBER() OVER (ORDER BY [ColumnName]) AS rownum FROM TableName where condition) update Record  set  col1=value ,col2=value where Record.rownum = value

Example-

WITH Record AS (SELECT *, ROW_NUMBER() OVER (ORDER BY EmpName) AS rownum FROM tab_Employee ) update Record  set  Age=25 ,Salary=45000 where Record.rownum = 1


In this example,   “ROW_NUMBER()” is a function which will generate sequence number for each record, here ordering will be based on EmpName column, you can change it.

Here "rownum" will be additional column in the result set, this will be used to update the record.

Here “Record “ is result set.


Try this example.


Thanks
 

Thursday, January 3, 2013

How to make Single Click and Double Click Events work together in windows form?


While working with Windows Application, This is common problem  that- Single Click and Double Click Events don't work together (If you have any processing code in Single Click Event like- Showing message using MessageBox), means if we have written some code in Single Click Event and some lines of code in Double Click Event then always Single Click Event code will be executed.

I also faced same problem and came with one workaround.

See this example-

  private void Form1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Single Click Event");
        }

  private void Form1_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("Double Click Event");
        }


In above lines of code always Single Click Event code will be executed and always output will be - Single Click only.

In this article, I am explaining - How to make Single Click and Double Click  Events work together?

Workaround-

To resolve this problem, I have taken one Timer Control, using Timer control I am handling the Single and Double Click Events functionalities.

Here is the code-

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = SystemInformation.DoubleClickTime;
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            MessageBox.Show("Double Click Event");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            MessageBox.Show("Single Click Event"); 
        }



In above code, first I am setting Timer Interval as System Double Click Time value. This can be different based on Operating System Settings.

  private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = SystemInformation.DoubleClickTime;
        }


Now in Single Click Event, I am just enabling the Timer to execute the code which is written in Tick() Event of Timer.

  private void Form1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;

        }


 In Tick() event write the code which you want to execute in Single Click Event.

    private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            MessageBox.Show("Single Click Event"); 
        }


Here I am disabling the Timer Control also because this code will be executed once.

Now this is Double Click Event code-

 private void Form1_DoubleClick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            MessageBox.Show("Double Click Event");
        }


Here I am just disabling the Timer Control; here you need to put actual Double Click Event code.

Here all the functionalities depend on Timer Control, Here making Timer control enable and disable is making trick for us.


Try this code.


Thanks