Wednesday, October 13, 2010

Get a selected object in datagridview and GridControl

///******GridControl******\\\
private void gridView_DoubleClick(object sender, EventArgs e)
            {
                   CustomObject  ObjectX = (CustomObject)((GridView)sender).GetFocusedRow();
                   // Do what ever you need with the objectX
            }

The following sample code can be used to get the value of the Row within the focused row.

DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);


///******DataBridView******\\\ 

private void gridRowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           foreach (DataGridViewRow row in gridCategorias.SelectedRows)
           {
               Categoria categ = row.DataBoundItem as Categoria;
               // Do what ever you want with the "Customn Object" Categoria
          }
        }

Thursday, October 7, 2010

how to detect wich key was press

This code detects when any kay is pressed

Thursday, July 8, 2010

Import Excel File Into SQL Server

First we need to enable the use of "Ad Hoc Distributed Queries" by using "SP_CONFIGURE"

1.- Open SQL server & New Query
2.- Run the command sp_configure
















3.- If we can not see the option "Ad Hoc Distributed Queries", we need activate "show advanced options"
     with the command "sp_configure 'show advanced options', 1", and then the commando        "reconfigure".

4.- If you run again the command "sp_configure" you will see the "Ad Hoc Distributed Queries" 

 
5.- Now we need anable Ad Hoc Distributed Queries, with the follow command "sp_configure 'Ad Hoc Distributed Queries', 1 " and the "reconfigure"

6.- Finally we are going to execute the following Query.

SELECT * INTO table FROM  OPENROWSET ('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\Book1.xls', 'SELECT * FROM [Sheet1$]')

Where:
table = New table name.
Database = Excel file path
Sheet1 = Name Sheet

Saturday, May 8, 2010

Skymonkeys

Imagen


 

Monday, April 19, 2010

