logs of my work and digital life
Devlopment
Design Patterns: Iterator
Jul 8th
Hi Guys,
I am reading more on design patterns from this article after little theory session here is working code. Key point was all of the collection classes in the System.Collections namespace, as well as arrays, implement IEnumerable and can therefore be iterated over.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class IteratorPattern
{
static void Main(string[] args)
{
int[] values = new int[] { 1, 2, 3, 4, 5 };
IEnumerator<int> e = ((IEnumerable<int>)values).GetEnumerator();
while (e.MoveNext())
{
Console.Write(e.Current.ToString() + " ");
}
Console.ReadKey();
}
}
}
Custom Event for Classes
Jul 6th
I was reading how to write events for your custom classes so after reading a little I thought give it a shot and created this sample code. Below code is using Product class with event and delegate for event handling and other class named Test is using this class and manipulate product class data. If you change the Name of the product event will fire.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// Product Class
/// </summary>
public class Product
{
public delegate void NameChangeEventHandler(int a);
public event NameChangeEventHandler NameChanged;
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
if (NameChanged != null)
{
NameChanged(5);
}
}
}
public Product(string name)
{
Name = name;
}
public void PrintName()
{
Console.WriteLine("ProductName={0}", Name);
}
}
/// <summary>
/// Test Class
/// </summary>
public class Test
{
public void ChangeDetected(int a)
{
Console.WriteLine("change found,argument passed is {0}",a);
}
public void TestProduct()
{
Product p = new Product("najam awan");
p.PrintName();
p.NameChanged += ChangeDetected;
p.Name = "najaf";
p.PrintName();
Console.ReadKey();
}
}
static void Main(string[] args)
{
Test t = new Test();
t.TestProduct();
}
}
}
JavaScript, 5 ways to call a function
Jun 29th
Hello
I have came across to this wonderful blog post that show in details how to call javascript functions using different ways its a must read article even if you are not good in javascript please read article at
http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
Random rows selection from a database table
Jan 29th
hi everyone,
today I discovered a nice tip from a blog I never knew selecting random rows were that easy with sqlserver 2000 and above. I have a country table that has four countries and from 4 I am selecting random 2.
SELECT TOP 2 * FROM country ORDER By NEWID()
select * from country
First Result
3 SaudiArabia
4 France
Second Result
1 UAE
4 France
Third Result
1 UAE
3 SaudiArabia
All Countries
1 UAE
2 Pakistan
3 SaudiArabia
4 France
Amazing right??
Sql Data Srouce Control Deleting Data From Database
Jul 17th
Hey guyz today i will show you how to fire delete query by using our beloved control sqldatasource.
Here is the code
SqlDataSource myDbSource22 = new SqlDataSource();
myDbSource22.ConnectionString = ConfigurationManager.ConnectionStrings["lalConnectionString"].ConnectionString;
myDbSource22.ProviderName = “System.Data.SqlClient”;
myDbSource22.DeleteCommand = “delete from ProductsGroups where pid = ” + productId;
myDbSource22.Delete();
All it does it deletes all the rows for a given productID from table productgroups. Simple right
Sql Data Srouce Control Inserting Data Into Database
Jul 17th
hey guyz how are you ??
Me back with my sqldatasource series. Today i will show you how to fire insert query using sqldatasource control into code behind i am using c#.
Below is the sample code UserID is the parameter passing into a function as an argument.
DateTime Entrydate = DateTime.Today;
SqlDataSource myDbSource = new SqlDataSource();
myDbSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
myDbSource.InsertCommand = “insert into tbl_invoices (user,Entrydate) values (” + UserID + “,’” + Entrydate + “‘)”;
myDbSource.ProviderName = “System.Data.SqlClient”;
myDbSource.Insert();
myDbSource.SelectCommand = “SELECT IDENT_CURRENT(‘tbl_invoices’)”;
DataView dv = (DataView)myDbSource.Select(new DataSourceSelectArguments());
DataTable dt = dv.Table;
string invNumber = dt.Rows[0][0].ToString();
return invNumber.ToString();
//return string.Empty;
Code above is very simple what it does simple take UserID as an argument and then insert a new record into tbl_invoices table and then after it i am firing a select statement that returns the latest ID into tbl_invoices table. SELECT IDENT_CURRENT(‘table name’) return the id of the last record inserted into the table.
Happy coding bye
Sql Data Srouce Control Fetch data from database
Jul 16th
Hi in asp.net 2.0 this new datasource control has been added and i liked it alot and i used it on many places so i thought it would be nice if I share my knowledge with all my readers so first of all i would just like to show you how to retrieve some data from your database into C# code behind file.
string userID = string.Empty;
string UserName=”najam”;
SqlDataSource myDbSource = new SqlDataSource();
myDbSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
myDbSource.SelectCommand = “select userId from Users where userName=’” + UserName + “‘”;
myDbSource.ProviderName = “System.Data.SqlClient”;
DataView dv = (DataView)myDbSource.Select(new DataSourceSelectArguments());
DataTable dt = dv.Table;
if (dt.Rows.Count > 0)
{
userID = dt.Rows[0][0].ToString();
}
Above code will return user id if username supplied. You will see no data adapters or command object.
If you like instead of creating sql datasrouce control in code behind just drag and drop it onto ur aspx page and then in your code behind reference it that will shorten the line of code for fetching data from your database.
Read SMTP settings from asp.net web.config
Jul 16th
hi guyz
Sorry for writing after such a long time i guess i am very busy now a days so for a project i needed to read my smtp email settings defined in my web.config file so after some efforts i am able to read these settings direct from my system .net section.
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup) config.GetSectionGroup("system.net/mailSettings");
Response.Write("<br>Username="+settings.Smtp.Network.UserName);
Response.Write("<br>Password=" + settings.Smtp.Network.Password);
Response.Write("<br>host=" + settings.Smtp.Network.Host);
Response.Write("<br>port=" + settings.Smtp.Network.Port);
Response.Write("<br>from=" + settings.Smtp.From);
How to get directory name of the page currently displaying
Nov 6th
If you want to know only the directory name not the full path of the page that is currently displaying you can use this code
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Directory.Name.ToString();
Response.Write(“<br><br>Directory name===” + sRet + “<br><br>”);
Happy Coding
Najam Sikander Awan
How to get pagename of itself
Nov 6th
If you want to display the filename or page name of the current displaying page just use these two lines and it will show only the filename like default.aspx, najam.aspx or indexas.aspx no matter it these pages are located deep into your site structure.
string pagename = System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]); Response.Write(pagename);
Enjoy coding
Najam Sikander Awan