opt=document.createElement("OPTION");
document.getElementById('ddlPages').options.add(opt);
opt.text=cnt-1;
opt.value=cnt-1;
Blog that contains articles about new technologies related or not with programming. I will describe and solves some problems that I encounter in my career. ASP .NET, AJAX, Javascript, C++, C# and SQL are some of the subjects that will appear.
27 December 2011
16 December 2011
17 November 2011
querystring n gv rows in javascript
query string in js: '<%=Request.QueryString["val"]%>';
--------------------------
gridrows in js: Totalrows = parseInt('<%= this.gv1.Rows.Count %>');
--------------------------
var rows = document.getElementById('gv').rows;
for(i=0;i <rows.length;i++)
{
fd = rows[i].cells[4].innerText;
rows[i].cells[4].style.display="none";
rows[i].cells[8].style.display="none";
}
------------------------------------
<asp:Label ID="lblSno" runat="server" Text="<%# Container.DataItemIndex + 1%>" /> ------------> to get the serial no for the gridview rows after bind the data (take the control to item template field of the grid)
--------------------------
gridrows in js: Totalrows = parseInt('<%= this.gv1.Rows.Count %>');
--------------------------
var rows = document.getElementById('gv').rows;
for(i=0;i <rows.length;i++)
{
fd = rows[i].cells[4].innerText;
rows[i].cells[4].style.display="none";
rows[i].cells[8].style.display="none";
}
------------------------------------
<asp:Label ID="lblSno" runat="server" Text="<%# Container.DataItemIndex + 1%>" /> ------------> to get the serial no for the gridview rows after bind the data (take the control to item template field of the grid)
11 November 2011
ws vs wcf
webservices uses XmlSerializer. A Web Service is programmable application logic accessible via standard Web protocols(Simple Object Access Protocol), which uses standards based technologies (XML for data description and HTTP for transport) to encode and transmit application data. consumers only need to understand how to send and receive SOAP messages (HTTP and XML). webservice can be hosted in iis and outside of iis. In Web service System.XML.Serialization is supported while in the WCF Service System.RunTime.Serialization is supported
wcf uses DataContractSerializer. Windows Communication Foundation (WCF) is a framework for building service-oriented applications. for secure applications, chat service that allows two people to communicate, to poll a service for the latest data feeds we use wcf. WCF is a replacement for all earlier web service technologies from Microsoft. An endpoint in WCF can be communicated with just as easily over SOAP/XML,TCP/binary,as this reduces the amount of new code needed.
WCF is flexible because its services can be hosted in different types of applications (IIS,WAS, Self-hosting, Managed Windows Service)
limits of xmlserializer:
----------------------
1.Only the public fields and properties of .NET Framework objects are translated into XML.
2.Hashtable cannot be serialized into XML.
3.Web Services can be accessed only over HTTP & it works in stateless environment
wcf uses DataContractSerializer. Windows Communication Foundation (WCF) is a framework for building service-oriented applications. for secure applications, chat service that allows two people to communicate, to poll a service for the latest data feeds we use wcf. WCF is a replacement for all earlier web service technologies from Microsoft. An endpoint in WCF can be communicated with just as easily over SOAP/XML,TCP/binary,as this reduces the amount of new code needed.
WCF is flexible because its services can be hosted in different types of applications (IIS,WAS, Self-hosting, Managed Windows Service)
limits of xmlserializer:
----------------------
1.Only the public fields and properties of .NET Framework objects are translated into XML.
2.Hashtable cannot be serialized into XML.
3.Web Services can be accessed only over HTTP & it works in stateless environment
28 October 2011
validate checkbox list using javascript
function valid()
{
var chkBoxList = document.getElementById('CheckBoxList11');
var chkBoxCount= chkBoxList.getElementsByTagName("input");
var k=0;
for(var i=0;i<chkBoxCount.length;i++)
{
if(chkBoxCount[i].checked)
k++;
}
if(k==0)
alert("select any item");
return false;
}
{
var chkBoxList = document.getElementById('CheckBoxList11');
var chkBoxCount= chkBoxList.getElementsByTagName("input");
var k=0;
for(var i=0;i<chkBoxCount.length;i++)
{
if(chkBoxCount[i].checked)
k++;
}
if(k==0)
alert("select any item");
return false;
}
24 October 2011
add js functions in .cs file in simple
Literal li = new Literal();
li.Text = "alert('hello');";
Page.Controls.Add(li);
------------------------------------
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jsfunction(), true);
Page.RegisterStartupScript("new","<script>alert('this is alert');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('hai');", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", jsfunction();, true);
Page.RegisterClientScriptBlock("a", "<script>jsfunction();</script>");
ScriptManager.RegisterClientScriptBlock(UpdatedatePanel1, this.GetType(), "Message", "Status(true);", true);
ScriptManager
.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), "$('#bd').attr('className', 'capse');", true);
Button.Attributes.Add("onclick", "return fnGet()");
for !ispostback:..
ClientScriptManager scm = this.ClientScript;
scm.RegisterStartupScript(this.GetType(), "onload", " __doPostBack('form1',null);setInterval(fncheck,10000);", true);
-------------------------------------
Response.Write("<script language="javascript">window.close();</script>");
Response.End();
li.Text = "alert('hello');";
Page.Controls.Add(li);
------------------------------------
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", jsfunction(), true);
Page.RegisterStartupScript("new","<script>alert('this is alert');</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('hai');", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", jsfunction();, true);
Page.RegisterClientScriptBlock("a", "<script>jsfunction();</script>");
ScriptManager.RegisterClientScriptBlock(UpdatedatePanel1, this.GetType(), "Message", "Status(true);", true);
ScriptManager
.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), "$('#bd').attr('className', 'capse');", true);
Button.Attributes.Add("onclick", "return fnGet()");
for !ispostback:..
ClientScriptManager scm = this.ClientScript;
scm.RegisterStartupScript(this.GetType(), "onload", " __doPostBack('form1',null);setInterval(fncheck,10000);", true);
-------------------------------------
Response.Write("<script language="javascript">window.close();</script>");
Response.End();
check whether the given value is string or not
public static bool IsNumeric(String strVal)
{
Regex reg = new Regex("[^0-9-]");
Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
}
-----------------------------
isNaN(objnumber)
{
Regex reg = new Regex("[^0-9-]");
Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
}
-----------------------------
isNaN(objnumber)
get files from a directory folder
string[] files = System.IO.Directory.GetFiles(serverpath + "/" + inputfolder);
filter data of dataset / datatable
ds.Tables[0].Select("Name LIKE %" + "abc%" );
------------------------------
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "lastname like '" + txtName.Text + "%'";
DataTable dt = dv.ToTable();
------------------------------
if (((DataRow[])dt.Select("dname="+newdnam)).Length > 0)
---------------------------------------------------------------
DataTable dt = (DataTable)Session["data"];
DataRow[] drow = dt.Select("id="+id+"");
Label1.Text = drow[0].ItemArray[1].ToString();
---------------------------------------------------------------
dt.Constraints.Clear();
dt.PrimaryKey = newDataColumn[] { dt.Columns["id"] };DataRow[] dr = dt.Rows.Find(val.ToString());
------------------------------
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "lastname like '" + txtName.Text + "%'";
DataTable dt = dv.ToTable();
------------------------------
if (((DataRow[])dt.Select("dname="+newdnam)).Length > 0)
---------------------------------------------------------------
DataTable dt = (DataTable)Session["data"];
DataRow[] drow = dt.Select("id="+id+"");
Label1.Text = drow[0].ItemArray[1].ToString();
---------------------------------------------------------------
dt.Constraints.Clear();
dt.PrimaryKey = newDataColumn[] { dt.Columns["id"] };DataRow[] dr = dt.Rows.Find(val.ToString());
don't allow duplicates for a listbox
if (!ContactsListBox.Items.Contains(item))
{
ContactsListBox.Items.Add(item);
}
{
ContactsListBox.Items.Add(item);
}
get datetime in user friendly (datetime convertion) in sqlserver
select convert (varchar(10), getdate(),101)
------------------------------------------
ddat = new Date();
ddat = new Date(ddat.setDate(new Date(stdate.value).getDate()+parseInt(days)));
var d=newdate.getDate();
var m=(newdate.getMonth()+1);
var y=newdate.getFullYear();
-----------------------------------------
day = new Date()
day = new Date("August15, 2006 08:25:00")
day = new Date(06,8,15)
day = new Date(06,8,15,8,25,0)
Ref: http://msdn.microsoft.com/en-us/library/ms187928.aspx with convertion chart
select convert(varchar, getdate(), 100) convertResult,100 style union
select convert(varchar, getdate(), 101),101 union
select convert(varchar, getdate(), 102),102 union
select convert(varchar, getdate(), 103),103 union
select convert(varchar, getdate(), 104),104 union
select convert(varchar, getdate(), 105),105 union
select convert(varchar, getdate(), 106),106 union
select convert(varchar, getdate(), 107),107 union
select convert(varchar, getdate(), 108),108 union
select convert(varchar, getdate(), 109),109 union
select convert(varchar, getdate(), 110),110 union
select convert(varchar, getdate(), 111),111 union
select convert(varchar, getdate(), 112),112 union
select convert(varchar, getdate(), 113),113 union
select convert(varchar, getdate(), 114),114 union
select convert(varchar, getdate(), 120),120 union
select convert(varchar, getdate(), 121),121 union
select convert(varchar, getdate(), 126),126 union
select convert(varchar, getdate(), 127),127 union
select convert(varchar, getdate(), 130),130 union
select convert(varchar, getdate(), 131),131
order by 2
------------------------------------------
ddat = new Date();
ddat = new Date(ddat.setDate(new Date(stdate.value).getDate()+parseInt(days)));
var d=newdate.getDate();
var m=(newdate.getMonth()+1);
var y=newdate.getFullYear();
-----------------------------------------
day = new Date()
day = new Date("August15, 2006 08:25:00")
day = new Date(06,8,15)
day = new Date(06,8,15,8,25,0)
Ref: http://msdn.microsoft.com/en-us/library/ms187928.aspx with convertion chart
select convert(varchar, getdate(), 100) convertResult,100 style union
select convert(varchar, getdate(), 101),101 union
select convert(varchar, getdate(), 102),102 union
select convert(varchar, getdate(), 103),103 union
select convert(varchar, getdate(), 104),104 union
select convert(varchar, getdate(), 105),105 union
select convert(varchar, getdate(), 106),106 union
select convert(varchar, getdate(), 107),107 union
select convert(varchar, getdate(), 108),108 union
select convert(varchar, getdate(), 109),109 union
select convert(varchar, getdate(), 110),110 union
select convert(varchar, getdate(), 111),111 union
select convert(varchar, getdate(), 112),112 union
select convert(varchar, getdate(), 113),113 union
select convert(varchar, getdate(), 114),114 union
select convert(varchar, getdate(), 120),120 union
select convert(varchar, getdate(), 121),121 union
select convert(varchar, getdate(), 126),126 union
select convert(varchar, getdate(), 127),127 union
select convert(varchar, getdate(), 130),130 union
select convert(varchar, getdate(), 131),131
order by 2
convertResult | style |
Mar 18 2016 5:27AM | 100 |
3/18/2016 | 101 |
2016.03.18 | 102 |
18/03/2016 | 103 |
18.03.2016 | 104 |
18-03-2016 | 105 |
18-Mar-16 | 106 |
18-Mar-16 | 107 |
5:27:19 | 108 |
Mar 18 2016 5:27:19:257AM | 109 |
3/18/2016 | 110 |
3/18/2016 | 111 |
20160318 | 112 |
18 Mar 2016 05:27:19:257 | 113 |
05:27:19:257 | 114 |
3/18/2016 5:27 | 120 |
27:19.3 | 121 |
2016-03-18T05:27:19.257 | 126 |
2016-03-18T05:27:19.257 | 127 |
9 ????? ??????? 1437 5:27:19 | 130 |
9/06/1437 5:27:19:257AM | 131 |
how to disable the all controls in a page
private void Disable(Control c)
{
if ((c is TextBox) || (c is LinkButton) || (c is Button) || (c is CheckBox) || (c is CheckBoxList) ||
(c is RadioButtonList) || (c is DropDownList) || (c is Panel) || (c is ImageButton))
{
((WebControl)c).Enabled = false;
}
foreach (Control ct in c.Controls)
Disable(ct);
}
{
if ((c is TextBox) || (c is LinkButton) || (c is Button) || (c is CheckBox) || (c is CheckBoxList) ||
(c is RadioButtonList) || (c is DropDownList) || (c is Panel) || (c is ImageButton))
{
((WebControl)c).Enabled = false;
}
foreach (Control ct in c.Controls)
Disable(ct);
}
how to use gridview datarow bound event
protected void gvSkinLoc_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == "Head")
e.Row.Cells[1].Text = "d Head Part";
}
}
Init, Load, Render,LoadComplete, Unload
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == "Head")
e.Row.Cells[1].Text = "d Head Part";
}
}
Init, Load, Render,LoadComplete, Unload
03 October 2011
get the ascii value as well as the typed value in alert
function getval()
{
var keyASCII = window.event.keyCode;
alert(keyASCII);
var keyValue = String.fromCharCode(keyASCII);
alert(keyValue);
}
{
var keyASCII = window.event.keyCode;
alert(keyASCII);
var keyValue = String.fromCharCode(keyASCII);
alert(keyValue);
}
30 September 2011
split last string in sqlserver
- username
emp/satya
md/sai
emp/develop/suresh
clerk/surya
comp/sam
select reverse(substring(reverse(username),0,charindex('/',reverse(username)))) from tblemp
creating new row for a gridview dynamically
if (!IsPostBack)
{
getdata();
}
----------------------------
void getdata()
{
dalLogin dal = new dalLogin();
DataSet ds = new DataSet();
ds= dal.getdata();
ViewState["table"] = ds.Tables[0];
bindgrid();
}
void bindgrid()
{
GridView1.DataSource = (DataTable)ViewState["table"];
GridView1.DataBind();
}
-------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[5].Attributes.Add("style", "display:none");
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
GridView1.Rows[i].Cells[5].Attributes.Add("style", "display:none");
}
}
-----------------------------------
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "newrow")
{
DataTable dt = (DataTable)ViewState["table"];
dt.Rows.Add(dt.NewRow());
ViewState["table"] = dt;
GridView1.EditIndex = GridView1.Rows.Count;
bindgrid();
Button btn = (Button)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Button1");
btn.Text = "save";
btn.CommandName = "sav";
}
else if (e.CommandName == "sav")
{
TextBox t1 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[0].Controls[0];
TextBox t2 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[1].Controls[0];
TextBox t3 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[2].Controls[0];
TextBox t4 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[3].Controls[0];
TextBox t5 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[4].Controls[0];
DataTable dt = (DataTable)ViewState["table"];
dt.Rows.RemoveAt(GridView1.Rows.Count - 1);
DataRow dr = dt.NewRow();
dr[0] = t1.Text; dr[1] = t2.Text; dr[2] = t3.Text; dr[3] = t4.Text; dr[4] = t5.Text;
dt.Rows.Add(dr);
ViewState["table"] = dt;
GridView1.EditIndex = -1;
bindgrid();
Button btn = (Button)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Button1");
btn.Text = "new";
btn.CommandName = "newrow";
}
}
{
getdata();
}
----------------------------
void getdata()
{
dalLogin dal = new dalLogin();
DataSet ds = new DataSet();
ds= dal.getdata();
ViewState["table"] = ds.Tables[0];
bindgrid();
}
void bindgrid()
{
GridView1.DataSource = (DataTable)ViewState["table"];
GridView1.DataBind();
}
-------------------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[5].Attributes.Add("style", "display:none");
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
GridView1.Rows[i].Cells[5].Attributes.Add("style", "display:none");
}
}
-----------------------------------
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "newrow")
{
DataTable dt = (DataTable)ViewState["table"];
dt.Rows.Add(dt.NewRow());
ViewState["table"] = dt;
GridView1.EditIndex = GridView1.Rows.Count;
bindgrid();
Button btn = (Button)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Button1");
btn.Text = "save";
btn.CommandName = "sav";
}
else if (e.CommandName == "sav")
{
TextBox t1 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[0].Controls[0];
TextBox t2 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[1].Controls[0];
TextBox t3 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[2].Controls[0];
TextBox t4 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[3].Controls[0];
TextBox t5 = (TextBox)GridView1.Rows[GridView1.Rows.Count - 1].Cells[4].Controls[0];
DataTable dt = (DataTable)ViewState["table"];
dt.Rows.RemoveAt(GridView1.Rows.Count - 1);
DataRow dr = dt.NewRow();
dr[0] = t1.Text; dr[1] = t2.Text; dr[2] = t3.Text; dr[3] = t4.Text; dr[4] = t5.Text;
dt.Rows.Add(dr);
ViewState["table"] = dt;
GridView1.EditIndex = -1;
bindgrid();
Button btn = (Button)GridView1.Rows[GridView1.Rows.Count - 1].FindControl("Button1");
btn.Text = "new";
btn.CommandName = "newrow";
}
}
29 September 2011
checking regular expression for different date formats
string spl = @"([/|\-|.|\s])?";
string d = "(0?[1-9]|[12][0-9]|3[01])";
string m = "(0?[1-9]|1[012])";
string mmm = "([J|j]an(uary)?|[F|f]eb(ruary)?|([M|m]a(r(ch)?|y))|[A|a]pr(il)?|[J|j]u(n(e)?|l(y)?)|[A|a]ug(ust)?|[O|o]ct(ober)?|([S|s]ep(t)?|[N|n]ov|[D|d]ec)(ember)?)";
string y = "((19|20)?[0-9]{2})";
string timspl = @"([:|.|\s])?";
string hh = @"(0?[0-9]|1[012])";
string min = @"([0?|1|2|3|4|5][0-9]|60)";
string ss = @"([0?|1|2|3|4|5][0-9]|60)";
string tt = @"([A|a|p|P]m|[A|P]M)?";
string time = @"(" + hh + timspl + min + timspl + ss + ")?"+spl+tt;
string zzz = @"(\+[0-9]{2}(\:)?[0-9]{2})?"+spl+tt;
string date = @"("+d+spl+"("+m+"|"+mmm+")|("+m+"|"+mmm+"+)+"+spl+d+")"+spl+y+"";
string exp = "^(" + date + spl + time + spl + zzz + ")|(" + time + spl + date + spl + zzz + ")|(" + zzz + spl + time + spl + date + ")$";
if (Regex.IsMatch(txtreg.Text.ToLower().Trim(), exp))
lblreg.Text = "available";
else
lblreg.Text = "not available";
string d = "(0?[1-9]|[12][0-9]|3[01])";
string m = "(0?[1-9]|1[012])";
string mmm = "([J|j]an(uary)?|[F|f]eb(ruary)?|([M|m]a(r(ch)?|y))|[A|a]pr(il)?|[J|j]u(n(e)?|l(y)?)|[A|a]ug(ust)?|[O|o]ct(ober)?|([S|s]ep(t)?|[N|n]ov|[D|d]ec)(ember)?)";
string y = "((19|20)?[0-9]{2})";
string timspl = @"([:|.|\s])?";
string hh = @"(0?[0-9]|1[012])";
string min = @"([0?|1|2|3|4|5][0-9]|60)";
string ss = @"([0?|1|2|3|4|5][0-9]|60)";
string tt = @"([A|a|p|P]m|[A|P]M)?";
string time = @"(" + hh + timspl + min + timspl + ss + ")?"+spl+tt;
string zzz = @"(\+[0-9]{2}(\:)?[0-9]{2})?"+spl+tt;
string date = @"("+d+spl+"("+m+"|"+mmm+")|("+m+"|"+mmm+"+)+"+spl+d+")"+spl+y+"";
string exp = "^(" + date + spl + time + spl + zzz + ")|(" + time + spl + date + spl + zzz + ")|(" + zzz + spl + time + spl + date + ")$";
if (Regex.IsMatch(txtreg.Text.ToLower().Trim(), exp))
lblreg.Text = "available";
else
lblreg.Text = "not available";
27 September 2011
date regular expressions in .net
@"(([0-9]{1,31})[/|-|(\s)]([0-9]{1,12})[/|-|(\s)](\d{4}|\d{2}))"
//DD/MM/YY(YY)
@"(([0-9]{1,12})[/|-|(\s)]([0-9]{1,31})[/|-|(\s)](\d{4}|\d{2}))"
//MM/DD/YY(YY)
@"((\d{4}|\d{2})[/|-|(\s)]([0-9]{1,31})[/|-|(\s)]([0-9]{1,12}))"
//YY(YY)/DD/MM
@"((\d{4}|\d{2})[/|-|(\s)]([0-9]{1,12})[/|-|(\s)]([0-9]{1,31}))"
//YY(YY)/MM/DD
@"(([0-9]{1,12})[/|-|(\s)](\d{4}|\d{2})[/|-|(\s)]([0-9]{1,31}))"
//MM/YY(YY)/DD
@"(([0-9]{1,31})[/|-|(\s)](\d{4}|\d{2})[/|-|(\s)]([0-9]{1,12}))"
//DD/YY(YY)/MM
@"^(([0-9]{1,2})(\s)?(Jan(uary)?|Feb(ruary)?|(Ma(r(ch)?|y))|Apr(il)?|Ju(n(e)?|l(y)?)|Aug|Oct(ober)?|(Sep(t)?|Nov|Dec)(ember)?)(\s)?(\d{2}|\d{4}))$"
//DD()MMM()YYYY
//DD/MM/YY(YY)
@"(([0-9]{1,12})[/|-|(\s)]([0-9]{1,31})[/|-|(\s)](\d{4}|\d{2}))"
//MM/DD/YY(YY)
@"((\d{4}|\d{2})[/|-|(\s)]([0-9]{1,31})[/|-|(\s)]([0-9]{1,12}))"
//YY(YY)/DD/MM
@"((\d{4}|\d{2})[/|-|(\s)]([0-9]{1,12})[/|-|(\s)]([0-9]{1,31}))"
//YY(YY)/MM/DD
@"(([0-9]{1,12})[/|-|(\s)](\d{4}|\d{2})[/|-|(\s)]([0-9]{1,31}))"
//MM/YY(YY)/DD
@"(([0-9]{1,31})[/|-|(\s)](\d{4}|\d{2})[/|-|(\s)]([0-9]{1,12}))"
//DD/YY(YY)/MM
@"^(([0-9]{1,2})(\s)?(Jan(uary)?|Feb(ruary)?|(Ma(r(ch)?|y))|Apr(il)?|Ju(n(e)?|l(y)?)|Aug|Oct(ober)?|(Sep(t)?|Nov|Dec)(ember)?)(\s)?(\d{2}|\d{4}))$"
//DD()MMM()YYYY
19 September 2011
how to find out rank of employees based on their salaries
select empname,dense_rank()over(order by salary)as rank from tblEmp
get the serial no. for records of table in sqlserver
select empname, row_number() over(order by empid)as 's.no' from tblEmp
Get the list of columns in a table in sqlserver
select column_name from information_schema.columns where table_name='tblEmp'
or
sp_columns 'tblName'
or
select name from sys.columns where object_id=object_id('tblEmp')
or
select rows from sysindexes where id=object_id('tblEemp') and indid in (0,1)
or
sp_columns 'tblName'
or
select name from sys.columns where object_id=object_id('tblEmp')
or
select rows from sysindexes where id=object_id('tblEemp') and indid in (0,1)
get the list of tables that r available in a particular database in sqlserver
select * from information_schema.tables
or
select * from sys.tables
or
sp_tables
or
select * from sys.tables
or
sp_tables
copy one column data into other column of a same table using query in sqlserver
update tblEmp set address1=address2
how to find out second highest salaried employee details of a table in sqlserver
with s1(id,names,sal) as (select 1 ,'a' ,1.00 union all select 2,'b',2.00 union all select 3,'c',3.00 union all select 4,'d',4.00 union all select 5,'f',5.00)
select * into sam from s1
select * from sam
--get 2nd highest sal.emp
--get 2nd lowest sal.emp
Max possible columns per a table in sqlserver is 1024 but in oracle 1000
Max possible databases : 32,767
select * into sam from s1
select * from sam
--get 2nd highest sal.emp
- select top 1 * from sam where sal in (select top 2 sal from sam order by sal desc) order by sal
- select * from sam tbl1 where 1=(select count(distinct sal) from sam tbl2 where tbl1.sal<tbl2.sal)
- select top 1 * from ( select top 2 * from sam order by sal desc)s order by sal
- select * from (select *, rn=row_number() over(order by sal desc) from sam) s where rn=2
- select * from (select *, rn = dense_rank() over(order by sal desc) from sam) s where rn=2
--get 2nd lowest sal.emp
- select top 1 * from sam where sal in (select top 2 sal from sam order by sal) order by sal desc
Max possible columns per a table in sqlserver is 1024 but in oracle 1000
Max possible databases : 32,767
Operator OverLoading in console apps
class ClsComplex
{
int R, I;
public ClsComplex(int R, int I)
{
this.R = R;
this.I = I;
Console.WriteLine("{0} + {1}i", R, I);
}
public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
{
ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
return Num3;
}
}
class ClsOpOverLoad
{
static void Main(string[] args)
{
Console.Write("C1= ");
ClsComplex C1 = new ClsComplex(4, 5);
Console.Write("C2= ");
ClsComplex C2 = new ClsComplex(8, 2);
Console.Write("C3= ");
ClsComplex C3 = C1 + C2;
Console.Read();
}
}
{
int R, I;
public ClsComplex(int R, int I)
{
this.R = R;
this.I = I;
Console.WriteLine("{0} + {1}i", R, I);
}
public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
{
ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
return Num3;
}
}
class ClsOpOverLoad
{
static void Main(string[] args)
{
Console.Write("C1= ");
ClsComplex C1 = new ClsComplex(4, 5);
Console.Write("C2= ");
ClsComplex C2 = new ClsComplex(8, 2);
Console.Write("C3= ");
ClsComplex C3 = C1 + C2;
Console.Read();
}
}
Method OverLoading in console apps
class mOverLoading
{
int x=10;
public void show()
{
Console.WriteLine("default constructor");
Console.WriteLine(x + this.x);
}
public void show(int x)
{
Console.WriteLine("Parameterized constructors");
Console.WriteLine(x+" "+this.x);
}
public void show(string s, int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(s + " " + x);
}
public static void Main()
{
mOverLoading mol = new mOverLoading();
mol.show();
mol.show(123);
mol.show("satya", 456);
Console.ReadLine();
}
}
{
int x=10;
public void show()
{
Console.WriteLine("default constructor");
Console.WriteLine(x + this.x);
}
public void show(int x)
{
Console.WriteLine("Parameterized constructors");
Console.WriteLine(x+" "+this.x);
}
public void show(string s, int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(s + " " + x);
}
public static void Main()
{
mOverLoading mol = new mOverLoading();
mol.show();
mol.show(123);
mol.show("satya", 456);
Console.ReadLine();
}
}
Constructor OverLoading in Console apps
class CACOverLoading
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}
Prime No. in console apps
class Prime
{
static void Main(string[] args)
{
int j = 0;
int count = 0;
Console.WriteLine("Plz enter a number: ");
int n =int.Parse( Console.ReadLine() );
Console.WriteLine("The Prime no's are:");
for (int i = 2; i <= n; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == j)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine();
Console.Write("Total Prime No's: "+count);
Console.ReadLine();
}
{
static void Main(string[] args)
{
int j = 0;
int count = 0;
Console.WriteLine("Plz enter a number: ");
int n =int.Parse( Console.ReadLine() );
Console.WriteLine("The Prime no's are:");
for (int i = 2; i <= n; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == j)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine();
Console.Write("Total Prime No's: "+count);
Console.ReadLine();
}
Virtual class overriding
class Virtual
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}
Abstract overriding in console apps
abstract class Abstract {
public abstract void mul(int x);
public void add(int x, int y)
{
Console.WriteLine("add is: "+(x + y));
}
}
class overrideabstract : Abstract
{
public override void mul(int a)
{
Console.WriteLine("mul is: "+a*a);
}
public static void Main()
{
overrideabstract ovra = new overrideabstract();
ovra.add(4, 5);
ovra.mul(3);
Console.ReadLine();
}
}
Note: An abstract class can implement from another abstract class. and a class can implement only one abstract class at a time.
public abstract void mul(int x);
public void add(int x, int y)
{
Console.WriteLine("add is: "+(x + y));
}
}
class overrideabstract : Abstract
{
public override void mul(int a)
{
Console.WriteLine("mul is: "+a*a);
}
public static void Main()
{
overrideabstract ovra = new overrideabstract();
ovra.add(4, 5);
ovra.mul(3);
Console.ReadLine();
}
}
Note: An abstract class can implement from another abstract class. and a class can implement only one abstract class at a time.
Reverse of a string or number in console apps
class Reverseofno
{
public static void Main()
{
Console.WriteLine("enter a no/string: ");
string a = Console.ReadLine();
for (int x = a.Length - 1; x >= 0; x--)
{
Console.Write(a[x]);
}
Console.WriteLine();
--------------------------------------------------
string bb = string.Empty;
Console.WriteLine("enter a no/string: ");
string b = Console.ReadLine();
for (int x = b.Length - 1; x >= 0; x--)
{
bb += b[x].ToString();
}
string[] bbb = new string[bb.Split(' ').Length];
for (int y = 0; y = 0; z--)
{
Console.Write(bbb[z]);
}
Console.WriteLine();
---------------------------------------------------
Console.WriteLine("enter a name: ");
string n= Console.ReadLine();
if(bbb.Contains(n+" "))
Console.WriteLine("found");
else
Console.WriteLine("not found");
Console.ReadLine();
}
}
{
public static void Main()
{
Console.WriteLine("enter a no/string: ");
string a = Console.ReadLine();
for (int x = a.Length - 1; x >= 0; x--)
{
Console.Write(a[x]);
}
Console.WriteLine();
--------------------------------------------------
string bb = string.Empty;
Console.WriteLine("enter a no/string: ");
string b = Console.ReadLine();
for (int x = b.Length - 1; x >= 0; x--)
{
bb += b[x].ToString();
}
string[] bbb = new string[bb.Split(' ').Length];
for (int y = 0; y = 0; z--)
{
Console.Write(bbb[z]);
}
Console.WriteLine();
---------------------------------------------------
Console.WriteLine("enter a name: ");
string n= Console.ReadLine();
if(bbb.Contains(n+" "))
Console.WriteLine("found");
else
Console.WriteLine("not found");
Console.ReadLine();
}
}
how to know the current system name and ip address in asp.net
Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()+"
");
Response.Write(HttpContext.Current.Server.MachineName);
");
Response.Write(HttpContext.Current.Server.MachineName);
sp for search
create proc sp_search(@p varchar(20))
as
begin
select lastname from tblemployee where lastname is not null and lastname like @p+'%'
end
--exec sa_tstname c
as
begin
select lastname from tblemployee where lastname is not null and lastname like @p+'%'
end
--exec sa_tstname c
10 September 2011
GridView data filter with StoredProcedure
SqlCommand cmd=newSqlCommand();
SqlConnection con = new SqlConnection("..your connection string....");
//Data Source=satya;Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=sa123
SqlDataAdapter da=newSqlDataAdapter();
DataSet ds=newDataSet();
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_getall";
cmd.Connection = con;
cmd.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = txtSearch.Text;
da = newSqlDataAdapter(cmd);
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "eName like '" + mr+"'";
gvsearch.DataSource = dv;
gvsearch.DataBind();
create proc sp_getall
as
begin
select*from tblemp
end
------------------------------------
cmd.CommandText = "select * from user_master";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
//table, ExecuteScalar - single value object type, ExecuteNonQuery - Count
gv1.DataSource = dr;
gv1.DataBind();
con.Close();
SqlConnection con = new SqlConnection("..your connection string....");
//Data Source=satya;Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=sa123
SqlDataAdapter da=newSqlDataAdapter();
DataSet ds=newDataSet();
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_getall";
cmd.Connection = con;
cmd.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = txtSearch.Text;
da = newSqlDataAdapter(cmd);
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "eName like '" + mr+"'";
gvsearch.DataSource = dv;
gvsearch.DataBind();
create proc sp_getall
as
begin
select*from tblemp
end
------------------------------------
cmd.CommandText = "select * from user_master";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
//table, ExecuteScalar - single value object type, ExecuteNonQuery - Count
gv1.DataSource = dr;
gv1.DataBind();
con.Close();
26 August 2011
GridView data filter with StoredProcedure
SqlCommand cmd=new SqlCommand();
cmd.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = txtSearch.Text;
da = new SqlDataAdapter(cmd);
SqlConnection con = new SqlConnection("..your connection string....");
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
cmd.Parameters.Clear();SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_getall";
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;dv.RowFilter = "eName like '" + mr+"'";
gvsearch.DataSource = dv;
gvsearch.DataBind(); create proc sp_getall
as
begin
select * from tblemp
endbegin
select * from tblemp
GridView data filter with StoredProcedure
SqlCommand cmd=new SqlCommand();
cmd.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = txtSearch.Text;
da = new SqlDataAdapter(cmd);
SqlConnection con = new SqlConnection("..your connection string....");
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
cmd.Parameters.Clear();SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_getall";
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;dv.RowFilter = "eName like '" + mr+"'";
gvsearch.DataSource = dv;
gvsearch.DataBind(); create proc sp_getall
as
begin
select * from tblemp
endbegin
select * from tblemp
sp for search
create proc sa_tstname(@p varchar(20))
as
begin
select lastname from geninfo where lastname like @p+'%'
end
exec sa_tstname c
25 August 2011
sp for search
create proc sa_tstname(@p varchar(20))
as
begin
select lastname from geninfo where lastname like @p+'%'
end
exec sa_tstname c
24 August 2011
checkbox check/uncheck all in gridview
static int kount=0;
protected void chkHead_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");
if (chhead.Checked)
ch.Checked = true;
else
ch.Checked = false;
}
}
protected void chkItem_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");
if (gv.Rows.Count == 1)
{
CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");
if (sinch.Checked)
chhead.Checked = true;
else
chhead.Checked = false;
}
else
{
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.FindControl("chkItem");
if (ch.Checked)
kount++;
}
if (kount == gv.Rows.Count)
chhead.Checked = true;
else
{
kount = 0;
chhead.Checked = false;
}
}
}
23 August 2011
checkbox check/uncheck all in gridview
protectedvoid chkHead_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");if (chhead.Checked)
ch.Checked =true;
else
ch.Checked =false;
}
}
protectedvoid chkItem_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");if (gv.Rows.Count == 1)
{
CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");if (sinch.Checked)
chhead.Checked =true;
else
chhead.Checked =false;
}
else
{
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.FindControl("chkItem");if (ch.Checked)
kount++;
}
if (kount == gv.Rows.Count)
chhead.Checked =true;
else
{
kount = 0;
chhead.Checked =false;
}
}
}
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");if (chhead.Checked)
ch.Checked =true;
else
ch.Checked =false;
}
}
protectedvoid chkItem_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");if (gv.Rows.Count == 1)
{
CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");if (sinch.Checked)
chhead.Checked =true;
else
chhead.Checked =false;
}
else
{
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.FindControl("chkItem");if (ch.Checked)
kount++;
}
if (kount == gv.Rows.Count)
chhead.Checked =true;
else
{
kount = 0;
chhead.Checked =false;
}
}
}
checkbox check/uncheck all in gridview
static int kount=0;
protected void chkHead_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");
if (chhead.Checked)
ch.Checked = true;
else
ch.Checked = false;
}
}
protected void chkItem_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");
if (gv.Rows.Count == 1)
{
CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");
if (sinch.Checked)
chhead.Checked = true;
else
chhead.Checked = false;
}
else
{
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.FindControl("chkItem");
if (ch.Checked)
kount++;
}
if (kount == gv.Rows.Count)
chhead.Checked = true;
else
{
kount = 0;
chhead.Checked = false;
}
}
}
22 August 2011
how to know the current system name and ip address in asp.net
Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()+"
");
Response.Write(HttpContext.Current.Server.MachineName);
");
Response.Write(HttpContext.Current.Server.MachineName);
grid view sorting and paging in asp.net using C#.net code
protected void gv_Sorting(object sender, GridViewSortEventArgse)
{
DataSet ds = newDataSet();
ds = bal.getdata();
ds.Tables[0].DefaultView.Sort = e.SortExpression +
" " + GetSortDirection(e.SortExpression);
gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
}
privatestring GetSortDirection(string column)
{
string sortDirection = "ASC";
ViewState["SortExpression"] = column;
ViewState["SortDirection"] = sortDirection;string sortExpression = ViewState["SortExpression"] asstring;if (sortExpression != null)
{
string lastDirection = ViewState["SortDirection"] asstring;if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection ="DESC";
}
}
return sortDirection;
}
protectedvoid gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
getdata();
}
{
DataSet ds = newDataSet();
ds = bal.getdata();
ds.Tables[0].DefaultView.Sort = e.SortExpression +
" " + GetSortDirection(e.SortExpression);
gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
}
privatestring GetSortDirection(string column)
{
string sortDirection = "ASC";
ViewState["SortExpression"] = column;
ViewState["SortDirection"] = sortDirection;string sortExpression = ViewState["SortExpression"] asstring;if (sortExpression != null)
{
string lastDirection = ViewState["SortDirection"] asstring;if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection ="DESC";
}
}
return sortDirection;
}
protectedvoid gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
getdata();
}
how to know the current system name and ip address in asp.net
Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()+"<br/>");
Response.Write(HttpContext.Current.Server.MachineName);
Response.Write(HttpContext.Current.Server.MachineName);
grid view sorting and paging in asp.net using C#.net code
protected void gv_Sorting(object sender, GridViewSortEventArgse)
{
DataSet ds = newDataSet();
ds = bal.getdata();
ds.Tables[0].DefaultView.Sort = e.SortExpression +
" " + GetSortDirection(e.SortExpression);
gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
}
privatestring GetSortDirection(string column)
{
string sortDirection = "ASC";
ViewState["SortExpression"] = column;
ViewState["SortDirection"] = sortDirection;string sortExpression = ViewState["SortExpression"] asstring;if (sortExpression != null)
{
string lastDirection = ViewState["SortDirection"] asstring;if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection ="DESC";
}
}
return sortDirection;
}
protectedvoid gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
getdata();
}
{
DataSet ds = newDataSet();
ds = bal.getdata();
ds.Tables[0].DefaultView.Sort = e.SortExpression +
" " + GetSortDirection(e.SortExpression);
gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
}
privatestring GetSortDirection(string column)
{
string sortDirection = "ASC";
ViewState["SortExpression"] = column;
ViewState["SortDirection"] = sortDirection;string sortExpression = ViewState["SortExpression"] asstring;if (sortExpression != null)
{
string lastDirection = ViewState["SortDirection"] asstring;if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection ="DESC";
}
}
return sortDirection;
}
protectedvoid gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
getdata();
}
Grid view sorting in asp.net using code
SqlConnection con = new SqlConnection("Data Source=...;Initial Catalog=...;Persist Security Info=True;User ID=sa;Password=...");
SqlDataAdapter da;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetData();
}
void GetData()
{
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from sample",con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from sample", con);
da.Fill(dt);
dt.DefaultView.Sort = e.SortExpression + direction();
GridView1.DataSource = dt;
GridView1.DataBind();
}
string direction()
{
if (ViewState["stat"] != null && ViewState["stat"].ToString().Trim() == "desc")
{
ViewState["stat"] = " asc";
return " asc";
}
else
{
ViewState["stat"] = " desc";
return " desc";
}
}
SqlDataAdapter da;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetData();
}
void GetData()
{
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from sample",con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from sample", con);
da.Fill(dt);
dt.DefaultView.Sort = e.SortExpression + direction();
GridView1.DataSource = dt;
GridView1.DataBind();
}
string direction()
{
if (ViewState["stat"] != null && ViewState["stat"].ToString().Trim() == "desc")
{
ViewState["stat"] = " asc";
return " asc";
}
else
{
ViewState["stat"] = " desc";
return " desc";
}
}
18 August 2011
xml read, write, modify data in asp.net
wsex.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml;
public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");
XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);
XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);
XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);
xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");
}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
wsxml:(webservice class)
[WebMethod]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml;
public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");
XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);
XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);
XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);
xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");
}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
emp.xml:
1
s
s
2
b
che
3
c
ban
wsxml:(webservice class)
[WebMethod]
public DataSet getdata()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
return ds;
}
17 August 2011
xml read, write, modify data in asp.net
wsex.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml;
public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");
XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);
XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);
XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);
xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");
}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
wsxml:(webservice class)
[WebMethod]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml;
public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");
XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);
XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);
XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);
xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");
}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
emp.xml:
<?xml version="1.0" standalone="yes"?>
<employee>
<emp>
<id>1</id>
<name>s</name>
<add>s</add>
</emp>
<emp>
<id>2</id>
<name>b</name>
<add>che</add>
</emp>
<emp>
<id>3</id>
<name>c</name>
<add>ban</add>
</emp>
</employee>
wsxml:(webservice class)
[WebMethod]
public DataSet getdata()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
return ds;
}
xml read, write, modify data in asp.net
wsex.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml;
public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");
XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);
XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);
XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);
xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");
}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
wsxml:(webservice class)
[WebMethod]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml;
public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");
XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);
XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);
XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);
xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");
}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
emp.xml:
<?xml version="1.0" standalone="yes"?>
<employee>
<emp>
<id>1</id>
<name>s</name>
<add>s</add>
</emp>
<emp>
<id>2</id>
<name>b</name>
<add>che</add>
</emp>
<emp>
<id>3</id>
<name>c</name>
<add>ban</add>
</emp>
</employee>
wsxml:(webservice class)
[WebMethod]
public DataSet getdata()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
return ds;
}
31 July 2011
Reverse of a string or number in console apps
class Reverseofno
{
public static void Main()
{
Console.WriteLine("enter a no/string: ");
string a = Console.ReadLine();
for (int x = a.Length - 1; x >= 0; x--)
{
Console.Write(a[x]);
}
Console.WriteLine();
string bb = string.Empty;
Console.WriteLine("enter a no/string: ");
string b = Console.ReadLine();
for (int x = b.Length - 1; x >= 0; x--)
{
bb += b[x].ToString();
}
string[] bbb = new string[bb.Split(' ').Length];
for (int y = 0; y <= bb.Split(' ').Length-1; y++)
{
bbb[y] = bb.Split(' ')[y]+" ";
}
for (int z = bbb.Length - 1; z >= 0; z--)
{
Console.Write(bbb[z]);
}
Console.WriteLine();
Console.WriteLine("enter a name: ");
string n= Console.ReadLine();
if(bbb.Contains(n+" "))
Console.WriteLine("found");
else
Console.WriteLine("not found");
Console.ReadLine();
}
}
Interface overriding in console apps
interface Interface
{
void add(int x, int y);
}
class overrideinterface : Interface
{
public void add(int a, int b)
{
Console.WriteLine("add is: " + (a + b));
}
public static void Main()
{
overrideinterface ovri = new overrideinterface();
ovri.add(5, 5);
Console.ReadLine();
}
}
Note: A class can inherit any no.of interfaces separated by comma(,)
Virtual class overriding
class Virtual
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}
Abstract Overriding in console apps
abstract class Abstract
{
public abstract void mul(int x);
public void add(int x, int y)
{
Console.WriteLine("add is: "+ (x + y));
}
}
class overrideabstract : Abstract
{
public override void mul(int a)
{
Console.WriteLine("mul is: "+ a*a);
}
public static void Main()
{
overrideabstract ovra = new overrideabstract();
ovra.add(4, 5);
ovra.mul(3);
Console.ReadLine();
}
}
Prime No. in console apps
class Prime
{
static void Main(string[] args)
{
int j = 0;
int count = 0;
Console.WriteLine("Plz enter a number: ");
int n =int.Parse( Console.ReadLine());
Console.WriteLine("The Prime no's are:");
for (int i = 2; i <= n; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == j)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine();
Console.Write("Total Prime No's: "+count);
Console.ReadLine();
}
Constructor OverLoading in Console apps
class CACOverLoading
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}
Method OverLoading in console apps
class mOverLoading
{
int x=10;
public void show()
{
Console.WriteLine("default constructor");
Console.WriteLine(x + this.x);
}
public void show(int x)
{
Console.WriteLine("Parameterized constructors");
Console.WriteLine(x+" "+this.x);
}
public void show(string s, int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(s + " " + x);
}
public static void Main()
{
mOverLoading mol = new mOverLoading();
mol.show();
mol.show(123);
mol.show("satya", 456);
Console.ReadLine();
}
}
Operator OverLoading in console apps
class ClsComplex
{
int R, I;
public ClsComplex(int R, int I)
{
this.R = R;
this.I = I;
Console.WriteLine("{0} + {1}i", R, I);
}
public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
{
ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
return Num3;
}
}
class ClsOpOverLoad
{
static void Main(string[] args)
{
Console.Write("C1= ");
ClsComplex C1 = new ClsComplex(4, 5);
Console.Write("C2= ");
ClsComplex C2 = new ClsComplex(8, 2);
Console.Write("C3= ");
ClsComplex C3 = C1 + C2;
Console.Read();
}
}
30 July 2011
Reverse of a string or number in console apps
class Reverseofno
{
public static void Main()
{
Console.WriteLine("enter a no/string: ");
string a = Console.ReadLine();
for (int x = a.Length - 1; x >= 0; x--)
{
Console.Write(a[x]);
}
Console.WriteLine();
string bb = string.Empty;
Console.WriteLine("enter a no/string: ");
string b = Console.ReadLine();
for (int x = b.Length - 1; x >= 0; x--)
{
bb += b[x].ToString();
}
string[] bbb = new string[bb.Split(' ').Length];
for (int y = 0; y <= bb.Split(' ').Length-1; y++)
{
bbb[y] = bb.Split(' ')[y]+" ";
}
for (int z = bbb.Length - 1; z >= 0; z--)
{
Console.Write(bbb[z]);
}
Console.WriteLine();
Console.WriteLine("enter a name: ");
string n= Console.ReadLine();
if(bbb.Contains(n+" "))
Console.WriteLine("found");
else
Console.WriteLine("not found");
Console.ReadLine();
}
}
Interface overriding in console apps
interface Interface
{
void add(int x, int y);
}
class overrideinterface : Interface
{
public void add(int a, int b)
{
Console.WriteLine("add is: " + (a + b));
}
public static void Main()
{
overrideinterface ovri = new overrideinterface();
ovri.add(5, 5);
Console.ReadLine();
}
}
Abstract Overriding in console apps
abstract class Abstract
{
public abstract void mul(int x);
public void add(int x, int y)
{
Console.WriteLine("add is: "+(x + y));
}
}
class overrideabstract : Abstract
{
public override void mul(int a)
{
Console.WriteLine("mul is: "+a*a);
}
public static void Main()
{
overrideabstract ovra = new overrideabstract();
ovra.add(4, 5);
ovra.mul(3);
Console.ReadLine();
}
}
Virtual class overriding
class Virtual
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}
Prime No. in console apps
class Prime
{
static void Main(string[] args)
{
int j = 0;
int count = 0;
Console.WriteLine("Plz enter a number: ");
int n =int.Parse( Console.ReadLine());
Console.WriteLine("The Prime no's are:");
for (int i = 2; i <= n; i++)
{
for (j = 2; j <= i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i == j)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine();
Console.Write("Total Prime No's: "+count);
Console.ReadLine();
}
Constructor OverLoading in Console apps
class CACOverLoading
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}
Subscribe to:
Posts (Atom)