简体   繁体   中英

Need help figuring out a gridview what am I doing wrong?

How can I get the data to show on the gridview, anybody see what I'm doing wrong?

<asp:GridView ID="xTimeGridView"
 runat="server" AllowSorting="True" 
          AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="CLOCK_IN_TIME"
 HeaderText="CLOCK_IN_TIME" 
                    SortExpression="CLOCK_IN_TIME" />
                <asp:BoundField DataField="CLOCK_OUT_TIME"
 HeaderText="CLOCK_OUT_TIME" 
                    SortExpression="CLOCK_OUT_TIME" />
            </Columns>

    string cmdquery = "SELECT * FROM EMPLOYEES WHERE BADGE ='" + Badge + "'";



            OracleCommand cmd = new OracleCommand(cmdquery);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            conn.Open();
            using (OracleDataReader reader = cmd.ExecuteReader())
            {

                while (reader.Read())
                {
                    this.xUserNameLabel.Text += reader["EMPLOYEE_NAME"];
                    this.xDepartmentLabel.Text += reader["REPORT_DEPARTMENT"];


                }




            }
            conn.Close();

            string hrquery = "SELECT CLOCK_IN_TIME, CLOCK_OUT_TIME FROM CLOCK_HISTORY WHERE BADGE='" + Badge + "'";

            OracleCommand time = new OracleCommand(hrquery);
            time.Connection = conn;
            time.CommandType = CommandType.Text;
            conn.Open();

            using (OracleDataReader readers = time.ExecuteReader())
            {
                while (readers.Read())
                {

                    xTimeGridView.DataSource = readers;
                    xTimeGridView.DataBind();

                }


            }
            conn.Close();

Oh, you've got a problem here.

Just so that you understand what's going on, in your code, you are trying to open up the data that you get, and loop through it all; I'm not sure but I think you are effectively re-binding your GridView to each line of data individually.

You don't want to iterate through all the data and "read" it, what you want to do is store your query's results in a bindable object (like a DataSet) so that you can glue your gridview to it.

Try something like this instead, be sure to add your try/catch's later:

        OracleCommand time = new OracleCommand(hrquery);
        time.Connection = conn;
        time.CommandType = CommandType.Text;
        conn.Open();
        // new code starts below
        DataSet data = new DataSet("my data");
        OracleDataAdapter adapter = new OracleDataAdapter(time);
        adapter.Fill(data);
        conn.Close();

        xTimeGridView.DataSource = data;
        xTimeGridView.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM