Search This Blog

Tuesday, February 4, 2014

Using jQuery ajax to call asmx WebService methods

ASP.NET > WebService > jQuery

Using jQuery ajax to call asmx WebService methods

Example:




Code:

WebService1.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace JQuery
{
    ///



    /// Summary description for WebService1
    ///
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetCode(string name)
        {
            return "Code for " + name + " is " + "1111";
        }
    }
}

HtmlPage1.html

<!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>
        <script src="jquery-2.1.0.min.js" type="text/javascript"></script>
</head>

<body>
    <form id="form1" runat="server">
    <div><br />Access Code</div>
    <div id="code"></div>

    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/GetCode",
                data: "name=John",
                dataType: "text",
                success: function (data) {
                    $("#code").html(data);
                }
            });
        });
    </script>
    </form>
</body>
</html>

Monday, February 3, 2014

Difference between Clone() and Copy() method C#

C# > String > Clone and Copy

Clone will copy the structure of a data 
Copy will copy the complete structure and data.

Example:
Difference between Clone() and Copy() method

string[] arr1 = { "a", "b", "c" };
string[] arr2 = arr1; //copy
string[] arr3 = (string[])arr1.Clone();
arr2[0] = "x";
MessageBox.Show(arr1[0] + "," + arr2[0] + "," + arr3[0]);





C# String Class

C# > String class

Represents text as a series of Unicode characters.
Examples




LEFT SQL Server Example

SQL Server > Built-in Functions > LEFT

Returns the left part of a string with the specified number of characters.

Example:

declare @str nvarchar(max)
set @str='SQL Server'
select left(@str,3)

result:
SQL




LEN SQL Server Example

SQL Server > Built-in Functions > LEN

Returns then length of string expression.

Example

declare @str nvarchar(max)

set @str='SQL Server'

select Len(@str)

result:
10




Thursday, January 30, 2014

HOST_NAME SQL Server Example

SQL Server > Built-In Functions > HOST_NAME


Represents the workstation name, name of computer connected to SQL Server.

SELECT HOST_NAME()




LINQ DataTable compare date VB.NET

VB.NET > Data > DataTable > Filter with LINQ

LINQ DataTable compare date VB.NET example

   Dim dt As DataTable = New DataTable("table")
        dt.Columns.Add("id", Type.GetType("System.Int32"))
        dt.Columns.Add("name", Type.GetType("System.String"))
        dt.Columns.Add("date_of_birth", Type.GetType("System.DateTime"))
 

        Dim dr As DataRow = dt.NewRow()
        dr("id") = 1
        dr("name") = "john"
        dr("date_of_birth") = #1/1/2000#
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("id") = 2
        dr("name") = "dan"
        dr("date_of_birth") = #9/11/1973#
        dt.Rows.Add(dr)

        Dim filteredTable As DataTable = (From n In dt.AsEnumerable()
                            Where n.Field(Of Date)("date_of_birth") = #9/11/1973#
                    Select n).CopyToDataTable()




Wednesday, January 29, 2014

DateAdd VB.NET Example

VB.NET > Functions > DateAdd

DateAdd: add or subtract a specified time interval from a date.

Example:

Label1.Text = Date.Now
Label2.Text = DateAdd(DateInterval.Day, 1, Date.Now)
Label3.Text = DateAdd(DateInterval.Month, 1, Date.Now)
Label4.Text = DateAdd(DateInterval.Year, 1, Date.Now)




Tuesday, January 28, 2014

LINQ FirstOrDefault C# Example

C# > LINQ > FirstOrDefault

Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.

Example

string[] cars = { "audi", "ford", "mercedes", "dacia" };
string car = cars.FirstOrDefault(c => c == "ford"); //ford
car = cars.FirstOrDefault(c => c == "ford1"); //null


          
  



Single LINQ C# Example

C# > LINQ > Single

Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

Example

            string[] cars = { "audi", "ford", "mercedes", "dacia" };
            try
            {
                string car = cars.Single(c => c == "ford");
            }
            catch (System.InvalidOperationException)
            {
                MessageBox.Show ("Car not found!");
            }





Monday, January 27, 2014

How To Programmatically Change Printer Settings for Internet Explorer and WebBrowser Control in C#

C# > Forms >  WebBrowser > ShowPrintPreviewDialog

How To Programmatically Change Printer Settings for Internet Explorer and WebBrowser Control in C#

Example:

Change margin right property.





string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
bool boolWritable = true;
string strName = "margin_right";
object oValue = "0";
RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, boolWritable);
oKey.SetValue(strName, oValue);
oKey.Close();

webBrowser1.ShowPrintPreviewDialog();
 
 

ROW_NUMBER SQL Server Example

SQL Server > Built-in Functions >  ROW_NUMBER

Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

Example:


CREATE TABLE #LocalTempTable(
       ID            int,
       Name   varchar(50),
       Salary int,
       DeptId int)

insert into #LocalTempTable(Id, Name, Salary, DeptId) values (1,'p1',100,1),  (2,'p2',200,1) ,  (3,'p2', 500,2)

SELECT ROW_NUMBER() OVER(ORDER BY Id DESC) AS Row,
    Name, Salary
FROM
       #LocalTempTable;

-- specify number of rows
WITH tbl AS
(
    SELECT Id, Name,
    ROW_NUMBER() OVER (ORDER BY Salary) AS RowNumber
    FROM #LocalTempTable
)

SELECT Id, Name, RowNumber 
FROM tbl
WHERE RowNumber BETWEEN 1 AND 2;

drop table #LocalTempTable










CapsLock pressed C# example

C# > Controls > > IsKeyLocked

IsKeyLocked check if CAPS LOCK, NUM LOCK, or SCROLL LOCK key is are pressed.

Example:

public static bool CapsLockActive()
{
     return Control.IsKeyLocked(Keys.CapsLock);
}




 





Friday, January 24, 2014

What is ASP.NET

ASP.NET is a unified Web development model that includes the services necessary for you to build enterprise-class Web applications.





Get file name C# Example

C# > Files > GetFileName

Returns the file name and extension of the specified path string.

Example:


Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".doc";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
     string filename = dlg.FileName;
     filename  = System.IO.Path.GetFileName(filename);
}






Autosize Control sized based on its text C# CilentSize

C# > Controls > ClientSize

ClientSize gets or sets the height and width of the client area of the control.

Example:
Autosize TextBox width and height  based on its text.








C# Code:

 private void AutoSizeControl(Control control)
        {
            Graphics g = control.CreateGraphics();
            Size size = g.MeasureString(control.Text, control.Font).ToSize();
            control.ClientSize = new Size(size.Width+10 ,size.Height);
            g.Dispose();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            AutoSizeControl(textBox1); 
        }





C# Control Class

C# > Control class




Control is the base class for controls. 
Controls are components with visual representation.