Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i made physiotherapy management system and i have two tables patient_mstr and old_patient. i give contact_no as primary key and foreign key. i want to search contact no and display particular fields records from table. the code as given below.

What I have tried:

private void txtscrcno_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    OleDbCommand cmd = new OleDbCommand("select patient_mstr.Sr_no,patient_mstr.P_name,patient_mstr.M_name,patient_mstr.L_name,old_patient.E_date,patient_mstr.Address,old_patient.Pain,old_patient.Total_charge,old_patient.Total_paid from patient_mstr join on old_patient where patient_mstr.Contact_no=old_patient.Contact_no and Contact_no=@Contact_no", cn);
                    cmd.Parameters.Add(new OleDbParameter("@Contact_no", txtscrcno.Text));
                    cn.Open();
                    if (cmd.ExecuteNonQuery() > 1)
                    {
                        //OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        da.Fill(ds, "patient_mstr");
                        dgvinfo.DataSource = ds;
                        dgvinfo.DataMember = "patient_mstr";
                        cn.Close();
                    }
                    else
                    {
                        MessageBox.Show("There is no record found.","No record",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception j)
            {
                MessageBox.Show(j.Message);
            }
        }
Posted
Updated 15-Sep-16 17:58pm
v2
Comments
TheSQLServerGuy 15-Sep-16 23:47pm    
please format the code for better readability
Member 11572517 15-Sep-16 23:58pm    
sorry sir, i don't know how to format it, so i just make this.
TheSQLServerGuy 16-Sep-16 0:08am    
somebody already did the formatting

Try something like following-
select patient_mstr.Sr_no,patient_mstr.P_name,patient_mstr.M_name,patient_mstr.L_name,old_patient.E_date,patient_mstr.Address,old_patient.Pain,old_patient.Total_charge,old_patient.Total_paid 
from patient_mstr
left join old_patient on patient_mstr.Contact_no=old_patient.Contact_no 
where old_patient.Contact_no=@Contact_no


Please let me know, if it doesn't help :)
 
Share this answer
 
v2
Comments
Member 11572517 15-Sep-16 23:59pm    
i try sir, and thank u sir.
TheSQLServerGuy 16-Sep-16 0:00am    
should it be left or inner join?
Member 11572517 16-Sep-16 0:06am    
sir, i want to left join, but, in datagrid view i don't define any field, it's just blank. so error is generate that there is no record. may be this reason the error is generate.
You can use inner join
select patient_mstr.Sr_no,patient_mstr.P_name,patient_mstr.M_name,patient_mstr.L_name,old_patient.E_date,patient_mstr.Address,old_patient.Pain,old_patient.Total_charge,old_patient.Total_paid 
from patient_mstr p inner join old_patient op on p.Contact_no=op.Contact_no 
where p.Contact_no=@Contact_no

For more details about INNER JOIN check following-
SQL INNER JOIN Keyword[^]
 
Share this answer
 
v2
Comments
Member 11572517 16-Sep-16 0:07am    
sir, i try it, because i want to left join.thank u sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900