Export Datatable To ExcelSheet in C#

public void ExportExcel()
{

try
{

string Apath = “”;

Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
// Add column headings…
int iCol = 0;

foreach (DataColumn c in DataTable.Columns)
{

  iCol++;
excel.Cells[1, iCol] = c.ColumnName;

}

// for each row of data…
int iRow = 0;
foreach (DataRow r in DataTable.Rows)
{

iRow++;
// add each row’s cell data…
iCol = 0;
foreach (DataColumn c in UserDT.Columns)
{

iCol++;
excel.Cells[iRow + 1, iCol] = r[c.ColumnName];

}

}
// Global missing reference for objects we are not defining…
object missing = System.Reflection.Missing.Value;

SaveFileDialog save = new SaveFileDialog();
save.Filter = “Xls Files|*.xls”;
save.ShowDialog();
Apath = save.FileName;

// If wanting to Save the workbook.
workbook.SaveAs(Apath,
Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,false, false, Excel.XlSaveAsAccessMode.xlNoChange,missing, missing, missing, missing, missing);
// If wanting to make Excel visible and activate the worksheet.
excel.Visible = true;
Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;

((Excel._Application)excel).Quit();

 LblErrorMsg.Text = “Message : Excel Exported…”;

}
catch (Exception)
{

}

}

Auto Complete In Textbox C#

  • Bring Data In DataTable.
  • Create the Function  and Call at Load Event.

public void AutofillTextbox()

{

AutoCompleteStringCollection Auto = new AutoCompleteStringCollection();

for (int i = 0; i < DTItem.Rows.Count; i++) //DTItem is a DataTable Name.
{

Auto.Add(DTItem.Rows[i][“CategoryName”].ToString()); // CategoryName is the Name of Columm in DataTable that has to be AutoComplete in TextBox.

}
TxtSearch.AutoCompleteMode = AutoCompleteMode.Suggest; //TxtSearch is the Textbox in Which AutoComplete is Set.
TxtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
TxtSearch.AutoCompleteCustomSource = Auto;

}

Send Email In C#

public void Mail
{

try
{

// Creating a new mail message. The sender and receiver are
// indicated as sendermail@gmail.com and receivermail@yahoo.com
MailMessage mail = new MailMessage();

// Creating a new SMTP Client. The server URL/IP is indicated as
// sendServer.Text (that is the text box with the data).
SmtpClient SmtpServer = new SmtpClient(“smtp.gmail.com”);

mail.From = new MailAddress(“sendermail@gmail.com”);//Set The Email Address Of the Sender.
mail.To.Add(“receivermail@yahoo.com”);//Set The Email Address Of Reciever.
mail.Subject = “Test Mail”;//Subject Of The Mail.
mail.Body = “This is for testing SMTP mail from GMAIL”;//It Is the Main Body Of The Email. Also Append HTMl Code.  

 // Some servers require a specific port to connect
SmtpServer.Port = 587;

 // To be able to send the message, it is necessary to provide the
// credentials on the server. The username and the password is to give.                
SmtpServer.Credentials = new System.Net.NetworkCredential(“sendermail@gmail.com”, “password”);
SmtpServer.EnableSsl = true;

// Send the Mail.
SmtpServer.Send(mail);
MessageBox.Show(“Mail Send”);

}
catch (Exception ex)
{

//Console.WriteLine(ex.ToString());

}

}