December 25, 2011

Code to Create Zip file and Download using DotNetZip Library

Below is the code to create zip file on the server and download it

first download the DotNetZip Library from the below link

http://dotnetzip.codeplex.com/

Add the reference of Ionic.zip.dll  within your application.

Add below using statements to the Program



using Ionic.Zip;
using Ionic.Zlib;
using Ionic.BZip2;
using System.IO;
using System.Net;



Below is the sample code to create zip files.
Dataset being passed in below method has the URL's of the files to be added in to the zip file


  public void ZIPfiles(DataSet ds,String FileName)
    {
        try
        {
            Response.Clear();
            // no buffering - allows large zip files to download as they are zipped
            Response.BufferOutput = false;
            // String ReadmeText = "Dynamic content for a readme file...\n" +
            // DateTime.Now.ToString("G");
            string archiveName = String.Format(FileName + ".zip",
                                              DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment; filename=" + archiveName);

            ZipFile zip = new ZipFile();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {


                string appPath = System.AppDomain.CurrentDomain.BaseDirectory;
                //  string appPath = HttpContext.Current.Request.ApplicationPath;
                String filePath = ds.Tables[0].Rows[i]["ImageURL"].ToString().Remove(0, 1).ToString();
                // add a file entry into the zip, using content from a string
                zip.AddFile(appPath + filePath);
           
                // compress and write the output to OutputStream
            }

            zip.Save(System.AppDomain.CurrentDomain.BaseDirectory + "DateWiseOrder.zip");

            //download the above Zipped file from the server
            //String filePath1 = System.Web.HttpContext.Current.Server.MapPath("~/" +"DateWiseOrder.zip");

            //HyperLink1.NavigateUrl = Server.MapPath("~/")+"DateWiseOrder.zip";

            try
            {
                string strURL =  "DateWiseOrder.zip";
                WebClient req = new WebClient();
                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.ClearContent();
                response.ClearHeaders();
                response.Buffer = true;
                response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
                byte[] data = req.DownloadData(Server.MapPath(strURL));
                response.BinaryWrite(data);
                response.End();
            }
            catch (Exception ex)
            {
            }
       
        }

        catch (Exception ex)
        {


        }
   
    }

Code to Copy Bulk Data from One Table to another using SqlBulkCopy

Below code sample is how to copy bulk data from one table to another in sqlServer from ASP.NET (C#)

//Create SqlBulkCopy Object
 SqlBulkCopy sbc = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString()); 


//Code to copy Data from Source to Destination

  try
            {
                //Read Data from Source
                SqlDataReader dr = sqlobject.RunQueryReturnRedaer("Select * from Orders");


                sbc.DestinationTableName = "BackUpOrders";
                sbc.WriteToServer(dr);




            }
            catch (Exception ex)
            {


                evtlog.WriteError(ex.Message);
            }




//Read Data from Source

public SqlDataReader RunQueryReturnRedaer(string strSql)
    {
        SqlCommand cmd = new SqlCommand(strSql, connObj);
        createConnection();
        SqlDataReader rdr = cmd.ExecuteReader();
        return rdr;
    }

December 14, 2011

Details View Row Invisible from Code Behind

Below is the sample code if you want to make some rows invisible in details view

//Method to Bind details View from DB

 public void GetUserProfile()
    {
        DataSet ds = sqlobj.RunQueryReturnDataSetonLoginDB("exec [GetServiceUsage] '" + Session["UserId"].ToString() + "'" + "," + 0);
        if (ds != null)
        {
            if (ds.Tables[0] != null) 
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dView.DataSource = ds.Tables[0];
                    dView.DataBind();


                }


            }


        }




    }


//Make Specific rows which have cell text ="0" invisible

    if (dView != null)
            {
                DataRowView row = dView.DataItem as DataRowView;
                
                    if (row  != null)
                    {
                       //Row 1
                        if (row["Testrow"].ToString() == "0" )
                        {
                            dView.Rows[0].Visible = false;


                        }


                      //Row 2
                        if (row["Testrow2"].ToString() == "0" )
                        {
                            dView.Rows[1].Visible = false;


                        }


                    }
                
            }