Friday, 27 April 2012

Auto Genrated ID



Use this code follow following setps.
1)Create class file and past this code
2)Past this code in button click event
     textbox1.text =convert.tostring(funcation.id("select max(id) from demotable "));



using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace POS
{
    class funcation //............>( class name)
    {
 
        //autoid
        public static string id(string strSQL) //..........> (pass parameter to funcation id (id is funcation name))
        {
            string pi_id = null;

            try
            {

                String userqry = "";
                userqry = strSQL;
                int i = 0;
                SqlCommand cmd = new SqlCommand(userqry, datastore.Connection());
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        pi_id = dr[0].ToString();
                    }
                    if (pi_id.Equals(""))
                    {
                        pi_id = "001";
                    }
                    else
                    {
                        i = Convert.ToInt32(pi_id);
                        i++;
                        pi_id = "" + i;

                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

            return pi_id.ToString().TrimEnd();..........> (return value)
        }

    }

}