Wednesday, January 18, 2017

How to bulk insert using StoreProcedure in SQL Server?

 Described in shortly................................

create a dataset named = dsName  which contain a table name tblName with field(ID,NAME,ADDRESS) in c#

// in c#

public SaveData(DataSet rqdXmlv = null)
{
----------------------------------
----------------------------------
SqlCommand cmd;
cmd = new SqlCommand(procedure, dbConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dsxmlu", SqlDbType.Xml).Value = (rqdXmlv == null ? null : rqdXmlv.GetXml());
-----------------------------------
------------------------------------
}

//in sql procedure

CREATE PROCEDURE [SP_TEST]
    @dsxmlu xml = null,



/------GET DATA FROM PARAM
        SELECT
            t.value('ID[1]','nvarchar(12)')AS CHNUM,
            t.value('NAME[1]','nvarchar(100)')AS ITMNAME,
            t.value('ADDRESS[1]','nvarchar(100)')AS UNIT
        into #tbl1a FROM
        @dsxmlu.nodes('/dsName/tblName')AS TempTable(t)


  INSERT INTO TEST(ID,NAME,ADDRESS)
  SELECT ID,NAME,ADDRESS FROM #tbl1a

Tuesday, January 10, 2017

How to print directly without showing report viewer in rdlc report ?

 //==========================

  LocalReport report = new LocalReport();
          string path = rptName;
          report.ReportPath = path;
          report.DataSources.Add(new ReportDataSource(dsName, ds.table[0]));
          report.SetParameters(parameters);
          PrintToPrinter(report);
//========================================================

  public static void PrintToPrinter(LocalReport report)
        {
            Export(report);

        }

  public static void Export(LocalReport report, bool print = true)
        {
            string deviceInfo =
             @"<DeviceInfo>
                <OutputFormat>EMF</OutputFormat>
                <PageWidth>3in</PageWidth>
                <PageHeight>8.3in</PageHeight>
                <MarginTop>0in</MarginTop>
                <MarginLeft>0.1in</MarginLeft>
                <MarginRight>0.1in</MarginRight>
                <MarginBottom>0in</MarginBottom>
            </DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            report.Render("Image", deviceInfo, CreateStream,out warnings);
            foreach (Stream stream in m_streams)
                stream.Position = 0;

            if (print)
            {
                Print();
            }
        }


public static void Print()
        {
            if (m_streams == null || m_streams.Count == 0)
                throw new Exception("Error: no stream to print.");
            PrintDocument printDoc = new PrintDocument();
            if (!printDoc.PrinterSettings.IsValid)
            {
                throw new Exception("Error: cannot find the default printer.");
            }
            else
            {
                printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                m_currentPageIndex = 0;
                printDoc.Print();
            }
        }

 public static Stream CreateStream(string name,string fileNameExtension, Encoding encoding,string mimeType, bool willSeek)
        {
            Stream stream = new MemoryStream();
            m_streams.Add(stream);
            return stream;
        }

 public static void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);

            // Adjust rectangular area with printer margins.
            Rectangle adjustedRect = new Rectangle(
                ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
                ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
                ev.PageBounds.Width,
                ev.PageBounds.Height);

            // Draw a white background for the report
            ev.Graphics.FillRectangle(Brushes.White, adjustedRect);

            // Draw the report content
            ev.Graphics.DrawImage(pageImage, adjustedRect);

            // Prepare for the next page. Make sure we haven't hit the end.
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }

public static void DisposePrint()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }

Saturday, December 24, 2016

How to populate a kendo Grid?

How Can you populate a kendo Grid?

//.............html.........

<div id="gridSummary"></div>
  
 <script src="~/Scripts/jquery-1.8.2.min.js"></script>
 <script src="~/kendo/kendoui.web/js/kendo.web.min.js"></script>
.....kendo jquery.............

