Output Screen Shots
![]() |
| Fig1:Scn Shot of Sample DB |
![]() |
| Fig2: Output on Successful search |
![]() |
| Fig3: Output on Unsuccessful search |
Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WinConnectedApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGetData_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
// Create a connection object
con.ConnectionString = @"Data source=USERS35\SQLExpress;initial catalog = training1;integrated security =true";
SqlCommand cmd = new SqlCommand();
cmd.Connection=con;
cmd.CommandText="select empid,empfname,emplname,emploc,empsal from employee";
//0 1 2 3 4
try
{
con.Open();//establishes phy link between App and DB
SqlDataReader dr=cmd.ExecuteReader();
while (dr.Read())//Reads one row from employee
{
txtEmpId.Text = dr["empid"].ToString();
txtFirstName.Text = dr["empfname"].ToString();
txtLastName.Text = dr["emplname"].ToString();
txtloc.Text = dr["emploc"].ToString();
txtSal.Text = dr["empsal"].ToString();
}
dr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State==ConnectionState.Open)
{
con.Close();
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
string eid = txtEmpId.Text;
SqlConnection con = new SqlConnection();
// Create a connection object
con.ConnectionString = @"Data source=USERS35\SQLExpress;initial catalog = training1;integrated security =true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select empid,empfname,emplname,emploc,empsal from employee where empid=" + eid;
//0 1 2 3 4
try
{
con.Open();//establishes phy link between App and DB
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();// Reads one row
if(dr.HasRows)
{
txtEmpId.Text = dr["empid"].ToString();
txtFirstName.Text = dr["empfname"].ToString();
txtLastName.Text = dr["emplname"].ToString();
txtloc.Text = dr["emploc"].ToString();
txtSal.Text = dr["empsal"].ToString();
}
else
MessageBox.Show("No matching records found");
dr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
}


