Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I have a Gridview and for some reason I can not see the first record oF my query

Gridview code

XML
<asp:GridView ID="GridView1"  CssClass="DG"  RowStyle-Wrap="false"
                     runat="server" AutoGenerateColumns="False" PageIndex="1" PageSize="1"   ><emptydatarowstyle backcolor="LightBlue"
                                     forecolor="Red"/>
                                   <emptydatatemplate>

                                      No Data Found.</emptydatatemplate><AlternatingRowStyle
                                BackColor="#99CCFF" Font-Size="Small" /><Columns>
                                <asp:TemplateField HeaderText="ID" Visible="false"><ItemTemplate><%# DataBinder.Eval(Container.DataItem, "ID") %>
                                </ItemTemplate></asp:TemplateField>
                                <asp:BoundField DataField="Employee_No"
                                    HeaderText="Employee No" SortExpression="Employee_No" /><asp:BoundField
                                    DataField="Employee_Name" HeaderText="Employee Name"
                                    SortExpression="Employee_Name" /><asp:BoundField DataField="Cost_Center"
                                    HeaderText="Cost Center" SortExpression="Cost_Center" /><asp:BoundField
                                    DataField="Lead_Id" HeaderText="Lead Id" SortExpression="Lead_Id" /><asp:BoundField
                                    DataField="WorkSchedule_Date" HeaderText="WorkSchedule Date"  HtmlEncode="false"
                                    SortExpression="WorkSchedule_Date" /><asp:BoundField
                                    DataField="Schedule_In" HeaderText="Schedule In" SortExpression="Schedule_In"  /><asp:BoundField
                                    DataField="Schedule_Out" HeaderText="Schedule Out"
                                    SortExpression="Schedule_Out" /><asp:BoundField DataField="Job_In_Time"
                                    HeaderText="Job In Time" SortExpression="Job_In_Time" /><asp:BoundField
                                    DataField="Job_Out_Time" HeaderText="Job Out Time"
                                    SortExpression="Job_Out_Time" /><asp:BoundField DataField="column1"
                                    HeaderText="ETC In (P10)" SortExpression="column1" /><asp:BoundField
                                    DataField="column2" HeaderText="ETC Out (P20)" SortExpression="column2" /><asp:BoundField
                                    DataField="ETBH" HeaderText="ETBH" SortExpression="ETBH" /><asp:BoundField
                                    DataField="ETAH" HeaderText="ETAH" SortExpression="ETAH" />
                                    <asp:TemplateField
                                    HeaderText="User Approval" SortExpression="User_Approval" >
                                    <HeaderTemplate>User Approval<br /><asp:LinkButton     ID="LnkUpdate"     runat="server"
                                    CommandName="update"     Text="Update All"     ForeColor="BlueViolet"
                                     OnClick="LnkUpdate_Click"></asp:LinkButton></HeaderTemplate><ItemTemplate>
                                    <asp:DropDownList
                                     ID="ddl_Approval" runat="server"><asp:ListItem Value="UNAPPROVED"></asp:ListItem><asp:ListItem
                                            Value="APPROVED"></asp:ListItem></asp:DropDownList><%# DataBinder.Eval(Container.DataItem, "User_Approval")%></ItemTemplate></asp:TemplateField></Columns><HeaderStyle
                                BackColor="WhiteSmoke" /><RowStyle Wrap="False" /></asp:GridView>



Code behind:

C#
protected void Button1_Click(object sender, EventArgs e)
        {

            if ((txtWSDate.Text == ""))
            {
                //Response.Write((txtCourseCreditDate.Text == "")  + " - " + (txtHours.Text == ""));
                //Response.End();
                Response.Write("<script type=\"text/javascript\" language=\"javascript\">");
                Response.Write("alert('Schedule Date must have an entry!');");
                Response.Write("</script>");
                return;
            }

            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["OTTARConnectionString"].ConnectionString);
            myConnection.Open();

            try
            {
                GridView1.DataSourceID = "";
                GridView1.Visible = true;
                SqlDataReader reader = null;
                string LeadID = ddl_leader_list.SelectedItem.Value.ToString();
                string SkedDate = txtWSDate.Text.ToString();
                SqlCommand cmd = new SqlCommand();
                //  SqlCommand cmd = new SqlCommand(" " + serverIP, myConnection);
                string strData = "";
                strData = "SELECT ID,[Employee No] AS Employee_No, [Employee Name] AS Employee_Name, ";
                strData += " [Cost Center] AS Cost_Center, RTRIM(LTRIM([Lead Id])) AS Lead_Id, ";

                strData += " [WorkSchedule Date] AS WorkSchedule_Date, [Schedule In] AS Schedule_In, ";
                strData += " [Schedule Out] AS Schedule_Out, [Job In Time] AS Job_In_Time,  ";
                strData += "  [Job Out Time] AS Job_Out_Time, [ETC In (P10)] AS column1,  ";
                strData += "[ETC Out (P20)] AS column2, [ETBH], [ETAH], ";
                strData += "[Approved Disapproved] AS User_Approval FROM [SAA_MCS_Extended_Time2] ";
                strData += " WHERE  RTRIM(LTRIM([Lead Id])) = '" + ddl_leader_list.SelectedValue + "'";
                strData += " AND [WorkSchedule Date] = Cast ('" + txtWSDate.Text + "' as datetime)";


               Response.Write(strData);
            //    Response.End();

                cmd.Connection = myConnection;
                cmd.CommandText = strData;
                reader = cmd.ExecuteReader();
            
                while (reader.Read())
                {
                    GridView1.DataSource = reader;
                    GridView1.DataBind();
                    Response.Write(reader);
                }

            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            myConnection.Close();
        }
Posted
Updated 30-Jul-11 16:39pm
v2
Comments
Rakesh From Patna 30-Jul-11 18:35pm    
How many recods display?

1 solution

I am not certain on this. But I do not think the lines "GridView1.DataSource = reader;" and "GridView1.DataBind()" should be inside the "while (reader.Read())" block. Someone please correct me if I am wrong. But, for example, if you query ten thousand rows, you are telling GridView1 to update its data ten thousand times.
 
Share this answer
 
Comments
thatraja 30-Jul-11 22:41pm    
You right, there is no need to put those things inside the while block
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900