0

i have a project, and part of it asks the user to input the ID of the patient to show his/her details This is my code

        sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=hospital database.accdb";
        dbConn = new OleDbConnection(sConnection);
        dbConn.Open();
        sql = "SELECT * FROM Patients";
        dbCmd = new OleDbCommand();
        dbCmd.CommandText = sql;
        dbCmd.Connection = dbConn;

        dbReader = dbCmd.ExecuteReader();


        listBox1.Items.Clear();




        if (dbReader.HasRows)
        {
            while (dbReader.Read())
            {

                if (dbReader["PatientID"] != DBNull.Value)
                {

                    int anInteger;
                    anInteger = Convert.ToInt32(textBox7.Text);
                    anInteger = int.Parse(textBox7.Text);

                    if (anInteger == 101)
                    {

                    }

                }


            }
        }

in the IF statement, i dont know what to write in it, to display on the row of the patient with this ID only

Please Help!!

1
  • Why don't you just query for the one patient info that you want instead of querying all of them? Commented Dec 8, 2013 at 17:44

4 Answers 4

1

Instead of selecting all rows, it is much more efficient to filter the one row you are looking for using a parameter and modifying your SQL statement as follows.

    sql = "SELECT * FROM Patients WHERE PatientID = [pID]";
    dbCmd = new OleDbCommand();
    dbCmd.CommandText = sql;
    dbCmd.Connection = dbConn;
    dbcmd.Parameters.AddWithValue("pID", 101);
    dbReader = dbCmd.ExecuteReader();

I would also suggest looking into the "using" clause. Here's a SO example.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 with two very minor comments: (1) Missing ) on AddWithValue. (2) With ACE/Jet OLEDB it is more common to use ? as the parameter placeholders/names since the provider ignores the actual names and only uses the order in which they are specified.
Thanks Gord. I guess my usage of brackets is a left over from the VBA days.
1
  sql = "SELECT Count(*) FROM Patients WHERE PatientID = @PID";
    dbCmd = new OleDbCommand();
    dbCmd.CommandText = sql;
    dbCmd.Connection = dbConn;
    dbcmd.Parameters.AddWithValue("@PID", 101);
    Int32 Cnt = dbCmd.ExecuteScalar();
if ( Cnt > 0)
{
// Do Something
}
else  { // Do something} 

Comments

0

you have to use a variable to be sure that your ID was found and and break; to exit your loop once it was found

if (dbReader.HasRows)
        {
           bool found  = false;  
            while (dbReader.Read())
            {

                if (dbReader["PatientID"] != DBNull.Value)
                {

                    int anInteger;
                    anInteger = Convert.ToInt32(textBox7.Text);
                    anInteger = int.Parse(textBox7.Text);

                    if (anInteger == 101)
                    {
                        found = true ;  Break; 

                    }

                }


            }
        }

Comments

0
    int anInteger = Convert.ToInt32(textBox7.Text);        
    sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=hospital database.accdb";
    dbConn = new OleDbConnection(sConnection);
    dbConn.Open();
    sql = "SELECT * FROM Patients where PatientID=@PatientID";
    dbCmd = new OleDbCommand();
    dbCmd.CommandText = sql;
    dbCmd.Parameters.AddWithValue("@PatientID",anInteger);
    dbCmd.Connection = dbConn;

    dbReader = dbCmd.ExecuteReader();


    listBox1.Items.Clear();
    while (dbReader.Read())
    {
       //now display the reader values here : sample
       //TextBox1.Text=dbReader["name"].ToString();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.