hi...
i have one function in my c # application.
this is to add one field from the table to the combo box.

but i have to use this function in many forms ,
instead of writing this function in all the forms is there any other way where i can write the function only once and i can call it anywhere ?
if it is so can u tell me with example ...
pls....

public void PopulateId()
        {
            ds.Clear();
            string qry = "select distinct(PID) from tbl_Registraion order by PID";
            da = new SqlDataAdapter(qry, con);
            da.Fill(ds, "tbl_Registraion");
            dt = ds.Tables["tbl_Registraion"];
            combobox1.Items.Clear();
            foreach (DataRow tdr in dt.Rows)
            {
                combobox1.Items.Add(tdr["PID"]);
            }
        }

hi,

You can make inside a Class file and call the method whenever needed...

hi Kamilacbe,

i dont know how to create a class with this function and how to call this in my program...

thats y i ve asked u a example...

hi ,

hopw this sample will help you...

class connect{
connection string comes here......

public  void list_DataView(string strSQL, ListView myList)
        {
            myList.Items.Clear();
            try
            {
                OleDbCommand cmd = new OleDbCommand(strSQL, cn);
                cn.Open();
                OleDbDataReader  dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ListViewItem lItem = new ListViewItem(dr[0].ToString());
                    for (int i = 1; i <= dr.FieldCount - 1; i++)
                    {
                        lItem.SubItems.Add(dr[i].ToString());
                    }
                    myList.Items.Add(lItem);
                    int rCount = myList.Items.Count;
                    if (rCount % 2 == 1)
                    {
                        //lItem.BackColor = Color.WhiteSmoke;
                        lItem.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(208)))), ((int)(((byte)(234)))), ((int)(((byte)(189))))); 
                    }
                    else
                    {
                        lItem.BackColor = Color.White;
                    }
                }
                cn.Close();
                cmd.Dispose();
                dr.Close();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

        }

}


.......
then in your forms you can call class name.methodname .......
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.