Search code examples
c#wpfreportviewer

Unable to get all records displayed in report viewer


[edit:I added the data visualizer] I have a teacher's registration where records are being input using an insert button in a SQL database as such

private void insertdata(object sender, RoutedEventArgs e)
        {
            try
            {
                string name = name_field.Text;
                string email = email_field.Text;

                string id = id_field.Text;
                string gender;
                if (male.IsChecked == true)
                {
                    gender = "M";
                }
                else
                    gender = "F";


                if (String.IsNullOrEmpty(dob.Text))
                {
                    string fe = "01/01/1900 12:00:00 AM";
                    dt = Convert.ToDateTime(fe);
                }
                else
                {
                    dt = Convert.ToDateTime(dob.Text);

                }



                var rich1 = new TextRange(this.history.Document.ContentStart, this.history.Document.ContentEnd);

                string teach_history = rich1.Text;

                var rich2 = new TextRange(this.achievement.Document.ContentStart, this.achievement.Document.ContentEnd);

                string teach_achievement = rich2.Text;

                connect();
                con.Open();


                string saved = "insert into teacher_details ([Teacher ID],Name,Gender,Email,[Date of Birth],Employment History,Achievements)values('" + id + "', '" + name + "','" + gender + "','" + email + "','" + dt + "','" + teach_history + "','" + teach_achievement + "')";

                SqlCommand cmd = new SqlCommand(saved, con);

                cmd.ExecuteReader();

                con.Close();

                MessageBox.Show("record is added");
              
            }
            catch(Exception ex)
            {
                MessageBox.Show("error occured " + ex);
            }

I have id,gender,name,email,history,achievements,date of birth as my fields. I made a class for this as follows:

    class Teacherdetail
    {
        public string id { get; set; }
        public string name { get; set; }
        public string email { get; set; }
        public string gender { get; set; }
        public string history { get; set; }
        public string acheive { get; set; }
        public DateTime dob { get; set; }
    }

in my rdlc report i used object as my dataset and selected Teacherdetail. Then I made a table as follow: teacherreport.rdlc

i then have a button view report where I call details into the reportviewer that is called teacherreport

ReportDataSource reportDataSource = new ReportDataSource();
            connect();
            con.Open();

            SqlDataAdapter adp = new SqlDataAdapter("select [Teacher ID],Name,Gender,email,[Date of Birth],[Employment History],Achievements from teacher_details", con);

            DataTable newtab = new DataTable();
            adp.Fill(newtab);
            reportDataSource.Name = "DataSet1";



            reportDataSource.Value = newtab;

            teacherreport.LocalReport.ReportPath = "C:\\Users\\Preeti Rawat\\Documents\\Visual Studio 2012\\Projects\\STDNT\\STDNT\\teacherreport.rdlc"; 



            teacherreport.LocalReport.DataSources.Add(reportDataSource);

            teacherreport.LocalReport.Refresh();
            teacherreport.RefreshReport();

enter image description here

My problem is that id, history, name and achieve are missing.

When I debug the program, in my data visualizer it does show that the information is passing through. enter image description here but it is still not appearing when in the program. What can the problem be and how can I fix it? Thank you ^^


Solution

  • The reason this was not working was that the class names were different from the table's field name. They have to be the same. So I changed column names of my database table so that it looks like this:

    Teacher_ID,Name,Gender,Email,Date_of_Birth,Employment_History,Achievements

    and my class looks like this:

            public int Teacher_ID { get; set; }
            public string Name { get; set; }
            public string Gender { get; set; }
            public string email { get; set; }
            public DateTime Date_of_Birth { get; set; }
            public string Employment_History { get; set; }
            public string Achievements { get; set; }