i have a c# form which contains textbox, combobox, and radio buttons, i am able to save data
from the form to my database. but am having problems rerieving data from the sql database
onto my combobox and radio buttons. the data is not displaying in the combobox and
radio button is always returning Male
codes in my 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 (client_id != null)
{
Fom1 frm = new Fom1(personname);
frm.ShowDialog();
}
SqlCommand cmd = new SqlCommand();
string sqlQuery = null;
sqlQuery = "select *,floor(datediff(getdate(),date_ofbirth)/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())
{
frmPersonal person = new frmPersonal();
client_id = dr["client_id"].ToString();
surname = dr["surname"].ToString();
othername = dr["othername"].ToString();
gender = dr["gender"].ToString();
date_ofbirth = (DateTime)dr["date_ofbirth"];
nationality = dr["nationality"].ToString();
age = dr["age"].ToString();
residential_address = dr["residential_address"].ToString();
postal_address = dr["postal_address"].ToString();
contact_number = dr["contact_number"].ToString();
marital_status = dr["marital_status"].ToString();
spouse_name = dr["spouse_name"].ToString();
email = dr["email"].ToString();
occupation = dr["occupation"].ToString();
typeof_id = dr["typeof_id"].ToString();
id_number = dr["id_number"].ToString();
id_expirydate = (DateTime)dr["id_expirydate"];
remarks = dr["remarks"].ToString();
picture = dr["picture"].ToString();
return true;
//cmd.CommandText = null;
}
else
{
return false;
}
conn.Close();
}
codes behind the form is
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();
Remittances Remitt = new Remittances();
if (person.searchpersonDetails(txtClientid.Text, txtSurname.Text))
{
var
_with3 = this;
_with3.txtClientid.Text = person.ID.ToString();
_with3.txtSurname.Text = person.Sname.ToString();
_with3.txtOthername.Text = person.Oname.ToString();
if (person.sex.ToString() == "Female")
{
optFemale.Checked = true;
optMale.Checked = false;
}
else
{
optMale.Checked = true;
optFemale.Checked = false;
}
_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";
break;
}
_with3.txtIdnumber.Text = person.numberID.ToString();
_with3.dtpExpiringdate.Value = person.expirydateID;
_with3.txtRemarks.Text = person.myremarks.ToString();
else
{
MessageBox.Show("Remittance Details Record not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//return new SqlCommand();
}
finally
{
// Close data reader object and database connection
if (conn.State == ConnectionState.Open)
conn.Close();
}
}