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

  • Thread starter Thread starter Rajendran M
  • Start date Start date
R

Rajendran M

Guest
This is my form1 which I designed. When I type emp id in form1s 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:

581938
581939
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

Continue reading...
 
Back
Top