Restore a Data Base from C#

 private void btnRestore_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (File.Exists(txtPath.Text + txtDireccion.Text + ".bak"))
                {
                    if (MessageBox.Show("¿Está seguro de restaurar?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (Program.laconexion.State != System.Data.ConnectionState.Open)
                            Program.laconexion.Open();

                        SqlCommand command = new SqlCommand("use master", Program.laconexion);
                        command.ExecuteNonQuery();
                        command = new SqlCommand(@"restore database FABRINOX from disk ='"+txtPath.Text + txtDireccion.Text + ".bak'", Program.laconexion);
                        command.ExecuteNonQuery();
                        Program.laconexion.Close();

                        MessageBox.Show("Se ha restaurado la base de datos", "Restauración", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                }
                else
                    MessageBox.Show(@"No haz hecho ningun respaldo anteriormente (o no está en la ruta correcta)", "Restauracion", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

Back up a Data Base from C#

 private void btnGuardar_Click(object sender, EventArgs e)
        {
                bool desea_respaldar = true;
                Cursor.Current = Cursors.WaitCursor;

                if (Directory.Exists(txtPath.Text))
                {
                    if (File.Exists(txtPath.Text + txtDireccion.Text+ ".bak"))
                    {
                        if (MessageBox.Show(@"Archivo existe ¿desea remplazarlo?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            File.Delete(txtPath.Text + txtDireccion.Text + ".bak");
                        }
                        else
                            desea_respaldar = false;
                    }
                }
                else
                    Directory.CreateDirectory(txtPath.Text);


                if (desea_respaldar)
                {
                    if (Program.laconexion.State != System.Data.ConnectionState.Open)
                        Program.laconexion.Open();
                    SqlCommand command;
                    command = new SqlCommand(@"backup database FABRINOX to disk ='" + txtPath.Text + txtDireccion.Text + ".bak" + "' with init,stats=10", Program.laconexion);
                    command.ExecuteNonQuery();

                    Program.laconexion.Close();

                    MessageBox.Show("Respaldo Exitoso");
                    this.Close();
                }
          
        }

Monday, March 29, 2010

Clear all Textboxes on a Form or Group control

//On a Form
foreach(Control ctl in this.Controls)
       {
         if (ctl is TextBox)
            (ctl as TextBox).Text = "";
       }

 //On Group Control
foreach (Control ctrls in gcGeneralInfo.Controls)
            {
                if (ctrls is TextBox)
                    ctrls.Text = string.Empty;
            }

Thursday, February 25, 2010

Constructors in C#

Constructors are class methods that are executed when an object of a given type is created. Constructors have the same name as the class, and usually initialize the data members of the new object.

In the following example, a class called Taxi is defined with a simple constructor. This class is then instantiated with the new operator. The Taxi constructor is invoked by the new operator immediately after memory is allocated for the new object.



 
     public class Taxi
     {
         public bool isInitialized;
         public Taxi()
         {
             isInitialized = true;
         }
     }

     class TestTaxi
     {
         static void Main()
         {
             Taxi t = new Taxi();
             System.Console.WriteLine(t.isInitialized);
         }
     }
 
A constructor that takes no parameters is called a default constructor.
Default constructors are invoked whenever an object is instantiated using 
the new operator and no arguments are provided to new 
 
Passing Arguments to Constructors 
 
Constructors may also be used to pass in initial values for an object's attributes
at the time that an object is being instantiated. Instead of creating an object whose
attributes are all initialized to zero-equivalent values, and then utilizing the accessors
provided by that class to initialize attribute values one-by-one, as illustrated
by the next snippet:
 
     //Create a bare boones Student onject
     Student s = new Student();
 
     //Initialize the atributes one-by-one
     s.name = "Ivan Osorio";
     s.ssn = "06210056";
     s.major = "English";
 
the initial values for selected attibutes can all passed in as a single step when
the constructor is called, if desired:

     Student s = new Student("Ivan Osorio", "06210056", "English");

In order to accommodate this, we'd have to define a Student constructor with an
appropriate header, as shown here:

     public class Student
     {
          //Atributes
          private string name;
          private string ssn;
          private straing major;
 
          /* We've programmed a constructor with three parameters to accommodate
             passing in argument values. */

          public Student (string s, string n, string m)
          {
               name = n;
               ssn = s;
               major = m; 
          }
     }

Wednesday, February 24, 2010

Saturday, January 16, 2010

Configure SQL Server Authentication

In order to use SQL Server authentication you must first configure your server by login with windows authentication.

Right-click on the server node and select 'Properties'.


On the new window select 'Security' from the left menu, select the 'SQL Server and Windows Authentication mode option, click 'OK' to close the dialog.

In the server node expand 'Security' and 'Logins', right click on the login name and select 'Properties'.Under the new window enter a password and confirm the password. Select 'Status' from the left menu, set the 'Login' option to 'Enabled' and click 'OK' to close the dialog.
Now right click on the server node and choose 'Restart' for the changes to take affect and connect with SQL Server Authentication, user 'sa' and you new password.

If you can't connect, you may need restart the service.

Go to Start => All Programs => Micrsoft SQL Server 2005 => Configuration Tools => SQL Server Configuration Manager.

From the left menu, select 'SQL Server 2005 Services', in the right menu right click on 'SQL Server(SQLEXPRESS) and restart it.

Monday, January 11, 2010

Use lambda

Lambda expressions use the lambda operator =>, which is read as "goes to".
(input parameters) => expression
The => operator has the same precedence as assignment (=) and is right-associative.

Example
 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);

Lamda Expressions are a simpler syntax for anonymous delegates and can be used everywhere an anonymous delegate can be used. However, the opposite is not true;
lambda expressions can be converted to expression trees which allows
for a lot of the magic that LINQ to SQL.

The following is an example using anonymous delegates then lambda expressions.

==Delegate==

delegate(int x)
{
return (x % 2) == 0;
}
==Lambda==
(x => (x % 2) == 0)