Wednesday 15 July 2015

WCF Errors

The type 'TransactionService.TransactionInfo', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

I changed the output path of the service. it should be inside bin folder of the service project. Once I put the output path back to bin, it worked.

WCF - Contract Name could not be found in the list of contracts

Your contract is the Interface not the implementation.Somewhere in the config you have written myService instead of IJsonService.

Monday 6 July 2015

Files Upload in FTP Using C#

using System.IO;
using System.Net;

Path = Server.MapPath("~/Folder/file_Name.txt");

private void UploadToFTP(string Path)
    {
        FileInfo fileInfo = new FileInfo(Path);
        string uri = "ftp://000.000.000.00/folder/" + fileInfo.Name;
        FtpWebRequest reqFTP;

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://000.000.000.00/folder/" + fileInfo.Name));
        reqFTP.Credentials = new NetworkCredential("userName""password");
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        reqFTP.UseBinary = true;
        reqFTP.UsePassive = false;
        reqFTP.ContentLength = fileInfo.Length;
        int buffLength = 10240;
        byte[] buff = new byte[buffLength];
        int contentLen;
        FileStream fs = fileInfo.OpenRead();
        try
        {
            Stream strm = reqFTP.GetRequestStream();
            contentLen = fs.Read(buff, 0, buffLength);
            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }
            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }

    }


Send Mail with Attachment Files Using Asp.net

using System.Net.Mail;

SmtpClient client = new SmtpClient();
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "Password");

            MailMessage mm = new MailMessage();
            mm.Subject = "Sending Mail Using Asp.net";
            mm.Body = "Hi, \n\n\Mail Sent Successfully \n\n\n --\n -- \nBest Regards\n"
                + "\n\n-----------------------------------------------------------------------\nThis is a system-generated e-mail, please don't reply to this message.";
            mm.Attachments.Add(new Attachment(File));
            mm.From = new MailAddress("xyz@gmail.com");
            mm.To.Add("xyz@gmail.com");             
            mm.CC.Add("xyz@gmail.com");
            client.Send(mm);

Saturday 4 July 2015

Print Gridview using JavaScript

<script  type="text/javascript">
        function CallPrint(strid) {
            var prtContent = document.getElementById(strid);
            var WinPrint = window.open('', '', 'letf=0,top=0,width=900,height=600,toolbar=1,scrollbars=1,status=1');
            WinPrint.document.write(prtContent.innerHTML);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
            prtContent.innerHTML = strOldOne;
            WinPrint.close();
        }

    </script>

<input id="btnPrint" onclick="javascript: CallPrint('divPrint')" type="button" value="Print" visible="True" />

<div id="divPrint" align="center">
            <asp:GridView ID="GridView2" runat="server"></asp:GridView>
</div>

Wednesday 1 July 2015

Dot Net Tricks

Showing number in 2 decimal places in gridview

Using DataFormatString="{0:0.00}" we can do


<asp:BoundField DataField="Number" HeaderText="Number with 2 decimal" ReadOnly="True" DataFormatString="{0:0.00}" SortExpression="Number">