Pages

Thursday, 18 June 2015

Code to EXPORT crystalreport to PDF

Introduction: In this article I will explain how to save the CrystalReport as PFD file. Saving pdf format of crystalreports is very important to keep the record.

Code :
Here is the complete code:

try
            {
                if (!Directory.Exists("D:\\Sanjeev\\"))
                {
                    Directory.CreateDirectory("D:\\Sanjeev\\");
                }
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = "D:\\Sanjeev\\newpdf.pdf";
                CrExportOptions = b.ExportOptions;
                {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                }
                b.Export();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


Javascript code to add GRIDVIEW row

Introduction:  JavaScript Code to add row to a GridView.

Code:

<script type="text/javascript" lang="ja">
 function AddNewRecord() {
      var grd = document.getElementById('<%=gvportfolio.ClientID%>');
      var tbod = grd.rows[0].parentNode;
      var newRow = grd.rows[grd.rows.length - 1].cloneNode(true);
      tbod.appendChild(newRow);
      }

</script>

Sunday, 14 June 2015

Connection String in Web.config

The web.config file stores the website configuration data, the connection string is used to store information on database connection.

The connection  string to connect to a local sql server database is :-

<connectionStrings>
    <add name="conn" connectionString="Data Source=.; Initial Catalog=DataBaseName; Integrated Security='True'" providerName="System.Data.SqlClient"/>
  </connectionStrings>

The connection string to connect to a server :-

<connectionStrings>
    <add name="conn" connectionString="Server=ServerAddress; Database=DatabaseName; User ID=userID; Password=password; Trusted_Connection=False" />
  </connectionStrings>

Tuesday, 9 June 2015

Upload file to FTP from windows form applications

Overview : Sometimes we need to upload some files to a web server from our desktop application. The file upload task is pretty easy in web applications but in desktop applications it is confusing because in websites we upload the files on the same server where we are working but in desktop applications we are working on a local computer and the server is come where else.
To overcome this issue I have created a function which will do this task for you.

public static void Fileuploader(string localpath, string username, string password)
        {
            var fileName = Path.GetFileName(localpath);
            var request = (FtpWebRequest)WebRequest.Create("ftp://www.example.com/httpdocs/_sft0258/" + fileName);

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            using (var fileStream = File.OpenRead(localpath))
            {
                using (var requestStream = request.GetRequestStream())
                {
                    fileStream.CopyTo(requestStream);
                    requestStream.Close();
                }
            }

            var response = (FtpWebResponse)request.GetResponse();
            Console.WriteLine("Success: {0}", response.StatusDescription);
            response.Close();
        }


By calling this function with correct parameters the upload file task will be completed smoothly.