Search This Blog

Friday, October 24, 2014

Telerik Reporting Binding C# Example

Telerik > Reporting > ObjectDataSource

ObjectDataSource component represents the middle-tier object and provides data retrieval capabilities. 
Example


var ods = new Telerik.Reporting.ObjectDataSource();
ods.DataSource = GetData();
ods.DataMember = "Products"; //table to bind to        
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = ods;
Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
reportSource.ReportDocument = report;
reportViewer1.ReportSource = reportSource;
reportViewer1.RefreshReport();





Thursday, October 23, 2014

Get Computer Name C#

C#  > System > Environment ClassMachineName

Returns the computer's name. This is established at system start up when the name is read from the registry. 

Example



   string _name = Environment.MachineName;





ADO.NET LINQ to SQL

ADO.NET LINQ to SQL

Is a component for managing relational data as objects. 

  • the data model of a relational database is mapped to an object model  
  • translates into SQL the language-integrated queries in the object model and sends them to the database for execution
  • translates database result  back to objects 
Methods





Tuesday, October 21, 2014

VB.NET LINQ TO SQL Delete Rows From Database

ADO.NET > LINQ TO SQL > Delete rows from database

DeleteOnSubmit removes entity, but does not disappear from the query results until after SubmitChanges is called. 

Example


C#

var deleteData =
    from details in db.GetDetails
    where details.ID == 1
    select details;

foreach (var detail in deleteData )
{
    db.Details.DeleteOnSubmit(detail);
}

try
{
    db.SubmitChanges();
}
catch (Exception e)
{
}



VB.Net

Dim deleteData = _
    From details In db.GetDetails() _
    Where details.ID = 1 _
    Select details

For Each detail As Detail In deleteData
    db.Details.DeleteOnSubmit(detail)
Next 

Try
    db.SubmitChanges()
Catch ex As Exception

End Try





Friday, October 17, 2014

Check Deny Access File C#

C# > System.Security > AccessControlAuthorizationRuleCollection 

AuthorizationRuleCollection 

Represents a collection of AuthorizationRule objects.

Example


Check Deny Access File C#

public bool CheckDenyAccessFile(string filePath)
{
       FileSecurity fs = File.GetAccessControl(filePath);
       AuthorizationRuleCollection acl = fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
       bool deny = false;
       for (int x = 0; x < acl.Count; x++)
       {
              FileSystemAccessRule currentRule = (FileSystemAccessRule)acl[x];
              AccessControlType accessType = currentRule.AccessControlType;
              if (accessType == AccessControlType.Deny && (currentRule.FileSystemRights & FileSystemRights.ListDirectory) == FileSystemRights.ListDirectory)
                {
                    deny = true;
                    break;
                }
        }
        return deny;
}





Writes lines to text file C#

C# > IO  > FileInfo > CreateText

Writes text a new text file.

Example


Writes lines to text file C#


string path = Path.GetTempFileName();
FileInfo fi = new FileInfo(path);

using (StreamWriter sw = fi.CreateText())
{
      sw.WriteLine("Line1");
      sw.WriteLine("Line2");
      sw.WriteLine("Line3");
      sw.WriteLine("Line4");


}      





FileInfo Class C#

C# > IO  > FileInfo Class

FileInfo

Use this class for working with files: create, open, delete, 

move, rename.

Methods

Properties






Thursday, October 16, 2014

String IsNullOrEmpty C# Example

C# > String > Methods > IsNullOrEmpty

With this method you can test simultaneously test whether a String is null or its value is Empty.

Example


string s = "test";
bool b = String.IsNullOrEmpty(s); // false
           
s = null;
b = String.IsNullOrEmpty(s); // true
           
s = "";
b = String.IsNullOrEmpty(s); // true

s = " ";

b = String.IsNullOrEmpty(s); // false








Wednesday, October 15, 2014

CONVERT SQL Server Example

SQL Server > Built-in Functions > CONVERT

CONVERT converts an expression of one data type to another.
Example

select CONVERT(nvarchar(30), GETDATE(), 100)

result
Oct 15 2014  9:54AM

Other examples:






Where keyword generic generic type constraint example C#

C# > Generics > where - generic type constraint

With where keyword you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates the generics class.


Example


    public class A
    {
    }
    public class B:A
    {
    }
    public class GenericList <T> where T : A
    {
    }

    GenericList<A> lst1 = new GenericList<A>(); // OK
    GenericList<B> lst2 = new GenericList<B>(); // OK B derives from A
    GenericList<int> lst3 = new GenericList<int>(); //Error  1      The type 'int' cannot be used as type parameter 'T' in the generic type or method 'WindowsFormsApplication1.GenericList'. There is no boxing conversion from 'int' to 'WindowsFormsApplication1.A'.
       






Tuesday, October 14, 2014

Ushort Type C# Example

C# > Types > ushort

The ushort keyword is an integral type.
Range: 0 to 63535


Example:

byte k = 63535; // ok
k = 63536;      // Error   




Byte Type C# Example

C# > Types > byte

The byte keyword is an integral type.
Range: 0 to 255


Example:

byte k = 255; // ok
k = 256;      // Error   




Wednesday, October 8, 2014

APP_NAME SQL Server Example

SQL Server > Built-in FunctionsAPP_NAME

APP_NAME returns the application name for the current session.

Example:

select APP_NAME();
Result:
Microsoft SQL Server Data Tools, T-SQL Editor



Create form in runtime C#

C# > Windows > Form

A Form is a representation of any window displayed in your application.

Properties
Example

Create form in runtime

Form form1 = new Form();
Button button1 = new Button();
button1.Text = "OK";
button1.Location = new Point(5, 5);
form1.Text = "My Form";
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
form1.MaximizeBox = false;
form1.MinimizeBox = false;
form1.StartPosition = FormStartPosition.CenterScreen;
form1.Controls.Add(button1);

form1.ShowDialog();




Jenkins Email Error: Network is unreachable

Jenkins > Email > Network is unreachable error

send failed, exception: javax.mail.MessagingException: Could not connect to SMTP host:     , port: 25;
nested exception is:  java.net.SocketException: Network is unreachable: connect


Solution:

The problem is because java 1.7 uses IPv6 by default.
To fix this add Java Option from command line:

setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true**




Tuesday, October 7, 2014

Set Next Build Number Jenkins

Jenkins > Next Build Number

Install plugin Next Build Number

https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin

After install you have to reload configuration from disk


Go to configure project and click on Set Next Build Number





Friday, October 3, 2014

ProcessStartInfo Example - Start Microsoft Word

C# > DiagnosticsProcessStartInfo

ProcessStartInfo specifies a set of values that are used when you start a process.

Example 

Start word

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
Process.Start(startInfo);






Process Class C#

C# > Diagnostics > Process Class

A process is a running application.
Process class provide access to a process that is running on a computer. 
The Process component is a useful tool for starting, stopping, controlling, and monitoring apps.

Methods:






System.Diagnostics Namespace C#

C# > System.Diagnostics

System.Diagnostics contains classes that allow to interact with 

  • system processes
  • event logs
  • performance counters
Classes