Friday, November 26, 2010

XAML not working properly in WPF Application

In some time the XAML wont work properly for WPF Application . To work properly run this text on run command

"%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\ide\devenv" /resetSkipPkgs

Wednesday, November 17, 2010

Copy Files and Folder from one path to another path

Copying the files and folder one path and paste in some other path.


try
            {
                //check if the target directory exists
                if (Directory.Exists(target.FullName) == false)
                {
                    Directory.CreateDirectory(target.FullName);
                }

                //copy all the files into the new directory
                foreach (FileInfo fi in source.GetFiles())
                {
                    fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
                }

                //copy all the folders into the new directory
                foreach (DirectoryInfo diSourceDir in source.GetDirectories())
                {
                    DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
                    CopyAll(diSourceDir, nextTargetDir);
                }

                MessageBox.Show("Copied Successfully");
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }




Friday, November 12, 2010

LINQ To SQL


LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query. Your application is free to manipulate the objects while LINQ to SQL stays in the background tracking your changes automatically.

Here I have created one small database application with basic operations using c# and I have implemented Linq to sql concept.
Step 1: Create one windows application like this






Step 2: Create a database and table with this structure.

















Step 3: Add new dbml file(LINQ to SQL Classses).




 













Step 4: Add new Data Connection in Server Explorer window.

Step 5: Drag the database tables into dbml file



















Once you dragged the tables you can see the classes are created in designer.cs (class file). Each table act as one class and the columns act as properties.

Now you can write the code for buttons
To Add New Record



private void btnAdd_Click(object sender, EventArgs e)
        {
            //Creating instance of dbml class
            Employee_DataClassesDataContext db = new Employee_DataClassesDataContext();

            try
            {
                //Createing instance of table
                EmployeeTable employee = new EmployeeTable();

                //Assigment of values to columns
                employee.Emp_ID = Convert.ToInt32(txtEmpId.Text);
                employee.Emp_Name = txtname.Text;
                employee.Address = txtAddress.Text;
                employee.Contactno = txtContact.Text;

                //Adding the instance to the database
                db.EmployeeTables.InsertOnSubmit(employee);
                db.SubmitChanges();


                //To retrieve the employee details from database
                List lstEmployee = new List();
                lstEmployee = (from emp in db.EmployeeTables
                               select emp).ToList();

                //Bind the data to gridview
                dataGridView2.DataSource = lstEmployee;

                MessageBox.Show("Employee Details Added Successfully");
            }
            catch (Exception)
            {
                MessageBox.Show("Error on adding Employee details");
            }
        }

To Updated Existing Record

private void btnUpdate_Click(object sender, EventArgs e)
        {
            //Creating instance of dbml class
            Employee_DataClassesDataContext db = new Employee_DataClassesDataContext();

            try
            {
                //Createing instance of table
                EmployeeTable employee = new EmployeeTable();

                int id = Convert.ToInt32(txtEmpId.Text);

                //fetching the record which is going to update
                employee = (from emp in db.EmployeeTables
                            where emp.Emp_ID == id
                            select emp).SingleOrDefault();

                //Assigment of new values
                employee.Emp_Name = txtname.Text;
                employee.Address = txtAddress.Text;
                employee.Contactno = txtContact.Text;

                //Updating in database
                db.SubmitChanges();

                MessageBox.Show("Employee Details Updated Successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error on Updating Employee Details");
            }

        }


To Delete a Record

private void btnDelete_Click(object sender, EventArgs e)
        {
            //Creating instance of dbml class
            Employee_DataClassesDataContext db = new Employee_DataClassesDataContext();

            try
            {
                //Createing instance of table
                EmployeeTable employee = new EmployeeTable();

                int id = Convert.ToInt32(txtEmpId.Text);

                //fetching the record which is going to update
                employee = (from emp in db.EmployeeTables
                            where emp.Emp_ID == id
                            select emp).SingleOrDefault();

                if (MessageBox.Show("Are you sure to Delete?", "Confirm Box", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //Delete the record
                    db.EmployeeTables.DeleteOnSubmit(employee);

                    //Updating in database
                    db.SubmitChanges();
                }

                //To retrieve the employee details from database
                List lstEmployee = new List();
                lstEmployee = (from emp in db.EmployeeTables
                               select emp).ToList();

                //Bind the data to gridview
                dataGridView2.DataSource = lstEmployee;

            }
            catch (Exception Ex)
            {

            }
        }

Its a simple example i wrote but. LINQ to SQL contains lots of interesting thinks lets see in other article 
Thanks for reading this article

Wednesday, November 10, 2010

List all the Database, Table, Columns name in Sql Server

Sql Server 2005

  • List All Database name from Sql Server
               Select name from Sys.databases;

  • List All Tables from Database 
       use Sample select name from Sys.tables; --> Sample is Database Name
                  
  • List All Column names from table
       use Sample select column_name, data_type from information_schema.columns   where table_name= 'Department'; --> Sample is Database Name and Department is Table Name

  Thanks for Reading this article





Tuesday, November 9, 2010

Distinct list with class property in Linq

In some scenario you may need to group by one of the property in collection so there is no direct way to group but we can use this query

lstTempOutlet = lstOutlets.GroupBy(o =>o.propertyname).Select(o => o.First()).ToList<>();

In case to multiple properties 

lstTempOutlet = lstOutlets.GroupBy(o => new {o.propertyname1, o.propertyname2, o.propertyname3 , etc.,}).Select(o => o.First()).ToList<>();