Search This Blog

Thursday, October 29, 2015

SQL Server XML Data

SQL Server > XML Data

XML support is integrated into all the components in SQL Server.






Tuesday, October 27, 2015

OrderBy Key Dictionary C#

C# > LINQ Query Expressions > OrderBy Key Dictionary

dictionary.OrderBy(key => key.Key);






Saturday, October 17, 2015

'cmake' is not recognized as an internal or external command error

'cmake' is not recognized as an internal or external command

Solution:

Add the path to system variables Path










Thursday, October 15, 2015

Visual Studio show Decrease Increase Line Indent Buttons

Visual Studio > Decrease/Increase Line Indent buttons

Go o the toolbar and press:
than check Decrease/Increase Line Indent:






Wednesday, October 7, 2015

Find max value of a column in a DataTable c#

C# > System.Data   > DataTableMax Value

Find max value of a column in a DataTable C#.

var maxValue = DataTable1.AsEnumerable().Max(r => r.Field<double>("value"));





Thursday, October 1, 2015

Telerik report bind to DataTable and add a sample calculated field

Telerik > Reporting > Bind to DataTable

Example
Bind to DataTable and add a sample calculated field.

string connString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=HR;Integrated Security=True";
string cmdText = "SELECT FirstName, LastName FROM Person;";

SqlDataAdapter da = new SqlDataAdapter(cmdText, connString);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);

Telerik.Reporting.ObjectDataSource ods = new Telerik.Reporting.ObjectDataSource();
ods.DataSource = dt;
ods.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.FirstName + ' ' + Fields. LastName ")); 
Telerik.Reporting.Report rpt = new Telerik.Reporting.Report();
rpt.DataSource = objectDataSource;
Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
reportSource.ReportDocument = rpt;
reportViewer1.ReportSource = reportSource;
reportViewer1.RefreshReport();





Monday, September 28, 2015

Choose Visual Basic Example

VB.NET Functions > Choose

Returns a member of the list passed in Choice().
It is based on the value of Index. The first member of the list is selected when Index is 1.

Example

Dim choice = Choose(2, "val1", "val2", "val2") // "val2"





Thursday, September 24, 2015

Appends text to a file C#

C# > Files > File Class > AppendText

Appends text to an existing file or create a new file if the specified file does not exist.

Example

using(System.IO.StreamWriter sw = System.IO.File.AppendText(@"c:\log.txt"))
{
      sw.WriteLine("logline");
}





Tuesday, September 15, 2015

Convert to double TimeSpan C#

C# > System > TimeSpan Structure

TimeSpan represents a time structure.

Examples

1. Convert to double
   
  TimeSpan ts = TimeSpan.FromMinutes(12.45);

  double time = ts.TotalMinutes;





Friday, August 14, 2015

SOUNDEX SQL Server Example

SQL Server > Built-in Functions > SOUNDEX

SOUNDEX converts an alphanumeric string to a four-character code that is based on how the string sounds when spoken. 

Example


SELECT SOUNDEX ('Jan'), SOUNDEX ('Jane');

Result:
(No column name) (No column name)
J500                 J500






TRY_CAST SQL Server Example

SQL Server > Built-in Functions > TRY_CAST

Try to cast a value to the specified data type. 
If the cast not succeeds returns null.

Example


SELECT
    CASE WHEN TRY_CAST('1' AS float) IS NULL
    THEN 'Cast failed'
    ELSE 'Cast succeeded'
END AS Result;

Result
Cast succeeded

SELECT
    CASE WHEN TRY_CAST('xx' AS float) IS NULL
    THEN 'Cast failed'
    ELSE 'Cast succeeded'

END AS Result;

Result
Cast failed





Monday, August 10, 2015

Pow C# Example

C#  > System > Math class > Pow

Pow  number raise a  number to the specified power.

Example:

double pow = Math.Pow(2, 8); // 256




Tuesday, August 4, 2015

Scroll to selected index DataGridView

C# > DataGridViewFirstDisplayedScrollingRowIndex 

Setting this property raises scroll event to match selected index.


Example


dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;




Friday, July 31, 2015

Read fast text file line by line C#

C# > Files > File Class > OpenText

Open text file for reading line by line.

Example

Read fast text file line by line C#

using (StreamReader sr = File.OpenText(@"c:\filename.txt"))
{
   while (!sr.EndOfStream)
   {
     sr.ReadLine();
    }
}







Monday, July 27, 2015

INSERT SQL Server Example

SQL Server > DML > INSERT

INSERT adds one or more rows to a table or a view.


  • When insert data into a view, the view must reference exactly one base table in the FROM clause of the view. 
  • When insert into a multi-table view you must use a column list that references only columns from one base table. 


Example


INSERT INTO Sales (ProductName, Price, [Date])
VALUES ('Product1', 200, GETDATE());




Friday, July 24, 2015

How to bind to a parent DataContext WPF

WPF > RelativeSource



IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.IsAPGCSourceFitted}"

Monday, July 20, 2015

Day VB.Net Example

VB.NET > Functions > Day

Day returns the day of the month as an integer between 1 and 31.

Example


Dim today As Integer = Microsoft.VisualBasic.DateAndTime.Day(Now)






Wednesday, June 24, 2015

C# Regex to match the words with dot

C#Text > Regular Expressions > Split with dot


 string[] substrings = Regex.Split("test.regex.dot", @"\.");




Thursday, June 18, 2015

Remove border Telerik RadGridView

Telerik > RadGridView > Remove border

radGridView1.GridViewElement.DrawBorder = false;
radGridView1.GridViewElement.GroupPanelElement.DrawBorder = false;






Monday, June 15, 2015

KeyNotFoundException Collection C# Example

Collections > Generic > KeyNotFoundException 

This is thrown when try to retrieve an element from a collection using a key that does not exist in that collection.

Example

   Dictionary<string, string> dict = new Dictionary<string, string>();
   dict.Add("key1","value1");
   try
   {
       string val = dict["key2"];
   }
   catch (KeyNotFoundException)
   {
       throw new KeyNotFoundException("KeyNotFoundException!");

   }





Wednesday, May 20, 2015

Print PictureBox Image C# Example Using PrintDocument

C# > Print  > PrintDocument

PrintDocument sends out to the printer.


Example

Print image



using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(pictureBox1.Image, 100, 100, 700, 600);
        }
    }
}





Tuesday, May 19, 2015

System tables SQL Server

SQL Server > System tables








Monday, May 18, 2015

Subtract SQL Server example

SQL Server > Operators > Subtract

- Operator (minus) subtracts two numbers or a number (in days) from date.





Example

1. Subtracts values from 2 numeric columns

create table #test(value int, fee int)
insert into #test(value, fee) values (500,50),(100,10)
  select 
   value - fee as 'diff'
  from 
   #test
drop table #test

Result
diff
450
90

2. Subtracts from date

SELECT getdate() - 1 AS 'Subtract Date';





C# unit test using Moq verify property has been set

C# > Unit test > Moq > Verify property has been set

SetupSet : Specifies a setup on the mocked type for a call to a property setter.
VerifySet : Verifies that a property has been set on the mock, regardless of its value.

Example

using System;

namespace UnitTestProject1
{
    public interface Interface1
    {
        int Id { get; set; }
    }
}

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mocker = new Mock<Interface1>();
            mocker.SetupSet(x => x.Id = 1);
            mocker.Object.Id = 1;
            mocker.VerifySet(x => x.Id);
        }
    }
}