Sunday, August 20, 2017

Difference between SingleOrDefault and FirstOrDefault in C# Linq

In this tutorial we will see the difference between SingleOrDefault () and FirstOrDefault ().

SingleOrDefault():-
When you are exactly sure that there is only 1 result/record you should use SingleOrDefault(). If there is no result/record returned or there is more than 1 result/record this function will throw exception.
Example:-
string[] fruits = { "orange" };

string singleFruit = fruits.SingleOrDefault();

Console.WriteLine(singleFruit);

/*
 This code produces the following output:

 orange
*/


FirstOrDefault():- It will not throw any exception in case when there is no result/record returned from database rather it returns null when there is no result. It is slightly better in performance than SingleOrDefault()that is why it is recommended to use when you need single value from database.
Example:-

int[] integers = { };
int first = integers.FirstOrDefault();
Console.WriteLine(first);

/*
 This code produces the following output:

 0
*/

Wednesday, April 26, 2017

social profiles with schema.org markup on google


Google uses schema.org structured data to highlight the social profiles of a website(organization) or a person on their knowledge Panel. it is a good way as per SEO experts to show an organization company profile in google search knowledge base by using schema.org.
Whenever you will perform a search on Google to check any brand, you will be given that brand's information on Google right side of search. This google knowledge base box will give you an overlook about company CEO name, logo, stock exchange, contact information and social profile, which is now a days very important for any brand to compete in the market.
We can also add for our organization these information by using schema.org structured data to increase our SEO rank and our website traffic.
Now google is supporting a limited range of social media like, Facebook, Twitter, Google+, Instagram, YouTube, LinkedIn, Myspace, Pinterest, SoundCloud and Tumblr. we can use other social profiles also but google does not included that in search results. We can use two way one is JSON-LD and other is microdata.
Here is the example of JSON-LD:
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "name": "your name",
  "url": "http://www.your-site.com",
  "sameAs": [
    "http://www.facebook.com/your-profile",
    "http://instagram.com/yourProfile",
    "http://www.linkedin.com/in/yourprofile",
    "http://plus.google.com/your_profile"
  ]
}
</script>

If you want to set for your organization just need to change type to "Organization". you need to add this json-ld part in the head section of your website.
Here is the example for microdata:

<span itemscope itemtype="http://schema.org/Organization">
  <link itemprop="url" href="https://your-company.com">
  <a itemprop="sameAs" href="https://www.facebook.com/your-company">FB</a>
  <a itemprop="sameAs" href="https://www.twitter.com/YourCompany">Twitter</a></span>

you can change Organization to person as per your requirements. once you are finished with coding and you updated on your website then check with view source code its showing their perfectly and then you can check your markup with google structured data from here:
https://search.google.com/structured-data/testing-tool
you can enter directly your page url or paste that specific part of code in testing tool it will show you its working fine or not, if there is any error it will show there also.

Wednesday, April 19, 2017

Constraints in SQL Database

Today we will learn what are constraints in SQL. in our daily routine work we are using many constraints but in actual we don't know what are constrains. basically constraints are rules, such rules which are enforced on data column of a table. we can apply a constraint on a table or a column. these are used to check the accuracy of data and reliability of data in any circumstances.
So, let's have a look on constraints one by one which are mostly used in SQL.
NOT NULL Constraint − will make sure this column cannot be NULL.
For example we have a PRODUCTS table and we want to add NOT NULL constraint on PRICE Column:
ALTER TABLE PRODUCTS 
       MODIFY PRICE DECIMAL (18, 2) NOT NULL;


DEFAULT Constraint − it will set Default value for a column if none is given.
how to add Default Constraints:
ALTER TABLE PRODUCTS 
        MODIFY PRICE DECIMAL (18, 2) DEFAULT 10.00; 

To DROP a default Constraint we will use this query other than normal DROP constraint query:
ALTER TABLE PRODUCTS 
   ALTER COLUMN PRICE DROP DEFAULT;


UNIQUE Constraint − this will check Column Values are unique.
How to Add Unique Constraint:
ALTER TABLE PRODUCTS 
   MODIFY QTY INT NOT NULL UNIQUE;


PRIMARY Key − it will set unique identity for each row/record.
How to add:
ALTER TABLE PRODUCTS 
   ADD CONSTRAINT PK_PRODTID PRIMARY KEY (PROD_ID, PRODUCT_NAME);


