Search This Blog

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





    Tuesday, September 30, 2014

    SqlClient namespace

    C# > System.Data  > SqlClient

    SqlClient namespace is the.NET Framework Data Provider for SQL Server.





    SqlBulkCopy C# SQL Server import data example

    C# > System.Data  > SqlClient > SqlBulkCopy

    SqlBulkCopy efficiently bulk load a SQL Server table with data from another source.
    The data source is not limited to SQL Server, any data source can be used.
    SqlBulkCopy offers a significant performance.






    Example:

    string sourceConnectionString = "YourSourceConnectionString";
    string destinationConnectionString = "YourDestinationConnectionString";

    using (SqlConnection sourceConnection =
           new SqlConnection(sourceConnectionString))
                {
                    sourceConnection.Open();
                    SqlCommand commandSourceData = new SqlCommand(
                        "SELECT * FROM source_table;", sourceConnection);
                    SqlDataReader reader = commandSourceData.ExecuteReader();
                    using (SqlConnection destinationConnection =
                               new SqlConnection(destinationConnectionString))
                    {
                        destinationConnection.Open();
                        using (SqlBulkCopy bulkCopy =
                                   new SqlBulkCopy(destinationConnection))
                        {
                            bulkCopy.DestinationTableName = "dbo.destination_table";
                            try
                            {
                                bulkCopy.WriteToServer(reader);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                            finally
                            {
                                reader.Close();
                            }
                        }
                    }

                }




    Monday, September 29, 2014

    GetDrives C# Example

    C# > Files > DriverInfoGetDrives 

    Retrieves the drive names of all logical drives

    Example:
    DriveInfo[] allDrives = DriveInfo.GetDrives();

    foreach (DriveInfo d in allDrives)
    {
       tvLeft.Nodes.Add(d.Name);  
    }





    Thursday, September 25, 2014

    RichTextBox control insert color text

    C# > RichTextBox Control > SelectionColor 


    Gets or sets the text color of the current text selection or insertion point.

    Example

    private void AppendText(RichTextBox rtb, string text, Color color)
    {
           rtb.SelectionStart = rtb.TextLength;
           rtb.SelectionLength = 0;

           rtb.SelectionColor = color;
           rtb.AppendText(text);
           rtb.SelectionColor = rtb.ForeColor;
    }

    AppendText(richTextBox1, "Blue", Color.Blue);

    AppendText(richTextBox1, "Red", Color.Red);   






    Wednesday, September 24, 2014

    Telerik RadGridView clear all rows

    Telerik > RadGridView > Rows > Clear
    
    
    Clear  is a efficient solution to remove all rows since the grid's events will be suspended.
    
    
    Example
    this.radGridView1.Rows.Clear();






    Bach file Current Directory Example

    Bach file > Current directory 

    set CURRENT_DIR=%CD%
    cd ..\..\install\kits
    install.bat
    cd %CURRENT_DIR%





    Tuesday, September 23, 2014

    Configure Jenkins to use Mercurial

    Jenkins > Mercurial 

    Mercurial Plugin: Install the Jenkins Mercurial Plugin first (read more https://wiki.jenkins-ci.org/display/JENKINS/Mercurial+Plugin)



    Source Code Management: Configure for Mercurial













    Reduce unnecessary builds by specifying a comma or space delimited list of "modules" within the repository. A module is a directory name within the repository that this project lives.




    To do that press advanced button:

    This will detect changes only in sdks subdirectory in src directory:


    Wednesday, September 17, 2014

    Environment UserName property C# example

    C#  > System > Environment ClassUserName

    UserName is used to identify the user on the current thread, the user name of the person who is logged on to Windows.

    Example



      string user = Environment.UserName;