Friday, 26 March 2021

Call API using ajax in asp.net bearer token bassed

//////////////////////////// Call Token Authontication

function CallAPI_Authoticate(objvr) {

                              var objvr = {
                                    grant_type: 'password',
                                    username: $("[id$=UserName]").val(),
                                    password: $("[id$=Password]").val(),
                                    scope: $('[id$=AnyOtherParameter]').val()
                                };

                        $.ajax({
                            type: "POST",
                            url: "BsaeURL/"+"Token",
                            headers: { "Accept": "application/json" },
                            contentType: "application/x-www-form-url; charset=urf-8",
                            data: objvr,
                            async: false,
                            success: function (result) {                              
                                if (result != "Error")
                                    CallAPI_Authorized(result);
                            },
                            error: function (req, status, error) {
                            
                                    Show_Alert_Msg(JSON.parse(req.responseText).error_description);                            
                            }
                        });
                    }


//////////////////////////// After Authontication API data comming in JSON

function CallAPI_Authorized(token) {                      
                        var tokes = token.access_token;
                        var Baseulr= "http://localhost:60005"
                        var APIURL= "/api/data/GetEmployees";
                        $.ajax({
                            type: "GET",
                            url: "BaseUrl" + "APIURL",
                            async: false,
                            headers: {
                                Authorization: 'Bearer ' + tokes
                            },
                            dataType: 'json',
                            success: function (result, status, xhr) {
                              
                                Other working code here....
                            },
                            error: function (req, status, error) {
                                Show_Alert_Msg(JSON.parse(req.responseText).error_description);
                            }
                        });
                    }

////////////////////////////

[{"districtName":"Haveri","districtCode":"11","talukName":"Haveri","talukCode":"1104","hobliName":"HAVERI","hobliCode":"110401","villageName":"Gowrapura.M.Adura","villageCode":"1104010013","message":"Data Available"}]

Friday, 19 March 2021

Return a JSON string explicitly from Asp.net WEBAPI? or api return string as json in asp net web

 /////// 

There are a few alternatives. The simplest one is to have your method return a HttpResponseMessage, and create that response with a StringContent based on your string


