Saturday, 19 July 2014

Sql Important Task

Sql  Task 1  Database backup

DECLARE @name VARCHAR(50) -- database name 
DECLARE @path VARCHAR(256) -- path for backup files 
DECLARE @fileName VARCHAR(256) -- filename for backup 
DECLARE @fileDate VARCHAR(20) -- used for file name

SET @path = 'D:\BACK\' 

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR FOR 
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb') 

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  

WHILE @@FETCH_STATUS = 0  
BEGIN  
       SET @fileName = @path + @name + '_' + @fileDate + '.BAK' 
       BACKUP DATABASE @name TO DISK = @fileName 

       FETCH NEXT FROM db_cursor INTO @name  
END  

CLOSE db_cursor  
DEALLOCATE db_cursor

Task  2  Bulk insert
 file1.txt

"Kelly","Reynold","kelly@reynold.com"
"Kelly","Reynold","kelly@reynold.com" "John","Smith","bill@smith.com" "Sara","Parker","sara@parker.com" - See more at: http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file#sthash.K2zFx1ZA.dpuf
"Kelly","Reynold","kelly@reynold.com" "John","Smith","bill@smith.com" "Sara","Parker","sara@parker.com" - See more at: http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file#sthash.K2zFx1ZA.dpuf
 "aa","bb","cc"
"Kelly","Reynold","kelly@reynold.com"
 BULK INSERT temp1 FROM 'D:\aa.txt' WITH (FIELDTERMINATOR = '","')

file2.txt
aa,bb,cc
 BULK INSERT temp1 FROM 'D:\aa.txt' WITH (FIELDTERMINATOR =',')
FileType=1 (TxtFile1.txt)
"Kelly","Reynold","kelly@reynold.com"
"John","Smith","bill@smith.com"
"Sara","Parker","sara@parker.com"
FileType=2 (TxtFile2.txt)
Kelly,Reynold,kelly@reynold.com
John,Smith,bill@smith.com
Sara,Parker,sara@parker.com
- See more at: http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file#sthash.K2zFx1ZA.dpuf
FileType=1 (TxtFile1.txt)
"Kelly","Reynold","kelly@reynold.com"
"John","Smith","bill@smith.com"
"Sara","Parker","sara@parker.com"
FileType=2 (TxtFile2.txt)
Kelly,Reynold,kelly@reynold.com
John,Smith,bill@smith.com
Sara,Parker,sara@parker.com
- See more at: http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file#sthash.K2zFx1ZA.dpuf
FileType=1 (TxtFile1.txt)
"Kelly","Reynold","kelly@reynold.com"
"John","Smith","bill@smith.com"
"Sara","Parker","sara@parker.com"
FileType=2 (TxtFile2.txt)
Kelly,Reynold,kelly@reynold.com
John,Smith,bill@smith.com
Sara,Parker,sara@parker.com
- See more at: http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file#sthash.K2zFx1ZA.dpuf

BULK INSERT tmpStList FROM 'c:\TxtFile2.txt' WITH (FIELDTERMINATOR = ',') - See more at: http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file#sthash.K2zFx1ZA.dpuf

 

Wednesday, 9 July 2014

image priview and crop image in asp.net

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <title>jQuery Crop Image using crop plugin</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script src="jquery.Jcrop.js" type="text/javascript"></script>
    <link href="jquery.Jcrop.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript">
       
         function getcroparea(c) {
             $('#hdnx').val(c.x);
             $('#hdny').val(c.y);
             $('#hdnw').val(c.w);
             $('#hdnh').val(c.h);
         };
</script>
    
<script type="text/javascript">
    function showimagepreview(input) {
        $("#imgprvw").show();
        if (input.files && input.files[0]) {
          
            var filerdr = new FileReader();
            filerdr.onload = function (e) {

                $('#imgprvw').attr('src', e.target.result);
                $('#imgprvw').Jcrop({
                    onSelect: getcroparea
                });

            }
            filerdr.readAsDataURL(input.files[0]);
            $("#imgprvw").hide();
          

        }
    }
</script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload runat="server" ID="filUpload1" onchange="showimagepreview(this)" />
    <asp:Image ID="imgprvw" runat="server" src="" />
   
    </div>

    <div>
<input type="hidden" id="hdnx" runat="server" />
<input type="hidden" id="hdny" runat="server"/>
<input type="hidden" id="hdnw" runat="server"/>
<input type="hidden" id="hdnh" runat="server" />
<asp:Button ID="btncrop" runat="server" OnClick="btncrop_Click" Text="Crop Images" />
<img id="imgcropped" runat="server" visible="false" />
</div>
    </form>
