I'm looking for help on how to display results when searching for clients informations with their “surname” on my application
I want to display a new form with multiple results in a
List View (clientid, surname, othername) and allow the user to select the one they are looking for
codes in class
public bool searchpersonDetails(string personname)
{
if (surname == string.Empty)
{
Fom1 frm = new Fom1(personname);
frm.Show();
personname = surname.ToString();
}
SqlCommand cmd = new SqlCommand();
string sqlQuery = null;
sqlQuery = "select client_id, surname, othername from tblspersonaldetails where surname like '%" + personname + "%';";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
client_id = dr["client_id"].ToString();
surname = dr["surname"].ToString();
othername = dr["othername"].ToString();
return true;
}
else
{
return false;
}
}
}
}
codes behind button click
private void button1_Click(object sender, EventArgs e)
{
try
{
getid getidentity = new getid();
if (getidentity.searchpersonDetails(txtSurname.Text))
{
var
_with9 = this;
_with9.txtSurname.Text = getidentity.Sname.ToString();
}
else
{
MessageBox.Show("Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
}
}
codes behind form
public partial class Fom1 : Form
{
SqlConnection conn = new SqlConnection();
public Fom1(string personname)
{
InitializeComponent();
SqlCommand cmd = new SqlCommand();
string sqlQuery = null;
sqlQuery = "select * from tblspersonaldetails where surname like '" + personname + "%" + "' order by surname,othername asc";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
while (dr.Read())
{
ListViewItem lst = default(ListViewItem);
lst = this.listView1.Items.Add(dr["client_id"].ToString());
lst.SubItems.Add(dr["surname"].ToString());
lst.SubItems.Add(dr["othername"].ToString());
}
cmd.Dispose();
}
BUT IT IS STILL NOT DISPLAYING ON THE FORM PLease HELP ME OUT