FOREIGN Key − it will also Uniquely identifies a row/record for any given database table.
How to add:
ALTER TABLE ORDERS 
   ADD FOREIGN KEY ((PRODUCT_ID) REFERENCES PRODUCTS (PROD_ID);


CHECK Constraint − it used to check that all values of column are according to a specific condition.
How to Add:
ALTER TABLE PRODUCTS
   MODIFY QTY INT NOT NULL CHECK (QTY >= 2);


INDEX − Basically Indexes are used to retreive data from a table quickly.
How to Create Index:
CREATE INDEX idx_price
   ON PRODUCTS (PRICE);


Constraints can also be added while creating a table with CREATE TABLE Statement otherwise you can set later with ALTER TABLE statement.

Dropping Constraints:
We can also remove a constraint by using below query:
For example, to drop the primary key constraint in the PRODUCTS table, you can use the following command.
ALTER TABLE PRODUCTS DROP CONSTRAINT PK_PRODTID;

Comments and Suggestions are always Welcome!

Tuesday, April 18, 2017

Tracing in asp .net Application for Beginners

Today we will learn how to use Tracing in Asp.Net to check the performance of Web Application.
In Web Development the most concerned part is Performance. so how we can check which part of our application is slower either its Asp.Net code or our Database queries. you can check your SQL queries performance by using SQL Profiler, look the execution plan which part of query is taking more time than normal and then you can easily find alternative or some good way to resolve query slowness.
But if query is performing fast in database then it means there is something in our application which is making causing slowness. In production environment its not easy to check which line of code is making problem so by using Tracing we can easily check where is the problem actually. We can turn on Tracing by setting page directive Trace="true". once its set on page then we can check at the end of page or in trace.axd file. from trace log in the file we can easily check which piece of code is making problem.
Let's have look on code:
First in page directive you need to set:
<%@ Page Language="C#" AutoEventWireup="true" Trace="true" CodeFile="WebPage.aspx.cs" Inherits="WebPage" %>
Now we will create three Gridviews to check each gridview data load performance on Page load.
<div>
            <b>All Orders</b>
            <asp:GridView ID="GridView1" autogeneratecolumns="True" runat="server"></asp:GridView>    
            <br />
            <b>Delivered Order</b>
            <asp:GridView ID="GridView2" autogeneratecolumns="True" runat="server"></asp:GridView>    
            <br />
            <b>Cancelled Order</b>
            <asp:GridView ID="GridView3" autogeneratecolumns="True" runat="server"></asp:GridView>    
        </div>


And In code behind we will call our Stored Procedure on Page load, here i am not going to write full code for calling stored procedures, we will just understand how Tracing will work:
protected void Page_Load(object sender, EventArgs e)
    {
        Trace.Warn("GetAllOrders() Stored procedure started");
        GetAllOrder();
        Trace.Warn("GetAllOrders() Stored procedure End");

        Trace.Warn("GetDeliveredOrder() Stored procedure started");
        GetDeliveredOrder();
        Trace.Warn("GetDeliveredOrder() Stored procedure End");

        Trace.Warn("GetCancelledOrder() Stored procedure started");
        GetCancelledOrder();
        Trace.Warn("GetCancelledOrder() Stored procedure End");

    }


Above we discussed it was page level Tracing, We can also set Tracing on application level by using below web.config code. it save trace data for up to 40 requests.
<configuration>
  <system.web>
    <trace enabled="true" requestLimit="40" localOnly="false" />
  </system.web>
</configuration>


Comments and Suggestions are always Welcome!

Sunday, April 16, 2017

Insert Data from one Database Table to another Database Table on Different Server

Today we are going to learn how to insert data from one database server table to another database server table.
So let’s start writing the query.

Insert into [Server/Machine Name].[Database Name].[Schema].[Table Name]
Select * from [Server/Machine Name]. [Database Name].[Schema].[Table Name]

If you want to pick specific columns then you will write this query like this

Insert into [Server/Machine Name]. [Database Name].[Schema].[Table Name](Column1,Column2)
Select Column1, Column2 from [Server/Machine Name]. [Database Name].[Schema].[Table Name]

Comments and Suggestions are Always Welcome!