Skip to main content

Posts

Showing posts with the label C#.net Programming

C Sharp code for Sending an email

C# code for Sending an email private void send_mail() { MailMessage mm = new MailMessage("write_emailid_reciever", txtEmailId.Text); mm.Subject = "Thnaks for your Feedback."; mm.Body = "Dear Costomer Thanks for your feedback.You are more than welcome to give us a try again. Thank you."; SmtpClient sc = new SmtpClient("mailing_server", 25);//25-is smtp port number NetworkCredential nc = new NetworkCredential(); nc.UserName = "write_emailid_sender"; nc.Password = "password"; sc.Credentials = nc; sc.Send(mm); }

Change textbox color if textbox is empty in asp.net using c#

Change textbox color if textbox is empty in asp.net using c# private void button1_Click(object sender, EventArgs e) { textBox1.BackColor = Color.White; textBox2.BackColor = Color.White; textBox3.BackColor = Color.White; textBox4.BackColor = Color.White; string[] text = new string[4]; text[0] = System.Convert.ToString(textBox1.Text); text[1] = System.Convert.ToString(textBox2.Text); text[2] = System.Convert.ToString(textBox3.Text); text[3] = System.Convert.ToString(textBox4.Text); for (int i = 0; i <= 3; i++) { if (text == "") { errorshow(i); } } } private void errorshow(int k) { switch (k) { case 0: textBox1.BackColor = Color.Red; break; case 1: textBox2.BackColor = Color.Red; break; case 2: textBox3.BackColor = Color.Red; break; case 3: textBox4.BackColor = Color.Red; break; } }

Code To create event handler at run time on button click in asp.net using c#

Code To create event handler at run time on button click in asp.net using c# using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button btn2 = new Button(); form1.Controls.Add(btn2); btn2.Text = "Example1"; btn2.Click += new EventHandler(btn2_Click); } void btn2_Click(object sender, EventArgs e) { Response.Write("Gooooooogle"); } }

Validation for future date in c sharp dot net lanugage

Validation for future date in c sharp dot net lanugage you can write this code in validation class which you may have in common class file. public static bool IsNotFuturedt(DateTimePicker datpic) { if (datpic.Value.Date > DateTime.Now.Date) { MessageBox.Show("Please enter date less than today "); datpic.Value = DateTime.Now; datpic.Focus(); return false; } else { return true; } }

Get data from Table using dataTable using c#.net

Get data from Table using dataTable using c#.net static public DataTable getData(string selectstr) { string connectionString = ""; OleDbConnection connection = null; OleDbDataAdapter adp = null; OleDbCommandBuilder oleDbCommandBuilder = null; DataTable dataTable = null; connectionString = ConfigurationSettings.AppSettings["ConnectionString"]; connection = new OleDbConnection(connectionString); connection.Open(); adp = new OleDbDataAdapter(selectstr, connection); oleDbCommandBuilder = new OleDbCommandBuilder(adp); dataTable = new DataTable(); adp.Fill(dataTable); connection.Close(); return dataTable; }

Coloring in particular cells using data grid

Coloring in particular cells using data grid foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows) { DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle(); dataGridViewCellStyle.ForeColor = Color.Red; if (dataGridViewRow.Cells[2].Value != null) { if (dataGridViewRow.Cells[2].Value.ToString().Trim().Equals("M", StringComparison.CurrentCultureIgnoreCase)) { dataGridViewRow.DefaultCellStyle = dataGridViewCellStyle; } } }

Example of abstract and interface class using c sharp

Example of abstract and interface class using c sharp namespace abstractClassDemo { class Program { static void Main(string[] args) { Car mycar = new Car("Skoda", "Fabia2CX"); mycar.ModelName(); mycar.BodyColor(); mycar.CubicCapacity(); mycar.Mileage(); Console.ReadKey(); } } abstract class Vehicle { string _model = ""; string _type = ""; public Vehicle(string company,string type) { _model = company; _type = type; } public abstract void CubicCapacity(); public abstract void BodyColor(); public abstract void Mileage(); public void ModelName() { Console.WriteLine("Vehicle is " + _type + " and Company is " + _model); } } class Car: Vehicle { public Car(string companyName, string modelName) : base(companyName, modelName) { } public override void BodyColor() { Console.Write("Color is Red."); } pub...

Data Decryption using C# Code

Data Decryption using C# Code public static byte[] Decryptor(string secrpcnm, string key) { byte[] b = Convert.FromBase64String(secrpcnm); TripleDES des = CreateDES(key); ICryptoTransform ct = des.CreateDecryptor(); byte[] output = ct.TransformFinalBlock(b, 0, b.Length); return Convert.FromBase64String(Encoding.Unicode.GetString(output)); } static TripleDES CreateDES(string key) { MD5 md5 = new MD5CryptoServiceProvider(); TripleDES des = new TripleDESCryptoServiceProvider(); des.Key=md5.ComputeHash(Encoding.Unicode.GetBytes(key)); des.IV = new byte[des.BlockSize / 8]; return des; } //And to call the functions on the click event of a button:: private void button4_Click(object sender, EventArgs e) { label6.Text = (EncrypCls.Decryptor(label5.Text, "sksdege")).ToString(); }

Encapsulate some code module in the code behind file of c#

Add a new project to you solution as class library. You can find class library under C# windows section. Name it as per your convenience. after adding, you will get a class file, with the name of you supplied. make this class public. Now you can write you logic in this class file, build it. it will create a dll file. add reference by right clicking on you main application project file select "Add reference", path of dll file. in your application, add the namespace of your class file and then call class and its methods. below is a sample class file using System; using System.Data; namespace myEncapsulatedNameSpace { public class myEncapsulatedClass { //property declaration //properties are used for passing values between application layers private string _myName = string.empty; public string myName { get { return _myName; } set { _myName = value; } public string myMethod() { return myName = "sk";...

Upload Image using code using c sharp dot net

This is example of c#.net code to Upload Image through image upload control and save image to table of database. using System.Data; using System.Data.SqlClient; public partial class ImageUpload : System.Web.UI.Page {     SqlConnection sqlcon=new SqlConnection(@"Server=SQLEXPRESS;database=abc;uid=xxxx;pwd=yyyy;");     SqlCommand sqlcmd=new SqlCommand();     String ImageName,ImageType;     protected void Button1_Click(object sender, EventArgs e)     {         if (FileUpload1.HasFile)         {             int len = FileUpload1.PostedFile.ContentLength;             byte[] picture = new byte[len];             ImageName = FileUpload1.FileName;     ...

code to send Mail From Web Application using c sharp

using System.Web.Mail; using System; public class MailSender { public static bool SendEmail( string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, MailFormat pFormat, string pAttachmentPath ) { MailMessage myMail = new MailMessage(); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","smtp.gmail.com"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpserverport","465"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusing","2"); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1"); //Use 0 for anonymous myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendusername",pGmailEmail); myMail.Fields.Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword...