Thursday, October 15, 2009

Sort DataTable

SqlConnection cn = new SqlConnection("Data Source=mahmad\\sqlexpress;Initial Catalog=emp;user id=sa;password=sql;");
cn.Open();
SqlCommand cmd = new SqlCommand("Select City_Id,City_Name From Cities");
cmd.Connection = cn;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);

ds.Tables[0].DefaultView.Sort = "City_Name";

DataTable dtblCities = ds.Tables[0].DefaultView.ToTable();

Get Base Url Of WebSite

string urlString;
urlString = "http://" + System.Web.HttpContext.Current.Request.Url.Host;

Tuesday, October 13, 2009

Out Vs Ref Parameters

For Ref parameter you must have to assign the variable to some value;

public void Add(out int a, ref int b)
{
a = 1 + 2;
b = 3 + 4;
}



int a;
int b=0; //Ref Must Be Assigned

Add(out a, ref b);

Generic Method

public static T GMethod(T g)
{
return g;
}



Response.Write(GMethod(10));
Response.Write("
");
Response.Write(GMethod("Hi From Gineric Method !"));



=====================OutPut=============================
10
Hi From Gineric Method !

Generic Method

public static T GMethod(T g)
{
return g;
}



Response.Write(GMethod(10));
Response.Write("
");
Response.Write(GMethod("Hi From Gineric Method !"));



=====================OutPut=============================
10
Hi From Gineric Method !

Saturday, October 3, 2009

Find object is of which class

public class Super
{

}

public class a : Super
{

}


public class b : Super
{

}

public class c : Super
{

}



Super objSuper;
a objA = new a();
b objB = new b();
c objC = new c();

objSuper = objB;

if (objSuper is a)
{
Response.Write("Of A");
}
else if (objSuper is b)
{
Response.Write("Of B");
}
else
{
Response.Write("Of C");
}


================OutPut===================
Of B

Can we have abstract class without abstract method ?

Yes

Friday, October 2, 2009

Use Of PageMethods In Javascript To Call Server Side Method

1. .aspx page

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



< html xmlns="http://www.w3.org/1999/xhtml" >

< head runat="server">
< title >Untitled Page< /title >

< script language="javascript" type="text/javascript" >
function GetServerString()
{
PageMethods.getString(OnGetProductsComplete);
}

function OnGetProductsComplete(result)
{
alert(result);
}

function GetCity()
{
var txtCityId = document.getElementById('txtCityId');
var id = txtCityId.value;

PageMethods.getCityById(id,OnGetProductsComplete);
}

< /script >

< /head >
< body >
< form id="form1" runat="server" >






< /form >
< /body >
< /html >


2. .aspx.cs file

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string getString()
{
return ("Hi Mahmad !");
}

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string getCityById(int CityId)
{
SqlConnection cn = new SqlConnection("Database=Emp;Server=mahmad\\sqlexpress;User Id=sa;password=sql;");
SqlCommand cmd = new SqlCommand("select City_Name from cities Where City_ID=" + CityId);
cmd.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)
{
cn.Close();
cmd.Dispose();
return ds.Tables[0].Rows[0]["City_Name"].ToString();
}
cn.Close();
cmd.Dispose();
return "Sorry City Not Found !";

}
}


Visit Below Link To Download Full Example
http://mahmad-khoja.googlegroups.com/web/WebMethodExample.rar