Wednesday 18 December 2013

Cannot delete file its being used by another process



delete file error the process cannot access the file because it is being used by another process.the process cannot access the file because it is being used c#.

Solution

Type following code before your code
GC.Collect();
GC.WaitForPendingFinalizers();

Wednesday 9 October 2013

Time validation C#



using System.Text.RegularExpressions;

string strcd = DateTime.Now.ToString("hh:mm:ss tt"); //02:56:59 PM 
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {



string strcd = DateTime.Now.ToString("hh:mm tt"); //01:06 PM        
bool isOK = Regex.IsMatch(strcd, @"[0-2][0-9]\:[0-5][0-9]\ [aApP][mM]");
if (isOK)
  {
     // Do something.
  }
else
  {
     // Do something else.
  }

Disable Browser Back Button



Javascript


<script type = "text/javascript" >
function disableBackButton()
{window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>

For internet explorer: <body onload="disableBackButton()">
If you are using firefox then use <body onunload="disableBackButton()">


C#  Asp.net

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);

Remove duplicate rows from a datatable in ASP.NET/C#



In this article I will explain how to remove (delete) duplicate rows (records) from DataTable using DataView in C# .

Namespaces
You will need to import the following namespace.

using System.Data;

Code:C#

DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "Raja Durai", "United States");
        dt.Rows.Add(2, "Raja", "United States");
        dt.Rows.Add(3, "Durai", "India");
        dt.Rows.Add(4, "S.Raja Durai", "India");
        dt.Rows.Add(4, "S.Raja Durai ", "India");
        dt.Rows.Add(4, "S.Raja Durai ", "India");
dt = dt.DefaultView.ToTable(true, "Id", "Name", "Country");

How to get File size in ASP.NET/C#



I want to share with you how to get the file size in asp.net. Its very simple...

Code:C#

You will need to import the following namespace.
Using System.IO;

string sMyFileName = "~/rajadurai/raja.jpg";

FileInfo finfo = new FileInfo(Server.MapPath(sMyFileName ));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;
long FileInMB = FileInKB /1024;

lblImgSize.Text="File Size: " + FileInBytes.ToString() +" bytes (" + FileInMB.ToString() + " MB)";

Tuesday 30 July 2013

Check Null Value in SQL Server 2005

Below SQL Query is used to Check null Value in SQL Server 2005.

Syntax

IFNULL(ColumnName,0)

Example

select (case when ISNULL((Select sum(Amount) From Payment B Where B.Purchase_RefID=Purchase.Purchase_RefID  and Status in('Active')),'0') <>'0' THEN  (Select  sum(Amount)  From Payment B Where B.Purchase_RefID=Purchase.Purchase_RefID  and Status in('Active')) else '0' end ) as PaidAmount

Designing a Sub Report inside RDLC Report

  1. Create Report 1 , this is your main report.
  2. Create Report 2 , this will act as subreport.
  3. Now open report 1 and from toolbox drag the subreport control to this main report.
  4. Then Right click on the subreport control and configure it to Point to the Report 2.
  5. Configure the Parameter For subreport , if you have to pass any paramter.

SQL SERVER – CASE Statement/Expression Examples



CASE expressions can be used in SQL anywhere an expression can be used. Example of where CASE expressions can be used include in the SELECT list, WHERE clauses, HAVING clauses, IN lists, DELETE and UPDATE statements, and inside of built-in functions.

Syntax:
CASE expression
WHEN expression1 THEN expression1
[[WHEN expression2 THEN expression2] [...]]
[ELSE expressionN]
END

Example:
DECLARE @Test INT
SET
@Test = 3
SELECT
CASE @Test
WHEN 1 THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END

Check NULL Value in RDLC Report Viewer and Set Value as ' 0 '



In The Report viewer select table fields and select Expression of Table fields

and Write Expression as Follows


=IIf(IsNothing(Fields!TextBox1.Value),"0",Fields!TextBox1.Value)

Sunday 30 June 2013

MySQL Remote Connection



1. Open Your MySQL Application and select MySQL database.  Then Select USER table.


2. Right Click user Table And Click View Data






3. Then Add another row in table (Given Below).




A)    localhost row always will be there.
B)     The second row %,  is not there then create manually like the same value in localhost row
 
