Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,208,017 members, 8,001,117 topics. Date: Wednesday, 13 November 2024 at 12:53 AM |
Nairaland Forum / Science/Technology / Programming / Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# (2247 Views)
Why Is OOP PHP So Confusing? / Mongoose/xampp: Using Php(on Mongoose) To Insert Data Into Mysql On Xampp / How To Insert Data Into An Online Mysql Database In Java (2) (3) (4)
Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by mj(m): 11:13am On Jul 04, 2012 |
This is my oop code for selecting patients: namespace HospitalAction { public class Patients { private string connectionString; public Patients() { connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\medicalDB.mdf;Integrated Security=True;User Instance=True"; } public DataSet GetPatient(int patientID) { string query = "SELECT * FROM patient WHERE registration_id =@Registration_ID"; SqlCommand cmd = new SqlCommand(query); cmd.Parameters.AddWithValue("@Registration_ID", patientID); return FillDataSet(cmd, "Patient" } public DataSet GetPatient() { string query = "SELECT * FROM patient"; SqlCommand cmd = new SqlCommand(query); return FillDataSet(cmd, "Patients" } private DataSet FillDataSet(SqlCommand cmd, string tableName) { SqlConnection con = new SqlConnection(connectionString); cmd.Connection = con; SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); try { con.Open(); adapter.Fill(ds, tableName); } finally { con.Close(); } return ds; } } this is the code in my form to load the data: HospitalAction.Patients DB = new HospitalAction.Patients(); dataGridView1.DataSource = DB.GetPatient(); but its not showing anythhing on the datagrid and reports no error. thanks. |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by AbidemiA: 11:20am On Jul 04, 2012 |
Add this line of code: dataGridView1.databind(); |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by mj(m): 11:44am On Jul 04, 2012 |
its showing this error: Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'databind' and no extension method 'databind' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) C:\Users\DENRELE\Documents\Visual Studio 2010\Projects\HospitalManagementSystem\HospitalManagementSystem\Employeedata.cs 30 32 HospitalManagementSystem |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by AbidemiA: 12:01pm On Jul 04, 2012 |
Sorry, thought it was web-form; delete previous and add this line: dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); Also please check the following: 1 Remove the try block that you used before your open method OR add catch block that will display error message in case there is problem with your form connecting to your database. 2 With the above, if there is no error msg and no record is displayed, confirm that your dataset has records in it. |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by mj(m): 12:34pm On Jul 04, 2012 |
my friend wrote the oop class but he is not around, i jst notice that select statement comes before the database was opened, dunno how to fix that, but im working on it, any help will be greatly accepted. |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by AbidemiA: 12:41pm On Jul 04, 2012 |
Replace this block try { con.Open(); adapter.Fill(ds, tableName); } finally { con.Close(); } with con.Open(); adapter.Fill(ds, tableName); con.Close(); ... please let me know if there is any error msg. |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by mj(m): 1:00pm On Jul 04, 2012 |
no error, but its not displaying anything in the grid, there are data in the table. |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by AbidemiA: 3:27pm On Jul 04, 2012 |
Use the step through tool in Visual Studio to step through your code when it is executing, check if the dataset "ds" has record in it before you bind it to your datagridview. |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by Kobojunkie: 5:38pm On Jul 04, 2012 |
a) When using an Adapter, the Adapter really takes care of the connection opening and closing, so that does not seem to be where the problem you are having is from. b) If you take a close look at what your GetPatient() method is doing, you probably need to make sure your dataset even contains the information you expect it to. Put a break point there and try to take a peek using the "Add Watch" debug functionality to make sure it is returning you data from the database. c) Also where you access the data in your dataset, try specifying the tablename HospitalAction.Patients DB = new HospitalAction.Patients(); dataGridView1.DataSource = DB.GetPatient().Tables[tablename]; |
Re: Help On Loading Sql Table Data Into Datagrid Using OOP Approach C# by mj(m): 2:58pm On Jul 05, 2012 |
It did not work really, this one works though... public DataTable GetPatientMJ() { SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM patient", connectionString); DataTable dt = new DataTable(); da.Fill(dt); return dt; } I called it this way: dataGridView1.DataSource = DB.GetPatientMJ(); Thanks though. |
(1) (Reply)
How To Make Money With Your Mobile Apps At Playjoor.com.ng / How To Call Digitalpersona One Touch Com-activex Sdk Using Javascript / How Many Courses Did It Take You To Learn/grasp Javascript?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14 |