</body>
</html>
==================================================================
using System;
using System.Drawing;
using System.IO;
using Image = System.Drawing.Image;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btncrop_Click(object sender, EventArgs e)
    {
        try
        {
            string filename = Path.GetFileName(filUpload1.PostedFile.FileName);
            string fname = filename;
            string fpath = Path.Combine(Server.MapPath("~/images"), fname);
            filUpload1.SaveAs(Server.MapPath("images/" + filename));
               Image oimg = Image.FromFile(fpath);
               
                Rectangle cropcords = new Rectangle(
                Convert.ToInt32(hdnx.Value),
                Convert.ToInt32(hdny.Value),
                Convert.ToInt32(hdnw.Value),
                Convert.ToInt32(hdnh.Value));
                string cfname, cfpath;
                Bitmap bitMap = new Bitmap(cropcords.Width, cropcords.Height, oimg.PixelFormat);
                Graphics grph = Graphics.FromImage(bitMap);
                grph.DrawImage(oimg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), cropcords, GraphicsUnit.Pixel);
                cfname = "crop_" + fname;
                cfpath = Path.Combine(Server.MapPath("~/cropimages"), cfname);
                bitMap.Save(cfpath);
                imgcropped.Visible = true;
                imgcropped.Src = "~/cropimages/" + cfname;
            
            }
            catch (Exception ex)
            {
                throw ex;
            }
    }
}
================================================
you need to download  jscrop js
thanks



using + operator in c# & javasript for concatenation example


            string strOut;
            strOut = "<table width='300' border='1' cellspacing='0' cellpadding='3'>";

            strOut = strOut + "<tr>";

            strOut = strOut + "<th colspan='2'>The + operator in JavaScript</th>";
            strOut = strOut + "</tr>";

            strOut = strOut + "<tr>";
            strOut = strOut + "<th>When used in</th>";
            strOut = strOut + "<th>Performs</th>";
            strOut = strOut + "</tr>";
            strOut = strOut + "<tr>";
            strOut = strOut + "<td>"+101+"</td>";
            strOut = strOut + "<td>"+301+"</td>";
            strOut = strOut + "</tr>";
            strOut = strOut + "<tr>";
            strOut = strOut + "<td>" + 401 + "</td>";
            strOut = strOut + "<td>" + 501 + "</td>";
            strOut = strOut + "</tr>";
            strOut = strOut + "</table>";
  Response.write(strout); in c#
Document.write(strout); in javascript

Sunday, 6 July 2014

auto complte in mvc

 Home Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Autocomplete.Models;
using newauto.Models;
using newauto.Controllers;

namespace Autocomplete.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        CountryEntities _entites = new CountryEntities();
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Autocomplete(string term)
        {
            var data = (from x in _entites.countries
                        where x.CountryName.Contains(term)
                        select new City
                        {
                            Key = x.pkCountryId,
                            Value = x.CountryName
                        }).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetDetail(int id)
        {
            DemoModel model = new DemoModel();
           // select data by id here display static data;
            if (id == 0)
            {
                model.id = 1;
                model.name = "Yogesh Tyagi";
                model.mobile = "9460516787";

            }
            else {
                model.id = 2;
                model.name = "Pratham Tyagi";
                model.mobile = "9460516787";
          
            }

            return Json(model);
          
        }

    }
}

=================================================================
view

@{
    ViewBag.Title = "Index";
}



<script src="../../Scripts/jquery-ui-1.8.20.js" type="text/javascript"></script>

<h2>Index</h2>
  @Html.Label("Enter Your name")
                @Html.TextBox("PassId")
<div id="VisitorDetail">
                    <label>Id</label><div id="Id"></div>
                    <label>Name</label><div id="Name"></div>
                    <label>Mobile</label><div id="Mobile"></div>
  </div>
<script type="text/javascript">
    $(document).ready(function () {
        $('#VisitorDetail').hide();
    });
    $("#PassId").autocomplete({
        source: function (request, response) {
            var customer = new Array();
            $.ajax({
                async: false,
                cache: false,
                type: "POST",
                url: "@(Url.Action("Autocomplete", "Home"))",
                data: { "term": request.term },
                success: function (data)
                {
                if(data.length==0)
                {
                    $('#VisitorDetail').hide();
                alert('No Records Found');
                }
                else
                {
                 $('#VisitorDetail').show();
                alert('No Records Found'+'1');
                    for (var i = 0; i < data.length ; i++)
                     {
                
                        customer[i] = { label: data[i].Value, Id: data[i].Key };
                      }
                    }
                }
            });
            response(customer);
        },
         select: function (event, ui) {
             //fill selected customer details on form
             $.ajax({
                 cache: false,
                 async: false,
                 type: "POST",
                 url: "@(Url.Action("GetDetail", "Home"))",
                data: { "id": ui.item.Id },

                success: function (data) {
                    $('#VisitorDetail').show();
                    $("#Id").html(data.id)
                    $("#Name").html(data.name)
                    $("#Mobile").html(data.mobile)
                    action = data.Action;
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert('Failed to retrieve states.');
                }
            });
        }
     });

</script>=================================================================