创建搜索按钮

时间:2011-08-04 07:32:14

标签: c# asp.net webforms

我想在我的c#表单上创建一个按钮,当我输入客户端ID或姓氏时,按下搜索按钮,它应该在c#表单上显示所有信息。

这不起作用,单击按钮时,消息框显示“clientid”。

课程中的代码

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

背后的代码

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

    }

1 个答案:

答案 0 :(得分:0)

数据库中是Client_id还是'Clientid'?在您的查询中,您提到了:

sqlQuery = "select * from tblspersonaldetails where client_id='" + personid + "'";    

几行,你有:

client_id = dr["clientid"].ToString();

您应该将查询更改为Clientid或将代码更改为dr[client_id](具体取决于数据库中的列名称。