$(document).ready(function () {
    SummaryHelper.GenerateGrid();
}



  
var SummaryManager={
    gridDataSource: function () {

        var gridDataSource = new kendo.data.DataSource({
            type: "json",
            serverPaging: true,

            serverSorting: true,

            serverFiltering: true,

            allowUnsort: true,

            pageSize:15,

            transport: {
                read: {
                    url: '../Employee/GetEmployeeData/',

                    type: "POST",

                    dataType: "json",

                    contentType: "application/json; charset=utf-8",
                    cache: false,
                    async: false,
                },
             
                parameterMap: function (options) {

                    return JSON.stringify(options);

                }
            },
            schema: { data: "Items", total: "TotalCount" }
        });
        return gridDataSource;
    },
};

var SummaryHelper={
GenerateGrid: function () {
        $("#gridSummary").kendoGrid({
            dataSource: SummaryManager.gridDataSource(),
            pageable: {
                refresh: true,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            xheight: 450,
            filterable: true,
            sortable: true,
            columns: SummaryHelper.Columns(),
            editable: false,
            navigatable: true,
            selectable: "row"

            //selectable: false

        });
    },

     Columns: function () {
        return columns = [
             { field: "Id", title: "Id", width: 50, hidden: true },
             { field: "Name", title: "Name", width: 80 },
             { field: "Designation", title: "Designation", width: 80 }

        ];
    },
};


   

How to use a PIVOT Query in SQL ?

Create a Table:
----------------------------------------------
 TABLE [dbo].[Salary](
    [SalaryId] [int] IDENTITY(1,1) NOT NULL,
    [EmpId] [varchar](50) NULL,
    [SalaryMonth] [date] NULL,
    [SalaryAmt] [decimal](18, 0) NULL,

//========================PIVOT Query=================================
  Select pv.* From
 (
 Select * From (
 Select EmpId,Month(SalaryMonth)Months,SalaryAmt From Salary
 ) sal
 PIVOT (SUM(SalaryAmt)for Months in([1],[2],[3]) )As p
  )pv

How to Checked and Unchecked Checkbox Using Jquery ?

Sometimes we need to work with CheckBox and we have faced many problem to manage functionality of CheckBox.
Now I Will give you an example of CheckBox functionality. I think it will be helpfull.

----------UI-------------------
Create an Input Item in html :

<div>
  <ul>
    <li>
        <label for="IsActive" class="lbl widthSize20_per">Is Active:</label>
        <input type="checkbox" id="chkIsActive" name="IsActive" />
    </li>
  </ul>
</div>

----------Javascript-------------------
How to get value from CheckBox:

var IsActive = $("#chkIsActive").is(":checked") == true ? 1 : 0;



----Fill Checkbox-------------
var data = GetDataFromServer();

 if (data.IsActive == 1) {
            $("#chkIsActive").prop('checked', 'checked');
        } else {
            $("#chkIsActive").removeProp('checked', 'checked');
        }

--------is checked event------------------
CheckBox click event:

       $('#chkIsActive').live('click', function (e) {
            var $cb = $(this);
            if ($cb.is(":checked")) {
                //do something
            } else {
               //do something
            }

        });


Clear CheckBox:  
---------------------------------------------------------
$("#chkIsActive").removeProp('checked', 'checked');
or,
$("#chkIsActive").prop('checked', false);





How to generate a Radio Button and work with jquery ?


//=========HTML================
 <form id="OperationType" style="display: inline">
        <input type="radio" id="txtInActive" name="InActive" value="1" />Make InActive
        <input type="radio" id="txtBranchSwitch" name="InActive" value="2" />Branch Switch
  </form>

//===========get Value from Control

var InActiveOrBranchSwitch = $("#OperationType input[type='radio']:checked").val();

///OperationType Change Event

    OperationTypeChangeEvent:function() {
        $("#txtBranchSwitch").change(function () {
            if ($("#txtBranchSwitch").attr("checked")) {
                $("#changedCompanyBranchDiv").show();
            }
        });
       
        $("#txtInActive").change(function () {
            if ($("#txtInActive").attr("checked")) {
                $("#changedCompanyBranchDiv").hide();
            }
        });
    }


Clear value :
.......................
  $("input:radio").attr("checked", false);