creating a search button
I want to create a button on my c# form and when I type in a clients id or surname and I press the search button, it should display all his info on the c# form.
This is not working, a message box displays 'clientid' when the button is clicked.
Code in Class
public bool searchpersonDetails(string personid, string personname)
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.ConnectionString = "Data Source=pc101;Initial Catalog=REMITTANCE;User ID=sa;Password=mike";
conn.Open();
if (personid == null)
{
Fom1 frm = new Fom1();
frm.ShowDialog();
personid = client_id;
}
SqlCommand cmd = new SqlCommand();
string sqlQuery = null;
//sqlQuery = "select *,floor(datediff(curdate(),dateofbirth)/365) AS AGE from tblspersonaldetails where client_id='" + personid + "'";
sqlQuery = "select * from tblspersonaldetails where client_id='" + personid + "'";
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
client_id = dr["clientid"].ToString();
surname = dr["surname"].ToString();
othername = dr["othername"].ToString();
gender = dr["gender"].ToString();
date_ofbirth = (DateTime) dr["dateofbirth"];
nationality = dr["nationality"].ToString();
//age = dr["Age"];
residential_address = dr["residentialaddress"].ToString();
postal_address = dr["postaladdress"].ToString();
contact_number = dr["telephonenumber"].ToString();
marital_status = dr["maritalstatus"].ToString();
spouse_name = dr["spousename"].ToString();
email = dr["email"].ToString();
occupation = dr["occupation"].ToString();
typeof_id = dr["typeofid"].ToString();
id_number = dr["idnumber"].ToString();
id_expirydate = (DateTime) dr["idexpirydate"];
remarks = dr["remarks"].ToString();
picture = dr["picture"].ToString();
return true;
cmd.CommandText = null;
}
else
{
return false;
}
conn.Close();
}
And codes behind
private void lklSearch_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
conn.ConnectionString = "Data Source=pc101;Initial Catalog=REMITTANCE;User ID=sa;Password=mike";
conn.Open();
try
{
Personal person = new Personal();
if (person.searchpersonDetails(txtClientid.Text, txtSurname.Text))
{
var
_with3 = this;
txtClientid.Text = person.ID.ToString();
_with3.txtSurname.Text = person.Sname.ToString();
_with3.txtOthername.Text = person.Oname.ToString();
if (person.sex.ToString() == "Male")
{
optMale.Checked = true;
}
else
{
optFemale.Checked = true;
}
_with3.dtpDob.Value = person.BirthDate;
_with3.txtNationality.Text = person.country.ToString();
_with3.txtResidentialaddress.Text = person.addressResidential.ToString();
_with3.txtPostaladdress.Text = person.AddressPostal.ToString();
_with3.txtContactnumber.Text = person.NumberContact.ToString();
string mstatus = person.statusMarital.ToString();
switch (mstatus)
{
case "Single":
this.cboMaritalstatus.Text = "Single";
break;
case "Married":
_with3.cboMaritalstatus.Text = "Married";
break;
case "Widow(er)":
_with3.cboMaritalstatus.Text = "Widow(er)";
break;
case "Divorce":
_with3.cboMaritalstatus.Text = "Divorce";
break;
}
_with3.txtSpousename.Text = person.nameSpouse.ToString();
_with3.txtEmail.Text = person.mail.ToString();
_with3.txtOccupation.Text = person.Work.ToString();
string iType = person.idtype.ToString();
switch (iType)
{
case "Bank ID Card":
this.cboIdtype.Text = "Bank ID Card";
break;
case "Driver Licence":
_with3.cboIdtype.Text = "Driver Licence";
break;
case "Passport":
_with3.cboIdtype.Text = "Passport";
break;
case "National Identification":
_with3.cboIdtype.Text = "National Identification";
break;
case "NHIS":
_with3.cboIdtype.Text = "NHIS";
break;
case "SSNIT":
_with3.cboIdtype.Text = "SSNIT";
break;
case "Voters ID":
_with3.cboIdtype.Text = "Voters ID";
开发者_如何学Python break;
}
_with3.txtIdnumber.Text = person.numberID.ToString();
_with3.dtpExpiringdate.Value = person.expirydateID;
_with3.txtRemarks.Text = person.myremarks.ToString();
btnPUpdate.Enabled = true;
/// this.txtChurchID.Text = this.txtID.Text;
///this.txtChurchID.ReadOnly = true;
person.searchpersonDetails(txtClientid.Text = "", txtSurname.Text = "");
}
else
{
MessageBox.Show("Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Close data reader object and database connection
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
Is it Client_id
or 'Clientid' in your database? In your query, you mentioned:
sqlQuery = "select * from tblspersonaldetails where client_id='" + personid + "'";
A few lines down, and you have:
client_id = dr["clientid"].ToString();
You should either change query to Clientid
or change the code to dr[client_id]
(depending on what the column name is in your database.
精彩评论