Wednesday 12 October 2016

Serialize dataset to xml in webservice .net

Create a class for extension:

public static class Extensions
    {
        public static string ToXml(this DataSet ds)
        {
            using (var mStream = new MemoryStream())
            {
                using (TextWriter sWriter = new StreamWriter(mStream))
                {
                    var xmlSerializer = new XmlSerializer(typeof(DataSet));
                    xmlSerializer.Serialize(sWriter, ds);
                    return Encoding.UTF8.GetString(mStream.ToArray());
                }
            }
        }
    }

Wednesday 27 July 2016

How to call web api using Ajax call

 <script>
        $(document).ready(function () {
            $("#Save").click(function () {

                var person = new Object();
                person.name = $('#name').val();
                person.surname = $('#surname').val();

                $.ajax({
                    url: 'http://localhost:11023/api/values/Get',
                    type: 'POST',
                    dataType: 'json',
                    data: person,
                    success: function (data, textStatus, xhr) {
                        console.log(data);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log('Error in Operation');
                    }
                });


            });
        });
    </script>

 Name :- <input type="text" name="name" id="name" />
        Surname:- <input type="text" name="surname" id="surname" />
        <input type="button" id="Save" value="Save Data" />



public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

Tuesday 19 July 2016

<div data-WRID="WRID-146894924273191428" data-widgetType="productBanner"  data-class="affiliateAdsByFlipkart" height="240px" width="120px"></div><script async src="//affiliate.flipkart.com/affiliate/widgets/FKAffiliateWidgets.js"></script>
<div data-WRID="WRID-146894924273191428" data-widgetType="productBanner"  data-class="affiliateAdsByFlipkart" height="240px" width="120px"></div><script async src="//affiliate.flipkart.com/affiliate/widgets/FKAffiliateWidgets.js"></script>

Tuesday 28 June 2016

Fill dataset using stored procedure in .Net

 // Within the code body set your variable  
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            DataSet ds = new DataSet();
            try
            {
                string query = "exec GetData  '" + FromDate + "','" + ToDate + "'";
                using (SqlConnection connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    adapter.Fill(ds);
                }
                return ds;
             
            }
            catch
            {
                return ds;
            }

Tuesday 7 June 2016

How to replace single quote with double quote

In C#

                strComment = ""Manoj tyagi's photo;
                strComment = strComment.Replace("'", "''");


In Sql :

EX 1


DECLARE @sql varchar(max)
SET @sql = ' INSERT INTO ' + @tempTablea + 
       ' SELECT 0 as TypeA, 0 as TypeB, ' + ''''+
         replace( @name ,'''','''''')+''''+' as Name
       FROM #tempTableb tt2'

Ex 2

UPDATE myTable1
SET myField1 = REPLACE(myField1, '''', '"');

Monday 6 June 2016

What is cursor and how to use in sql server?

Cursor: Cursor is basically a database object to retrieve data row by from resultset. cursor is used to loop through a resultset.

Steps to use cursor:

declare @studentid int

1. Declare Cursor-

DECLARE @MyCursor CURSOR

2. Set Cursor

SET @MyCursor = CURSOR FAST_FORWARD
FOR
select studentid from tbl_students

3.Open Cursor

OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @studentid
WHILE @@FETCH_STATUS = 0

WHILE @@FETCH_STATUS = 0//loop till last row

BEGIN
 query you want to execute
END

4. Close Cursor

CLOSE @MyCursor

5. Deallocate Cursor

DEALLOCATE @MyCursor