Sunday, August 11, 2013

How to write Data In XML with some attributes

                string dir = HttpContext.Current.Server.MapPath("~/Errors/");
                string FileName = HttpContext.Current.Server.MapPath("~/Errors/AKAARERRORFILE.xml");

                string date = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" +                     DateTime.Now.Year.ToString();
                XmlTextReader reader = new XmlTextReader(FileName);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();

                XmlElement parentelement = doc.CreateElement("Date");
                XmlAttribute dateatt=doc.CreateAttribute("date");
                dateatt.Value = date;
                parentelement.Attributes.Append(dateatt);
                XmlElement thepage = doc.CreateElement("page");
                thepage.InnerText = page;
                XmlElement thefunction = doc.CreateElement("fun");
                thefunction.InnerText = function;
                XmlElement thedes = doc.CreateElement("des");
                thedes.InnerText = description;

                parentelement.AppendChild(thepage);
                parentelement.AppendChild(thefunction);
                parentelement.AppendChild(thedes);
                doc.DocumentElement.AppendChild(parentelement);

                doc.Save(FileName);

               Download Example

Tuesday, July 30, 2013

Monday, June 17, 2013

How to Backup and Restore .bak file using C# 2010

references :
http://www.geekpedia.com/tutorial180_Backup-and-restore-SQL-databases.html
http://www.codeproject.com/Articles/123441/SQL-Server-2008-Backup-and-Restore-Databases-using
http://www.c-sharpcorner.com/uploadfile/rohatash/restore-sql-server-backup-file-with-c-sharp/
http://manish4dotnet.blogspot.com/2013/03/RestoreDataBaseusingCSharp.html
......................................................................................................................................................

//Using namespaces :
// these namespace found in "c:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\"
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using System.Collections.Specialized;


                 ServerConnection srvConn = new ServerConnection(Environment.MachineName);
                //my SQL user doesnt have sufficient permissions,
                //so i am using my windows account
                srvConn.LoginSecure = true;
                //connection.LoginSecure = false;
                //connection.Login = "testuser";
                
                //connection.Password = "testuser";
             


  public static void RestoreDB(string backUpFilePath, string databaseName)
        {
            try
            {
                Restore restore = new Restore();
                //Set type of backup to be performed to database
                restore.Database = databaseName;
                restore.Action = RestoreActionType.Database;

                // Replace the Db if already exists
                restore.ReplaceDatabase = true;

                //Set up the backup device to use filesystem.         
                restore.Devices.AddDevice(backUpFilePath, DeviceType.File);
                //set ReplaceDatabase = true to create new database
                //regardless of the existence of specified database
                restore.ReplaceDatabase = true;
                //If you have a differential or log restore to be followed,
                //you would specify NoRecovery = true
                restore.NoRecovery = false;
                Server sqlServer = new Server(srvConn);
                //SqlRestore method starts to restore database           
                restore.SqlRestore(sqlServer);
                MessageBox.Show("Restore operation succeeded");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Restore operation failed");
                MessageBox.Show(ex.Message);
            }
            Console.ReadLine();
        }
===============================
AND to call this Method :

RestoreDB(@"C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\SmallStoreme.bak", "SmallStore");

===============================
  public static void BackUpDB(string databaseName)
        {
                 // If there was a SQL connection created
                  // Create a new backup operation
                    Backup bkpDatabase = new Backup();
                    // Set the backup type to a database backup
                    bkpDatabase.Action = BackupActionType.Database;
                    // Set the database that we want to perform a backup on
                    bkpDatabase.Database =databaseName;

                    // Set the backup device to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName,    
                           DeviceType.File);
                    // Add the backup device to the backup
                    bkpDatabase.Devices.Add(bkpDevice);
                    // Perform the backup

                    bkpDatabase.SqlBackup(srvSql);
         }
===============================
AND to call this Method :

BackUpDB("SmallStore");

===============================

Monday, May 6, 2013

Make Combobox in C# Winforms Autocomplete

               // This Code is working properly
               // Just Copy and paste it 


                AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
                foreach (DataRow row in MyDatatable.Rows)
                {
                    //looping through the datatable and adding values from datatable into autocomplete collection
                    ac.Add(row["CustName"].ToString());
                }
                comboCustomerName.AutoCompleteSource = AutoCompleteSource.CustomSource; //assigning  
                // autocomplete source to the combobox
                comboCustomerName.AutoCompleteCustomSource = ac;

Monday, April 22, 2013

How To get The Focused Control


Import namespaces


using System.Runtime.InteropServices;
using System.Windows.Forms;


Win API

[DllImport("user32.dll")]
static extern IntPtr GetFocus();

Use

Control focused = null;
IntPtr handle = GetFocus();
if (handle != IntPtr.Zero)
{
    focused = Control.FromHandle(handle);
}
messageBox(focused.Name);