No Gridview shown

Hi,
I have the codes like
    <asp:GridView ID="GridView1" runat="server"  Width = "550px"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
    HeaderStyle-BackColor = "green" AllowPaging ="true" Data  
    ShowFooter = "true" OnPageIndexChanging="GridView1_PageIndexChanging" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" 
    OnRowUpdating="GridView1_RowUpdating" PageSize = "10" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
    <asp:TemplateField HeaderText="Ite">
        <ItemTemplate>
            <asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddl2" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddl3" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
        <ItemTemplate>
            <asp:TextBox id="tb_Ite_code" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox id="TextBox2" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox id="TextBox5" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Saving Rate">
        <ItemTemplate>
            <asp:Label ID="curr_rate" runat="server"
                Text='<%# Eval("Ite_rate")%>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="Label1" runat="server" 
                Text='<%# Eval("Ite_rate")%>' >
            </asp:Label>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:Label ID="Label2" runat="server" 
                Text='<%# Eval("Ite_rate")%>' >
            </asp:Label>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete?">
        <ItemTemplate>
            <span onclick="return confirm('Are you sure to delete the record?')">
                <asp:LinkButton CssClass="forbutton" ID="lnkdelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete"/>
            </span>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Button CssClass="forbutton" ID="btnadd" runat="server" Text="Add" OnClick="AddNewRec" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="true" />
    </Columns>
    <AlternatingRowStyle BackColor="White"/>
    <EditRowStyle BackColor="#efefef" />
    <FooterStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="true" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    ...
        private void BindData()
        {
            DataTable table = new DataTable();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString))
            {
                string sql = "Select * from itetab order by Ite_code";
                //string sql = "Select * from itetab";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                //using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    //using (OleDbDataAdapter ad = new OleDbDataAdapter(cmd))
                    {
                        ad.Fill(table);
                    }
                }
            }
            GridView1.DataSource = table;
            GridView1.DataBind();
        }
        ...

Open in new window

and there're records within "itetab", but I don't know why there is no Gridview displayed, when I go to the deployed page.
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramachandar NCommented:
I don't find any issues with your code. Can you check if the visibility of DataGridView is set to True? Also debug and check if the query actually returns rows and the DataTable holds rows.
Lokesh B RDeveloperCommented:
Hi,

Everything looks fine.

Add the following code to check whether there are any ROWS in database.

 if (table.Rows.Count > 0)
            {
                GridView1.DataSource = table;
                GridView1.DataBind();
               Response.Write("TOTAL ROWS = " + table.Rows.Count);
           }
            else
            {
               Response.Write("NO ROWS FOUND");
           }
Lokesh B RDeveloperCommented:
Hi,

Call you BindData in Page_Load as below and check the rows as i mentioned in above post.

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
         {
             // Bind your GridView on Page load only once 
               BindData();
         }
}

Open in new window

HuaMin ChenProblem resolverAuthor Commented:
Many thanks.
Sorry for that I further get this
Server Error in '/Start_dev' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   Start._Default.GridView1_RowEditing(Object sender, GridViewEditEventArgs e) +329
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +861
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237

Open in new window

with these codes?
    <asp:GridView ID="GridView1" runat="server"  Width = "550px"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
    HeaderStyle-BackColor = "green" AllowPaging ="true" Data  
    ShowFooter = "true" OnPageIndexChanging="GridView1_PageIndexChanging" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" 
    OnRowUpdating="GridView1_RowUpdating" PageSize = "10" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
    <asp:TemplateField HeaderText="Ite">
        <ItemTemplate>
            <asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddl2" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddl3" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
            </asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField Visible="false">
        <ItemTemplate>
            <asp:TextBox id="tb_Ite_code" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox id="tb_Ite_cd" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox id="tb_Ite_cd2" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Saving Rate">
        <ItemTemplate>
            <asp:Label ID="Ite_rate" runat="server"
                Text='<%# Eval("Ite_rate")%>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="Label1" runat="server" 
                Text='<%# Eval("Ite_rate")%>' >
            </asp:Label>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:Label ID="Label2" runat="server" 
                Text='<%# Eval("Ite_rate")%>' >
            </asp:Label>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete?">
        <ItemTemplate>
            <span onclick="return confirm('Are you sure to delete the record?')">
                <asp:LinkButton CssClass="forbutton" ID="lnkdelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete"/>
            </span>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Button CssClass="forbutton" ID="btnadd" runat="server" Text="Add" OnClick="AddNewRec" />
        </FooterTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowEditButton="true" />
    </Columns>
    <AlternatingRowStyle BackColor="White"/>
    <EditRowStyle BackColor="#efefef" />
    <FooterStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="true" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    ...
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            lb_msg.Text = "";
            TextBox tb = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("tb_Ite_cd");
            TextBox tb2 = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("tb_Ite_cd2");
            tb2.Text = tb.Text;
            GridView1.EditIndex = e.NewEditIndex;
            this.PopulateData();
        }
        ...

Open in new window

Lokesh B RDeveloperCommented:
Hi,

In RowEditing, the only thing you need to set is the NewEditIndex and populate the GridView.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
         GridView1.EditIndex = e.NewEditIndex;
         this.PopulateData();
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.