I have a C# table that displays sql database. It has the Id,Name and Location as fields.When the program runs it shows the name place for both name and location.

here is the code

   public static void AddPat(string id,string name, string location)
       {
           string insStmt = "INSERT INTO PATIENT (Id,Name,Loation) VALUES (@id,@name,@location)";
           SqlConnection conn = GetConnection();
           SqlCommand insCmd = new SqlCommand(insStmt, conn);
           insCmd.Parameters.AddWithValue("@id",id);
           insCmd.Parameters.AddWithValue("@name", name);
           insCmd.Parameters.AddWithValue("@location", location);
           try { conn.Open();insCmd.ExecuteNonQuery(); }
           catch (SqlException ex) { throw ex;}
           finally { conn.Close(); }

       }

        public static List<pat> getpatient()
   {

       List<pat> patientlist = new List<pat>();
       SqlConnection conn = GetConnection();
       string selStmt = "SELECT * FROM PATIENT ORDER BY ID ";
                SqlCommand selCmd = new SqlCommand(selStmt,conn);

       try
       {

           conn.Open();
           SqlDataReader reader = selCmd.ExecuteReader();
           while (reader.Read())
           {
               pat patinet = new pat();
               patinet.Name = (string)reader["Name"];
               patinet.Id = (string)reader["ID"];
               patinet.Location = (string)reader["Location"];
               patientlist.Add(patinet);
           }
           reader.Close();

       }

       catch (SqlException ex) { throw ex; }
       finally {conn.Close();} 
        return patientlist;
        }



  and 

          private void showtable_Load(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            List<pat> patientlist;
            try
            {
                patientlist = pat2.getpatient();
                if (patientlist.Count > 0)
                {
                    pat patinet;
                    for (int i = 0; i < patientlist.Count; i++)
                    {
                        patinet = patientlist[i];
                        listView1.Items.Add(patinet.Name.ToString());
                        listView1.Items[i].SubItems.Add(patinet.Id);
                        listView1.Items[i].SubItems.Add(patinet.Name);
                        listView1.Items[i].SubItems.Add(patinet.Location);
                    }
                }
                else
                {
                    MessageBox.Show("There are no Patients.", "Alert"); }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());} 


        }
    }

Recommended Answers

All 2 Replies

When the program runs it shows the name place for both name and location.

Check what is actually in the database first.

                    listView1.Items.Add(patinet.Name.ToString());

was the problem!!! Thanks all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.