Question

I am kinda new to C# and not sure what am I doing wrong. Tried to find a solution but failed. Thanks in advance.

When I do not use parameter "select * from Καρτέλα_Ασθενή"; the form will get populate with values from the reader. The form does not bring values in other words looks like the select command not working.

            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Καρτέλα_Ασθενή where Κωδ_Ασθενή = @Κωδ_Ασθενή";
            cmd.Parameters.AddWithValue("@Κωδ_Ασθενή", MyGlobals.Patient_code);
            cmd.Connection = accessdb;
            accessdb.Open();

            OleDbDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                while (dr.Read())
                {
                    txt_patient_code.Text = dr["Κωδ_Ασθενή"].ToString();
                    txt_Surname.Text = dr["Επώνυμο"].ToString();
                    txt_Name.Text = dr["Όνομα"].ToString();
                    txt_date.Text = Convert.ToDateTime(dr["Ημερομηνία_Γέννησης"]).ToShortDateString();
                    txt_address.Text = dr["Διεύθυνση"].ToString();
                    txt_area.Text = dr["Περιοχή"].ToString();
                    txt_phone.Text = dr["Τηλ"].ToString();
                    txt_fax.Text = dr["Φαξ"].ToString();
                    txt_insurance.Text = dr["Ασφάλεια"].ToString();
                    txt_comments.Text = dr["Παρατηρήσεις"].ToString();
                    txt_history.Text = dr["Ιστορικό"].ToString();
                    txt_alergies.Text = dr["Αλλεργίες"].ToString();
                    txt_email.Text = dr["e-mail"].ToString();
                }
            }
            dr.Close();
            accessdb.Close();
Was it helpful?

Solution 2

ok, i see some problems with your code, but i do not fully understand your question so i'll answer it as much as i can. first of all, as i wrote, remove the if (dr.Read()), because the while loop already do that. now, there is a problem on the inside of your loop because it meant that if you have several items they will override the previous, and you'll be left with the last item values, so you have to figure what you want to do with the data, maybe a dataGrid or something.

in your question you are talking about your query, that if you don't put value in MyGlobals.Patient_code the query still works, at least i think that's what you meant. might be because then you're left with the last value or a string.empty value that you might have in your database. check the database and the value you have each time when you get to that line and tell us more about your question if you want further help

OTHER TIPS

remove the while loop like below

if (dr.Read())
{
  txt_patient_code.Text = dr["Κωδ_Ασθενή"].ToString();
  //..... set all other text box values 

}

if you have only one record match with your sql then in first dr.Read() it will fetch that record . when you come to while loop there is no record to fetch from the database.

And also there may be columns allow null values, before calling to string method you better check for dbnull

 txt_email.Text = dr["Επώνυμο"] == DBNull.Value ? string.Empty : dr["Επώνυμο"].ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top