Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How to display employee information in form2 by specifying emp id in form1?

$
0
0

This is my form1 which I designed. When I type emp id in form1's textbox and click on the search button it shows form2.

In this second form I need to carry all the details corresponding to emp id and should display details into corresponding textboxes.

I have created emp table in SQL Server...I would like to pull the employee details from the database based on emp id. This is my code:

private void btnsearch_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(tbempid.Text);
            f2.Show();
            SqlConnection con = new SqlConnection("Data Source=RAJIM-PC;Initial Catalog=Practicing;User ID=sa;Password=RajiSha");

            try
            {
                con.Open();
                SqlCommand com = new SqlCommand("SELECT eid,emp_name,mobile_no FROM emp WHERE ID='" + tbempid.Text.Trim() + "'", con);
                com.CommandType = CommandType.Text;
                DataTable dtb = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(com);
                da.Fill(dtb);

                if (dtb.Rows.Count > 0)
                {

                    Form2.txtempid.Text= dtb.Rows[0]["eid"].ToString();
                    Form2.txtempname.Text = dtb.Rows[0]["emp_name"].ToString();
                    Form2.txtmbno.Text= dtb.Rows[0]["mobile_no"].ToString();
                }

                FormCollection fc = System.Windows.Forms.Application.OpenForms;

                foreach (Form f in fc)
                {
                    if (f.Name == "Form2")
                    {
                        f.Update();
                    }
                }

            }
            catch (SqlException sql) 
            { 
                System.Windows.Forms.MessageBox.Show(sql.Message); 
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                    con.Dispose();
                }
            }

        }

form 2:
 public partial class Form2 : Form
    {
        private string p;

        public Form2(string txtempid, string txtempname, string txtmbno)
        {
            InitializeComponent();
             string tbempid =txtempid;
            string tbempname = txtempname;
           string tbmbno = txtmbno;
        }

        public Form2(string p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }

}


Thanks & Regards RAJENDRAN M


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>