Search This Blog

Wednesday, March 20, 2013

Open Excel file C#

C# > Diagnostics > Process > Start


Starts a process resource.


Example


using System.Diagnostics;
Process.Start("Excel.exe", @"C:\MyExcelfile.xlsx");

Populating a DataSet and table from a DataAdapter C#

C# > Data >  Populating DataSet and DataTable

SQL Adapter to executes the SQL statement or stored procedure referenced in its SelectCommand and put the results into a table in the dataset.
Than you call data adapter's Fill method.  
If the dataset contains multiple tables, you should have separate data adapters for each table and must therefore fill each table separately.

Example:

SqlConnection con = new SqlConnection("connString");
SqlCommand com = new SqlCommand("exec your_stored_procedure", con);
com.CommandTimeout = 30;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Data");
DataTable dt = ds.Tables[0];
con.Close();

           






Thursday, March 14, 2013

Set minimum and maximum value to GridViewDecimalColumn Telerik RadGridView


Telerik > Windows Forms > RadGridView > GridViewDecimalColumn

GridViewDecimalColumn allows decimal data to be displayed and edited and can be bound to fields of any numeric type.

Examples:

1. Add GridViewDecimalColumn in runtime

GridViewDecimalColumn decimalColumn = new GridViewDecimalColumn();
decimalColumn.Name = "Price";
decimalColumn.HeaderText = "Price";
decimalColumn.FieldName = "Price";
decimalColumn.DecimalPlaces = 2;
radGridView1.MasterTemplate.Columns.Add(decimalColumn);

2. Set minimum and maximum value to GridViewDecimalColumn Telerik RadGridView

GridViewDecimalColumn decimalColumn = (GridViewDecimalColumn)this.radGridView1.Columns[0];
decimalColumn.Minimum = 1;
decimalColumn.Maximum = 100;




How to limit the text length in RadGridView cell




void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (e.Column is GridViewTextBoxColumn)
{
((RadTextBoxEditorElement)((RadTextBoxEditor)this.radGridView1.ActiveEditor).EditorElement).MaxLength = 100;
}
}
}

 

Tuesday, March 12, 2013

Interface C#

C# > Types > Interface

An interface contains only the signatures of properties and methods.
The implementation of the methods is done in the class that implements the interface.

Interfaces can contain:

  • methods
  • properties
  • events
  • indexers

An interface can't contain:

  • constants
  • fields
  • operators
  • instance constructors
  • destructors
  • types


Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplicationCS
{
    interface IAnimal
    {
        string Name { get; set; }
        string Type { get; set; }
    }
    class Dog : IAnimal, IComparable
    {
        private string strName;
        private string strType;

        public Dog(string name)
        {
            this.Name = name;
        }

        public string Name
        {
            get { return strName; }
            set { strName = value; }
        }

        public string Type
        {
            get { return strType; }
            set { strType = value; }
        }

        public int CompareTo(object obj)
        {
            if (obj is IAnimal)
                return this.Name.CompareTo((obj as IAnimal).Name);
            return 0;
        }
    }


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Dog dog1 = new Dog("Dog1");
            dog1.Type = "Carnivore";

            Dog dog2 = new Dog("Dog2");
            dog2.Type = "Carnivore";

            Dog dog3 = new Dog("Dog3");
            dog3.Type = "Carnivore";

            int x = dog1.CompareTo(dog2);  // reurn -1
            int y = dog1.CompareTo(dog3);  // reurn 0
        }
    }
}






Friday, March 8, 2013

Find large tables size in SQL Server

SQL Server > Scripts

Microsoft SQL Server has two undocumented stored procedures that allow you to process through all tables in a database (sp_MSforeachtable), or all databases (sp_MSforeachdb).

sp_MSforeachtable

Example: Find tables size in SQL Server

SET NOCOUNT ON
CREATE TABLE #tbl_size
(
       tbl_name            sysname ,
       rows int,
       reserved_size VARCHAR(50),
       data_size           VARCHAR(50),
       index_size          VARCHAR(50),
       unused_size         VARCHAR(50)
)
INSERT #tbl_size
EXEC sp_msforeachtable 'sp_spaceused ''?'''
select
       *
from
       #tbl_size
order by
       CAST(REPLACE(data_size, ' KB', '') AS int) desc

drop table #tbl_size






Concatenate rows from table into a single text string SQL Server

SQL Server > XML Data > FOR XML > Concatenate rows into text lines

create table #person
(
       id           int,
       name   nvarchar(50)
)
insert into #person(id, name)
values (1,'john')
insert into #person(id, name)
values (1,'smith')
insert into #person(id, name)
values (2,'laura')
insert into #person(id, name)
values (2,'stan')

select * from #person

1      john
1      smith
2      laura
2      stan

select distinct
       p2.id,
    substring((Select ','+ p1.name   AS [text()] From #person p1 Where p1.id = p2.id
                ORDER BY p1.id For XML PATH ('')),2, 1000) name
         From #person p2


1 john,smith
2 laura,stan
drop table #person