Normal Code >>

        public HttpResponseMessage GetEmployees()
        {
            DataTable dt = new DataTable();
            var item = JsonConvert.SerializeObject(dt);            
            if (item != null && !string.IsNullOrEmpty(item))
            {               
                return Request.CreateResponse(HttpStatusCode.OK, item);
            }
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

Method >1.

public HttpResponseMessage Get() {

            string yourJson = GetJsonFromSomewhere();

            var response = this.Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");

            return response;

        }


Method >2.


public HttpResponseMessage Get()        {            

            string yourJson = GetJsonFromSomewhere();

            if (!string.IsNullOrEmpty(yourJson)) {

                var response = this.Request.CreateResponse(HttpStatusCode.OK);

                response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");

                return response;

            }

            throw new HttpResponseException(HttpStatusCode.NotFound);

        }



Wednesday, 24 February 2021

How to get start and end date from month number and Year in sql server

 /////////////


declare @Year int=2020, @Month int=1

Select 'Month Name : '+ DateName( month , DateAdd( month , @Month , 0 ) - 1 )

select cast(dateadd(month, @Month - 1, dateadd(year, @Year - 1900, 0)) as date) [Start Date],

       cast(dateadd(month, @Month,dateadd(year, @Year - 1900, -1)) as date) [End Date]



////////////////// Result

Month Name : January

Start Date End Date

2020-01-01 2020-01-31


Tuesday, 9 February 2021

Select Column values as Comma Separated (Delimited) string in SQL Server using COALESCE without using xml

 ///////////////// Sql

declare @Fieldname VARCHAR(max),

     SELECT @Fieldname = COALESCE(@Fieldname + iif(len(@Fieldname)>0, ',',''), '') + CAST(Fieldname AS VARCHAR(5))

      FROM tbl_Table_Fields  order by RID

select @Fieldname

***************************---********************

declare @Fieldname VARCHAR(max)

SELECT @Fieldname = COALESCE(@Fieldname + iif(len(@Fieldname)>0, ',',''), '') + 'cast(['+Fieldname+'] as varchar(150))['+Fieldname+']' FROM tbl_Table_Fields order by RID

select @Fieldname



/////////////// REsult

cast([Reservior_Level] as varchar(150))[Reservior_Level], cast([TMC_GrossCapacity] as varchar(150))[TMC_GrossCapacity], cast([TMC_Live_Above_Cill] as varchar(150))[TMC_Live_Above_Cill], cast([TMC_Above_Cill] as varchar(150))[TMC_Above_Cill], cast([Flow_Inflow] as varchar(150))[Flow_Inflow], cast([Flow_OutFlow] as varchar(150))[Flow_OutFlow], cast([Flow_Withdrawal] as varchar(150))[Flow_Withdrawal], cast([Cum_TMC_Inflow] as varchar(150))[Cum_TMC_Inflow], cast([Cum_TMC_OutFlow] as varchar(150))[Cum_TMC_OutFlow], cast([Cum_TMC_Withdrawl] as varchar(150))[Cum_TMC_Withdrawl], cast([Evaporation] as varchar(150))[Evaporation], cast([Cum_Evaporation] as varchar(150))[Cum_Evaporation], cast([River_Spillway] as varchar(150))[River_Spillway], cast([River_PowerHouse] as varchar(150))[River_PowerHouse], cast([River_Sluice] as varchar(150))[River_Sluice], cast([Other_Abstractions] as varchar(150))[Other_Abstractions]


Monday, 8 February 2021

CSS-Only Scrollable Table or gridview with fixed headers pure css

 ///////// Style 

<style type="text/css">

        .Tbl-fixed-headers {

            width: 750px;

            table-layout: fixed;

            border-collapse: collapse;

        } 

            .Tbl-fixed-headers th {

                text-decoration: underline;

            } 

            .Tbl-fixed-headers th,

            .Tbl-fixed-headers td {

                padding: 5px;

                text-align: left;

            } 

                .Tbl-fixed-headers td:nth-child(1),

                .Tbl-fixed-headers th:nth-child(1) {

                    min-width: 200px;

                } 

                .Tbl-fixed-headers td:nth-child(2),

                .Tbl-fixed-headers th:nth-child(2) {

                    min-width: 200px;

                }

 

                .Tbl-fixed-headers td:nth-child(3),

                .Tbl-fixed-headers th:nth-child(3) {

                    width: 350px;

                } 

            .Tbl-fixed-headers thead {

                background-color: #333;

                color: #FDFDFD;

            } 

                .Tbl-fixed-headers thead tr {

                    display: block;

                    position: relative;

                }

 

            .Tbl-fixed-headers tbody {

                display: block;

                overflow: auto;

                width: 100%;

                height: 300px;

            } 

                .Tbl-fixed-headers tbody tr:nth-child(even) {

                    background-color: #DDD;

                } 

        .old_ie_wrapper {

            height: 300px;

            width: 750px;

            overflow-x: hidden;

            overflow-y: auto;

        }

            .old_ie_wrapper tbody {

                height: auto;

            }

    </style>


//////////////// Html


<table class="Tbl-fixed-headers" border="1">

  <thead>

    <tr>

      <th>Name</th>

      <th>Color</th>

      <th>Description</th>

    </tr>

  </thead>

  <tbody>

    <tr>

      <td>Apple</td>

      <td>Red</td>

      <td>These are red and this is healthy</td>

    </tr>

    <tr>

      <td>Pear</td>

      <td>Green</td>

      <td>These are green and this is healthy</td>

    </tr>

    <tr>

      <td>Grape</td>

      <td>Purple / Green</td>

      <td>These are purple and green and this is healthy</td>

    </tr>

    <tr>

      <td>Orange</td>

      <td>Orange</td>

      <td>These are orange and this is healthy</td>

    </tr>

    <tr>

      <td>Banana</td>

      <td>Yellow</td>

      <td>These are yellow and this is healthy</td>

    </tr>

    <tr>

      <td>Kiwi</td>

      <td>Green</td>

      <td>These are green and this is healthy</td>

    </tr>

    <tr>

      <td>Plum</td>

      <td>Purple</td>

      <td>These are Purple and this is healthy</td>

    </tr>

    <tr>

      <td>Watermelon</td>

      <td>Red</td>

      <td>These are red and this is healthy</td>

    </tr>

    <tr>

      <td>Tomato</td>

      <td>Red</td>

      <td>These are red and this is healthy</td>

    </tr>

    <tr>

      <td>Cherry</td>

      <td>Red</td>

      <td>These are red and this is healthy</td>

    </tr>

    <tr>

      <td>Cantelope</td>

      <td>Orange</td>

      <td>These are orange inside and this is healthy</td>

    </tr>

    <tr>

      <td>Honeydew</td>

      <td>Green</td>

      <td>These are green inside and this is healthy</td>

    </tr>

    <tr>

      <td>Papaya</td>

      <td>Green</td>

      <td>These are green and this is healthy</td>

    </tr>

    <tr>

      <td>Raspberry</td>

      <td>Red</td>

      <td>These are red and this is healthy</td>

    </tr>

    <tr>

      <td>Blueberry</td>

      <td>Blue</td>

      <td>These are blue and this is healthy</td>

    </tr>

    <tr>

      <td>Mango</td>

      <td>Orange</td>

      <td>These are orange and this is healthy</td>

    </tr>

    <tr>

      <td>Passion Fruit</td>

      <td>Green</td>

      <td>These are green and this is healthy</td>

    </tr>

  </tbody>

</table>



/////////////// Result




How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...