4. Then Click Startà MySQLà MySQL server 5.0 àMySQL Comment Line Client




5. Now “MySQL Comment Line Client “Appeared

6. in this comment box, First Type Your MySQL Password and Click ENTER Button
Then Type

 GRANT ALL PRIVILEGES ON *. * TO 'root' @ 'local host' IDENTIFIED BY '123 'WITH GRANT OPTION;”

 and Click ENTER Button.
 


6. Now this MySQL will be connected from other machines.


Wednesday 6 March 2013

Gridview MergeRows in asp.net

Give the below query in aspx page 


<asp:GridView ID="gvwRaja" runat="server"
        onprerender="gvwRaja_PreRender" onrowdatabound="gvwRaja_RowDataBound"
        BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
        CellPadding="3" CellSpacing="2">
      <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
      <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
      <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
      <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
      <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
      <SortedAscendingCellStyle BackColor="#FFF1D4" />
      <SortedAscendingHeaderStyle BackColor="#B95C30" />
      <SortedDescendingCellStyle BackColor="#F1E5CE" />
      <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>

Then give the below queryin aspx.cs page

protected void Page_Load(object sender, EventArgs e)

    {
        if (!IsPostBack)
            CreateTable();
    }
    private void CreateTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CountryId");
        dt.Columns.Add("CountryName");
        dt.Columns.Add("State");

        dt.Rows.Add("1", "India", "TamilNadu");
        dt.Rows.Add("1", "India", "Delhi");
        dt.Rows.Add("1", "India", "Andhra Pradesh");
        dt.Rows.Add("1", "India", "Maharashtra");
        dt.Rows.Add("2", "US", "Alabama");
        dt.Rows.Add("2", "US", "Hawaii");
        dt.Rows.Add("3", "UK", "United Kingdom");

        gvwRaja.DataSource = dt;
        gvwRaja.DataBind();
    }


  protected void gvwRaja_PreRender(object sender, EventArgs e)
    {
        Class2.MergeRows(gvwRaja);
    }

Then create a class file nane same as (Class2) 
Then give the query in class2 page 


public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
            {
                if (row.Cells[cellIndex].Text == previousRow.Cells[cellIndex].Text)
                {
                    row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
                    previousRow.Cells[cellIndex].Visible = false;
                }
            }
        }
    }


 Now run Your Program Your OutPut Looks Like Given Below
 

Url Rewrite in Asp.net


3 Different places need to modify in Web Configuration




Modify the above detail in web configuration

       Then set the link to the aspx page


  

Add   Intelligencia.UrlRewriter.dll “    in our project bin folder
 Then right click on the project and select Property Pages.


    When click the Property Pages a window appeared.  In this window we change   “Home.aspx to Home”
 



     Then click apply and click ok.
 

Wednesday 20 February 2013

How to export DataTable into Excel file in ASP.NET

There is a one simple function written below, which takes datatable as a parameter and give it popup to user to save the file as an excel file.
Copy and paste file in your code, and call this function on Event where it required

using System;
using System.Data;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Qualification", typeof(string));
        dt.Rows.Add("Raja", "M.B.A");
        dt.Rows.Add("Durai", "M.B.B.S");
        dt.Rows.Add("SRD", "BE");
        ExportToExcel(dt);
    }

public void ExportToExcel(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = "Raja.xls";
            string excelHeader = "Raja Report";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();
            // Report Header
            hw.WriteLine("<b><u><font size='3'> " + excelHeader + " </font></u></b>");
            //Get the HTML for the control.
            dgGrid.RenderControl(hw);
            //Write the HTML back to the browser.          
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();
        }
    }

 

Export DataTable To MicroSoft Word In C#

we have Export the DataTable as a Word Format. I hope the below code is useful to us.

using System;
using System.Data;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

protected void Page_Load(object sender, EventArgs e)
    {
        ExportToWord();
    }

protected void ExportToWord()
    {      
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Qualification", typeof(string));
        dt.Rows.Add("Raja", "M.B.A");
        dt.Rows.Add("Durai", "M.B.B.S");
        dt.Rows.Add("SRD", "BE");
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
        "attachment;filename=DataTable.doc");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word ";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }