Hi
I designed an application where I register students, these records is added then to a database in my localdb using sqlserver etc.
everything works fine.
I want to create a form to do a "attendance register". Basically to select a date from a datetimepicker and then selecting, or by checking each student
present from 5 different classes. These attendances I want to export to an excel sheet and draw pie charts etc etc.
My problem is when I try to retrieve the data from my database into a list view box with only 3 fields(and not fields like address etc) I get the following error
" Executereader: Connection Property has not been initialized".
If someone could please help, or guide me to create this form/function to my existing program, I will really appreciate it.
Here is the code I used in my attendance from
I designed an application where I register students, these records is added then to a database in my localdb using sqlserver etc.
everything works fine.
I want to create a form to do a "attendance register". Basically to select a date from a datetimepicker and then selecting, or by checking each student
present from 5 different classes. These attendances I want to export to an excel sheet and draw pie charts etc etc.
My problem is when I try to retrieve the data from my database into a list view box with only 3 fields(and not fields like address etc) I get the following error
" Executereader: Connection Property has not been initialized".
If someone could please help, or guide me to create this form/function to my existing program, I will really appreciate it.
Here is the code I used in my attendance from
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace test2 { public partial class attendence : Form { public SqlConnection cn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog = Addressbook; Integrated Security = True; Connect Timeout = 30; Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); public attendence() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } private void attendence_Shown(object sender, EventArgs e) { try { cn.Open(); } catch (SqlException ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); Application.ExitThread(); } } private void btnPopulate_Click(object sender, EventArgs e) { listView1.Items.Clear(); SqlCommand cm = new SqlCommand("SELECT * from BizContacts ORDER BY First_Name ASC,cn"); try { SqlDataReader dr = cm.ExecuteReader(); while (dr.Read()) { ListViewItem item = new ListViewItem(dr["First_Name"].ToString()); item.SubItems.Add(dr["Last_Name"].ToString()); item.SubItems.Add(dr["Class"].ToString()); listView1.Items.Add(item); } } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
Last edited by a moderator: