0

I'm trying to have a DataGridView in my Windows Forms showing data from the database, but it doesn't want to run.

namespace DentalClinicManagementSystem
{
    class MyPatient
    {
        public void AddPatient(string query)
        {
            ConnectionString MyConnection = new ConnectionString();

            SqlConnection Con = MyConnection.GetCon();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Con;

            Con.Open();
            cmd.CommandText = query;
            cmd.ExecuteNonQuery();
            Con.Close();
        }

        public DataSet ShowPatient(string query)
        {
            ConnectionString MyConnection = new ConnectionString();

            SqlConnection Con = MyConnection.GetCon();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Con;
            cmd.CommandText = query;

            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();

            sda.Fill(ds);

            return ds;
        }
    }
}
5
  • Please share the code you use to actually call the function. Commented Feb 5, 2024 at 5:06
  • Please post your actual SQL and connection string that you are using. Commented Feb 5, 2024 at 6:16
  • Sqlcommand indicates that you use ms sql server, not mysql. But this question has nothing to do with either. Commented Feb 5, 2024 at 6:21
  • You need to set sda.SelectCommand = cmd; before calling Fill(ds); Commented Feb 5, 2024 at 8:52
  • I sincerely hope you aren't doing SQL injection which is very insecure. Commented Feb 5, 2024 at 12:30

0

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.