Monday, July 4, 2016

Bootstrap's JavaScript requires jQuery version 1.9.1 or higher

Change the order of files it should be like below..
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>

<script src="js/wow.min.js"></script>

Wednesday, June 29, 2016

How to get my project path?

private void btn_upload_Click(object sender, EventArgs e)
        {
            OpenFileDialog op1 = new OpenFileDialog();
            op1.Multiselect = true;
            op1.ShowDialog();
            op1.Filter = "allfiles|*.xls";
            textBox1.Text = op1.FileName;
            int count = 0;
            string[] FName;
            foreach (string s in op1.FileNames)
            {
                //string startupPath1 = System.IO.Directory.GetCurrentDirectory();
                //string startupPath = Environment.CurrentDirectory;
                string wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
                FName = s.Split('\\');
                File.Copy(s, wanted_path + "\\Upload\\ " + FName[FName.Length - 1]);
                count++;
            }
            MessageBox.Show(Convert.ToString(count) + " File(s) copied");

        }

Tuesday, June 28, 2016

How can I check ModelState.IsValid from inside my Razor view

@if (!ViewData.ModelState.IsValid)
{
    <div>There are some errors</div>
<script>fun()<script>
}
function fun()
{
}

Sunday, June 26, 2016

InnerException = {"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}

This is the case if you have a date time set to 0001-01-01 because it’s out of range for Sql Server (even with Sql Server 2008 R2). In fact, Sql Server date can have date only after the year 1753.

The solution to this problem is to set a property date or to make nullable this field in the database and into your entity. This ways, you could set to null if the date is not set. Otherwise, you can use instead of of 0001-01-01 an other date has a “not set date” which is over 1753-01-01.

Wednesday, June 22, 2016

What's the difference between event.stopPropagation and event.preventDefault?

stopPropagation stops the event from bubbling up the event chain.
preventDefault prevents the default action the browser makes on that event.
Let's say you have
<div id="foo">
 <button id="but" />
</div>

$("#foo").click(function() {
   // mouse click on div
});

$("#but").click(function(ev) {
   // mouse click on button
   ev.stopPropagation();
});

jQuery limit to 2 decimal places

var amount = $("#textid").slider("value") * 1.60;
$("#textboxid2").val('$' + amount.toFixed(2));

Monday, June 20, 2016

Default return type of LINQ Query

If you are querying a database(Linq to SQL) then the return type is IQueryable<T> where T is Product in below example
var product = from p in dbContext.Products
             select p;


If you are using Linq againist a datasource which is an Enumerable collection(List) then the return type will be IEnumerable<t>
------------------------------------------
it depends on your original data source. It can be either IEnumerable or IQueryable 

The result of a Linq database query is typically IQueryable<T> . 

If the database query includes an OrderBy clause, the type is IOrderedQueryable<T> .It is derive from IQueryable<T> 

If the data source is an IEnumerable, the result type is IEnumerable<T> 

You can find out the datatype by calling .GetType() on the source. 

Generate Random Password

Here, we will see how to generate random passwords in ASP.NET. Random numbers may be generated in the .NET Framework using the Random class. You create an instance to an object of Random class and generate random numbers. You have to create a web site and add a new page to the website. Drag and drop two TextBox and one Button control on the form. One TextBox to give the length of the TextBox, another to display the random password of the specified length when we click on the Button control. Let's take a look at a practical example.
First you have to create a web site.
  • Go to Visual Studio 2010
  • New-> Select a website application
  • Click OK

Now add a new page to the website.
  • Go to the Solution Explorer
  • Right Click on the Project name
  • Select add new item
  • Add new web page and give it a name
  • Click OK

Now create a new website and drag and drop two TextBox controls onto the aspx page. The TextBoxcode looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="randomnumber.aspx.cs"Inherits="randomnumber" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   <div>
       Password Length: &nbsp;&nbsp; <asp:TextBox ID="txtPassLength" runat="server"></asp:TextBox>
        <br />
        Random Password: <asp:TextBox ID="txtpassword" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Generate Password" />
        <br />
    </div>
    </form>
</body>
</html>
In ASP.Net you can use a Random class to generate the random numbers. Create an instance to an object of the Random class and generate random numbers. This class may be instantiated using the following code:
 Random rand = new Random();
In the above rand is an object of the Random Class.
Now double-click on the Button control and add the following code:
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class randomnumber : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string allowedChars = "";
        allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";
        allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";
        allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";
        char[] sep = { ',' };
        string[] arr = allowedChars.Split(sep);
        string passwordString = "";
        string temp = "";
        Random rand = new Random();
        for (int i = 0; i < Convert.ToInt32(txtPassLength.Text); i++)
        {
            temp = arr[rand.Next(0, arr.Length)];
            passwordString += temp;
        }
        txtpassword.Text = passwordString;
    }
}

Now run the application and test it.

Now give the length of the password.

Now click on the Button to generate a random password.


The above output displays a random password.

how to get last inserted id in C#

SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

You can use SqlCommand.ExecuteScalar to execute the insert command and retrieve the new ID in one query.

using (var con = new SqlConnection(ConnectionString))
                {
                    int newID;
                    var cmd = "INSERT INTO foo (column_name)VALUES (@Value);SELECT CAST(scope_identity() AS int)";
                    using (var insertCommand = new SqlCommand(cmd, con))
                    {
                        insertCommand.Parameters.AddWithValue("@Value", "bar");
                        con.Open();
                        newID = (int)insertCommand.ExecuteScalar();
                    }

                }

Difference between View and PartialView in Asp.net MVC

I have seen a lots of debates and discussions on Difference between View and PartialView in Asp.net MVC. Both are used to return Html but main difference is that View return whole page including Layout while PartialView return only piece of html content. Beside all these points, I made some remarks on difference between view and partialview and some interesting analysis based on return type of view and partialview are described here.
  •  Key Difference between view and partialview in Asp.net mvc

  1. Partialview generally does not have any Layout while view content Layout.
  2. Partialview mostly does not content html attribute like head , tag , meta and body while view can have all this attribute
  3. Partialview is reusable content which is render inside view(parent page)
  4. Partialview mostly used with Ajax request to process fast and update particular portion while view load whole page.
  5. Partialview generally process with mvc Ajax.ActionLink and Ajax.Beginform while View process with mvc Html.ActionLink and Html.Beginform
But above point are not always true for view vs partialview, as I have experienced its practically with different return types and appying layout or not. My analysis with some interesting results, while comparing view and partialview as below.
  • Return view and partialview with or without Layout Result

  1. Return View + Layout = View
  2. Return View – Layout = View (if Layout specified in _ViewState.cshtml)
  3. Return View – Layout = PartialView (if Layout not  specified in _ViewState.cshtml)
  4. Return PartialView + Layout = View
  5. Return PartialView – Layout = PartialView