开发者

Search not Working

am creating a search button where a clients ID or his SURNAME can both be used in searching for the clients details. when the surname is used for the search, the clients ID, Surname and Othername suolud be displayed on a different form1 The ID is working correctly but the SURNAME is dis playing 'record not found' pls help me

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() == "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 开发者_如何学C= person.idtype.ToString();
                    switch (iType)
                    {
                        case "Bank ID Card":
                            _with3.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();



                    btnPUpdate.Enabled = true;
                    this.txtSclientid.Text = this.txtClientid.Text;
                    this.txtSclientid.ReadOnly = true;

                }
                else
                {
                    MessageBox.Show("Record not found");
                }




                if (Remitt.searchremitDetails(txtClientid.Text))
                {

                    var _with4 = this;
                    txtClientid.Text = Remitt.ID.ToString();
                    _with4.txtSecretcode.Text = Remitt.Scode.ToString();

                    string sendCountry = cboSendingcountry.Text;
                    switch (sendCountry)
                    {
                        case "Germany":
                            _with4.cboSendingcountry.Text = "Germany";
                            break;
                        case "Ghana":
                            _with4.cboSendingcountry.Text = "Ghana";
                            break;
                    }

                    string sendBranch = cboSendingbranch.Text;
                    switch (sendBranch)
                    {
                        case "Accra":
                            _with4.cboSendingbranch.Text = "Accra";
                            break;
                        case "Dotmond":
                            _with4.cboSendingbranch.Text = "Dotmond";
                            break;
                        case "Dusseldurf":
                            _with4.cboSendingbranch.Text = "Dusseldurf";
                            break;
                        case "Koln":
                            _with4.cboSendingbranch.Text = "Koln";
                            break;
                        case "Kumasi":
                            _with4.cboSendingbranch.Text = "Kumasi";
                            break;
                    }

                    string sendCurrency = cboScurrency.Text;
                    switch (sendCurrency)
                    {
                        case "Cedis":
                            _with4.cboScurrency.Text = "Cedis";
                            break;
                        case "Euros":
                            _with4.cboScurrency.Text = "Euros";
                            break;
                    }

                    _with4.txtAmountsent.Text = Remitt.Totalamount.ToString();
                    _with4.txtCommission.Text = Remitt.Tax.ToString();

                    string remitStatus = cboStatus.Text;
                    switch (remitStatus)
                    {
                        case "Not Sent":
                            _with4.cboStatus.Text = "Not Sent";
                            break;
                        case "Sent":
                            _with4.cboStatus.Text = "Sent";
                            break;
                    }

                    _with4.dtpDatesent.Value = Remitt.Sentdate;
                    _with4.txtReceiversname.Text = Remitt.Nameofreceiver.ToString();

                    string receiveCountry = cboReceivingcountry.Text;
                    switch (receiveCountry)
                    {
                        case "Germany":
                            _with4.cboReceivingcountry.Text = "Germany";
                            break;
                        case "Ghana":
                            _with4.cboReceivingcountry.Text = "Ghana";
                            break;
                    }

                    string receiveBranch = cboReceivingbranch.Text;
                    switch (receiveBranch)
                    {

                        case "Accra":
                            _with4.cboReceivingbranch.Text = "Accra";
                            break;
                        case "Dotmond":
                            _with4.cboReceivingbranch.Text = "Dotmond";
                            break;
                        case "Dusseldurf":
                            _with4.cboReceivingbranch.Text = "Dusseldurf";
                            break;
                        case "Koln":
                            _with4.cboReceivingbranch.Text = "Koln";
                            break;
                        case "Kumasi":
                            _with4.cboReceivingbranch.Text = "Kumasi";
                            break;
                    }


                    string receiveCurrency = cboRcurrency.Text;
                    switch (receiveCurrency)
                    {
                        case "Cedis":
                            _with4.cboRcurrency.Text = "Cedis";
                            break;
                        case "Euros":
                            _with4.cboRcurrency.Text = "Euros";
                            break;
                    }


                    _with4.txtSremarks.Text = Remitt.Remarksremit.ToString();

                    lklRsearch.Enabled = false;
                    txtSclientid.ReadOnly = true;
                    btnRUpdate.Enabled = true;

                }

                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();
            }

        }




codes in the 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) 
            {
                Module1 clientid = new Module1();

                Fom1 frm = new Fom1(personname);
                frm.ShowDialog();
                personid = clientid.ToString();
            }

        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()) 
        {
            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();



    }


as far as I can see you don't translate the personname into a clientid (or is this done in the unknown-to-us Module1?).

So you need a way to translate the personname into a personid or you have to use different queries for those cases.

And: why are there two queries where the second overrides the first?

Aside from this: please don't create SQL-Queries with string-concatination (search for SQL-injection)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