Thursday, December 17, 2015

How to save XML from URL c# asp.net

 Here is the example for save XML to your system from the URL.

follow the below to save xml


             public HttpWebRequest GetRequest(string s)
             {
              bool automaticDetectProxy = true;
              var request = (HttpWebRequest)HttpWebRequest.Create(s);
            if (automaticDetectProxy)
            {
               IWebProxy proxy = WebRequest.GetSystemWebProxy();
               System.Net.CredentialCache.DefaultNetworkCredentials.UserName = "Username";
               System.Net.CredentialCache.DefaultNetworkCredentials.Password = "Password";
               System.Net.CredentialCache.DefaultNetworkCredentials.Domain = "Domain";
               proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
               request.Proxy = proxy;
               }
              request.PreAuthenticate = true;
             return request;
         }

              string url = "http://www.xxxx/yyyy
              var request = (HttpWebRequest)this.GetRequest(url);
              
              // //geting the response from the request url
              var response = (HttpWebResponse)request.GetResponse();
              
             // //create a stream to hold the contents of the response (in this case it is the contents of the XML file
              var receiveStream = response.GetResponseStream();
             
               // //creating XML document
              var mySourceDoc = new XmlDocument();
              try
              {
                  // //load the file from the stream
                  mySourceDoc.Load(receiveStream);
                  mySourceDoc.Save("E:\\Logistics\\XML_Gati\\" + tracking + ".xml");
                  // //close the stream
                  receiveStream.Close();
              }
              catch (Exception ex)
              {
                  Response.Write(ex.Message);
              }

Tuesday, December 15, 2015

How to color the Data of Gridview Control

here is the example for Gridview color.

Gridview color can be change as per requiement Back Color or Front Color

for(int i = 0; i<GridView.Rows.Count; i++)
 {
     Label lblStatus = (Label)GridView.Rows[i].FindControl("lblStatus");

     if ( lblStatus.Text  == 1 )
        {
           GridView.Rows[i].ForeColor = color.Red;
         }
    else
        {
            GridView.Rows[i].ForeColor = color.Green;
        }
 }

the above code is used for the Fore Color for Gridview Control


for(int i = 0; i<GridView.Rows.Count; i++)
 {
     Label lblStatus = (Label)GridView.Rows[i].FindControl("lblStatus");

     if ( lblStatus.Text  == 1 )
        {
           GridView.Rows[i].BackColor = color.Red;
         }
    else
        {
            GridView.Rows[i].BackColor = color.Green;
        }
 }


this will used for the back color..........

Monday, December 14, 2015

How to pivot in sql server

Here is the sql query to get pivot data from the database.

Let say we have a database named testDB having the column name DocNo,Customer and Month.
and we need to filter out the Customer in each month from Jan to June.

and we have data like

 001, ABC Customer, 01
 001, ABC Customer, 02
 002, ABC Customer, 03
 003, XYZ Customer, 03


this is the simple sql query to get out the data from the database.

select [01] as Jan,[02] as Feb,[03] as March
from (select DocNo,Customer,Month from testDB)
up pivot (count(DocNo) for [01],[02],[03] in Month) 
as NewPvtTable order by DocNo,Customer,[01],[02],[03]

the above query resulting the newly created pivot table 

How to send email from c# .net

hi everyone , this article having the example for sending the email from the coding ............

Step 1 . first of all we have to define the namespace System.Web and System.Web.Mail or System.Net and System.Net.Mail

Step 2 . create a public class for sending the email having the attributes from email, to email and cc email if having.

Example 1 : Send Email using the namespace System.Net.Mail

              public class EmailExample()
             {
                    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress();
                    System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress();
                    System.Net.Mail.MailAddress cc = new System.Net.Mail.MailAddress();
                 
                    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                    mail.CC.Add(cc);  /////this is optional
                    mail.Subject ="Enter Your mail suject here";
                    mail.Subject ="Enter Your mail body here";
                    mail.IsBodyHtml =true; /// if you use your email as html

                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); // your smtp
                    control to configure mail
                   smtp.Host = "enter your smtp host/exchange server";
                   smtp.Port = 25;
                   smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
     
                  smtp.EnableSsl = false;///if you are not using the default credentials
                  smtp.Credentials = new System.Net.NetworkCredential("Username",    
                  "Password");

                 smtp.Send(mail);
     
                 return true;
             }

Example 2 : Send Email using the namespace System.Web.Mail

              public class EmailExample()
             {                
                    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
                    mail.From = "XXX@yyy.com";
                    mail.To = "ZZZ@yyy.com";
                    mail.CC = "XXX@yyy.com"; ///optional
                    mail.Subject ="Enter Your mail suject here";
                    mail.Subject ="Enter Your mail body here";

                    mail.IsBodyHtml =true; /// if you use your email as html

                    System.Web.Mail.SmtpClient smtp = new System.Web.Mail.SmtpClient(); // your smtp
                    control to configure mail
                   smtp.SmtpSever = "enter your smtp host/exchange server";
                   smtp.Send(mail);
     
                   return true;
             }

After creating the class, use this class in your code.

i hope this is enough to send the mail from the intranet within the domain and outside the domain.