Sunday, June 17, 2012

How to get NIC Address or Machine Address in C# Programming using .NET technology

Step1: Add a refrence(System.Management) in Refrences Folder

Step2: use these "name space system" in code windows

using System.Management;
using System.Runtime.Remoting;
using System.Security.Permissions;
using System.Net.NetworkInformation;
using System.Collections;
using System.ComponentModel;

Step3: make a private function as GetMacAddress() and call it on an event;

private string GetMacAddressNew()
        {
            string macAddresses = "";

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.OperationalStatus == OperationalStatus.Up)
                {
                    macAddresses += nic.GetPhysicalAddress().ToString();
                    break;
                }
            }
            return macAddresses;
        }
That's it all.

Thursday, June 7, 2012

Paging in Data List C# with .NET Code

On Form View Take
"DataList" and Name it as DataList1
"SqlDataSource" and Named it  as SqlDataSource1
Take a Label and named it as "lblCurrentPage"
take 2 button one for next and one for previous

With SqlDataSource
configure it with database and table

With grid view
set the "RepeatColumn" property and height width property

On Code window
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class DataListPaging : System.Web.UI.Page
{
    static int CurrentPage;
    protected void Page_Load(object sender, EventArgs e)
    {
        items();
    }
    protected void items()
    {
        PagedDataSource objDs = new PagedDataSource();
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        objDs.DataSource = dv;
        objDs.AllowPaging = true;
        objDs.PageSize = 6;
        objDs.CurrentPageIndex = CurrentPage;
        lblCurrentPage.Text = "Page:" + (CurrentPage + 1).ToString() + " Of " + objDs.PageCount.ToString();
        back1.Enabled = !objDs.IsFirstPage;
        next1.Enabled = !objDs.IsLastPage;
        DataList2.DataSource = objDs;
        DataList2.DataBind();

    }

    protected void next1_Click(object sender, EventArgs e)
    {
        try
        {

            CurrentPage += 1;
            items();
        }
        catch (Exception)
        {        }
    }
    protected void back1_Click(object sender, EventArgs e)
    {
        try
        {
            CurrentPage -= 1;
            items();
        }
        catch (Exception)
        {    }
    }
  
}



Thursday, May 3, 2012

Making a class in C# ASP.net for SQL univarsal connection

class name : Connection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; //for sql data connection;
using System.Data;  // for data set data table ;
using System.Windows.Forms; // for taking a refrence of controls in a class file. like combobox;

//this class is make for universal sql connnection in the project;
namespace WFA2
{
    class Connection
    {
        public static SqlConnection conn()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;User ID=sa");
            return con;
        }
        public static DataSet getData(String str)  // this connection architecture .... no need to open connection // to display data
        {
          
                SqlConnection con = Connection.conn();
                SqlDataAdapter da = new SqlDataAdapter(str, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
        }
        public static void AllPer(String str)  // this is not this connection architecture // to inserting data
        {
            SqlConnection con = Connection.conn();
            SqlCommand cmd = new SqlCommand(str,con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}


Using class for accessing connection

// this will show the data from table in grid viewer
private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = Connection.getData("Select * from infoCustomer");
            dataGridView1.DataSource = ds.Tables[0];
        }

Note : Take a button named it button1 and take a dataGridView1 control from toolbox;

// this will insert the data in table

private void button2_Click(object sender, EventArgs e)
        {
            Connection.AllPer("insert into infoCustomer values('" + comboBox1.Text + "','"+txtAge .Text +"','"+txtSalary .Text+"')");
            MessageBox.Show("Record Inserted");
        }



Adding the items from data table into combobox on run time


Make a class for FillBox method

public static void FillBox(ref ComboBox cmb, string str) //using System.Windows.Forms; here
        {
            DataSet ds = Connection.getData(str);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                cmb.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
            }
        }


On Form Load Code
private void Form1_Load(object sender, EventArgs e)
        {
            Connection.FillBox(ref comboBox1, "select name from infoCustomer");
        }

Note: "Connection.FillBox " is used regarding to Connection.cs class file...you can change the word "Connection" if your class file name is different.