4 Replies - 1055 Views - Last Post: 27 June 2013 - 06:10 PM Rate Topic: -----

#1 bullrider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-February 10

Data grid Not Displaying

Posted 26 June 2013 - 06:34 PM

I am working on a project where I am needing to display a list of data in a data grid. I have a working SQL statement, that I have verified valid data via SQL Management Studio (the statement is in the code). I have tried several different methods of populating the data grid with the rows returned from the query but the data grid will not display (which I am assuming is due to it not getting any data). The current code is my adaptation of MSDN's method of populating the data grid. I have comments of one of the other ways I have tried I think.

I am kind of at my wits end chasing this thing around. I would really appreciate any help. Thank you in advance.

This is the code-behind the web form:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace cipher_pos_asp.Pages
{
    public partial class ViewPunches : System.Web.UI.Page
    {
        private static SQL_Connections sql = new SQL_Connections();

        private const string CurrentDay =
            " Select P.Punch As Punch, (E.FirstName + ' ' + E.LastName) As EmployeeName, PT.Description As PunchType " +
            " From Punches P " +
            " Join Employee E On P.EmployeeID = E.EmployeeID " +
            " Join PunchType PT On P.PunchTypeID = PT.PunchTypeID " +
            " Where (Punch > DATEADD(d, -1, GetDate())) And " +
	        " (Punch < DATEADD(d, 1, GetDate())) ";

        private DataTable dtViewPunches = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            sql.Clear();
            sql.Add(CurrentDay);
            sql.Open();

            var initialColumns = new List<string> {"Punch", "EmployeeName", "PunchType"};
            grdViewPunches.DataSource = LoadData(initialColumns);
            grdViewPunches.DataBind();
            /*
            dsViewPunches.ConnectionString = sql.ConnectionString;
            dsViewPunches.SelectCommand = CurrentDay;
            dsViewPunches.Select(DataSourceSelectArguments.Empty);
            grdViewPunches.DataBind();
             */
        }

        private ICollection LoadData (IEnumerable<string> columns)
        {
            foreach (var column in columns)
            {
                dtViewPunches.Columns.Add(column);
            }
            
            do
            {
                foreach (var column in columns)
                {
                    DataRow dataRow = dtViewPunches.NewRow();
                    try
                    {
                        dataRow[column] = sql.reader[column];
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            } while (sql.reader.Read());
            /*
            dataRow = dtViewPunches.NewRow();
            dataRow[0] = sql.reader["Punch"];
            dataRow[1] = sql.reader["PunchType"];
            dataRow[2] = sql.reader["EmployeeName"];

            while (sql.reader.Read())
            {
                dataRow = dtViewPunches.NewRow();
                dataRow[0] = sql.reader["Punch"];
                dataRow[1] = sql.reader["PunchType"];
                dataRow[2] = sql.reader["EmployeeName"];
            }
            */
            var dv = new DataView(dtViewPunches);
            return dv;
        }
    }
}



This is the relevent parts of my data class:
class SQL_Connections
    {
		private SqlConnection connection;
		private SqlCommand command;
        private static SqlDataReader dataReader;
		private SqlCommandBuilder commandBuilder;
        private const string TCConnectionString = "Data Source = (local); Initial Catalog = Timeclock; User ID = sa; Password = Sasa001";
        private string RecordValue;

        public string ConnectionString
        {
            get { return TCConnectionString; }
        }

        public string CurrentValue
        {
            get
            {
                return RecordValue != String.Empty ? RecordValue : String.Empty;
            }
        }

        public SqlDataReader reader
        {
            get { return dataReader; }
        }

        public SQL_Connections()
        {
            connection = new SqlConnection(TCConnectionString);
            command = new SqlCommand();
        }

        public void Open()
		{
            if (command == null) return;
            if (command.CommandText == String.Empty) return;
            if (command.Connection == null)
                command.Connection = connection;
			try
			{
                connection.Close();
				connection.Open();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
			}

            
            dataReader = command.ExecuteReader();
            if (dataReader.Read())
                RecordValue = dataReader.GetValue(0).ToString();
		}

		public void Clear()
		{
			if (command == null) return;
			command.CommandText = String.Empty;
		}

		public void Close()
		{
			if (connection == null) return;
			if (connection.State != ConnectionState.Closed)
			{
			    Clear();
                connection.Close();
			}
		}

		public void Execute()
		{
			if (command == null) return;
            if (command.CommandText == String.Empty) return;

            try
            {
                connection = new SqlConnection
                                 {
                                     ConnectionString = TCConnectionString
                                 };
                connection.Open();
                if (command.Connection == null)
                    command.Connection = connection;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

			command.ExecuteNonQuery();
		}

		public void Add(string sqlString)
		{
			if (command == null)
			{
				command = new SqlCommand ();
			}

			command.CommandText = command.CommandText + sqlString;
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Data grid Not Displaying

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3318
  • View blog
  • Posts: 11,222
  • Joined: 12-December 12

Re: Data grid Not Displaying

Posted 26 June 2013 - 06:45 PM

Quote

the data grid will not display

To clarify, are you saying that it doesn't display at all, or that it isn't populated with the data?

This post has been edited by andrewsw: 26 June 2013 - 06:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 bullrider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-February 10

Re: Data grid Not Displaying

Posted 26 June 2013 - 06:56 PM

View Postandrewsw, on 26 June 2013 - 06:45 PM, said:

Quote

the data grid will not display

To clarify, are you saying that it doesn't display at all, or that it isn't populated with the data?


From the data view that is returned from the function is populated with valid data (from what I can tell in debugger), which is what the grid view is supposed to be populated from. So I am assuming that it is getting populated. However, I think that the grid view is not displayed if there is no data.

To keep from running in circles, the grid is not displayed on the page at all (that I can tell). There is no evidence of it displaying on the page, that I can tell.
Was This Post Helpful? 0
  • +
  • -

#4 bullrider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-February 10

Re: Data grid Not Displaying

Posted 27 June 2013 - 04:29 PM

Ok, so I have continued to try and find the reason behind this grid not displaying. I have found that the dataview that is being returned is getting populated with the columns, however, no data is being associated with them, that I can find in the values pane. So, assuming that the gridview does not display if it is "empty", this would be the problem.

Now, does anything with the code just jump out and say "Hey! I'm the reason your dataview is not getting populated!"? I am going to continue to look into it. I just wanted to update anyone who was trying to help.
Was This Post Helpful? 0
  • +
  • -

#5 bullrider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-February 10

Re: Data grid Not Displaying

Posted 27 June 2013 - 06:10 PM

Got it going. So it turns out, if you never actually add the row to the datatable before you try to use it to populate the gridview, the gridview will be empty!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1