locked
How do I resolve Grid view issue RRS feed

  • Question

  • User-218090889 posted

    I was trying to fetch some data from data table to display the result in a gridview, but I am getting error message that says:

    Unable to cast object of type 'ASP.GridviewPage_aspx' to type 'System.Web.UI.WebControls.GridViewRow'.

    Below is my aspx code

     <asp:Button ID="Button1" runat="server" BackColor="#3399FF" Font-Bold="True" ForeColor="White" Height="21px" Text="Search" Width="156px" OnClick="Button5_Click" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField> <ItemTemplate> <span class="auto-style4">Name</span> <asp:TextBox ID="TextBox1" runat="server" Width="156px" OnTextChanged="TextBox1_TextChanged" Text='<%# DataBinder.Eval(Container.DataItem,"TextBox1") %>'></asp:TextBox> <asp:Label ID="lbl1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"lbl1") %>'></asp:Label> <span class="auto-style4">Town</span> <asp:TextBox ID="TextBox2" runat="server" Height="16px" OnTextChanged="TextBox2_TextChanged" Width="158px" Text='<%# DataBinder.Eval(Container.DataItem,"TextBox2") %>'></asp:TextBox> <br /> </span> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

    below is my command code.

     protected void Button1_Click(object sender, EventArgs e)
            {
                GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
                TextBox TextBox1 = row.FindControl("TextBox1") as TextBox;
                TextBox TextBox2 = row.FindControl("TextBox2") as TextBox;
                Label lbl1 = row.FindControl("lbl1") as Label;
    
                string ScID = Session["UserId"].ToString();
                SqlConnection con; //**Normal sql connection not of DataBase class**
                SqlCommand cmdd;
                string connetionString = null;
                connetionString = (@"Data Source=MyDB;Integrated Security=False;");
                con = new SqlConnection(connetionString);
    
                using (SqlCommand sqlCmd = new SqlCommand("SELECT TOP (1) col1, col2, col3 FROM Table1", con))
                {
                    con.Open();
                    SqlDataReader rd = sqlCmd.ExecuteReader();
                    if (null != rd && rd.HasRows)
                    {
                        //-- this reads a single line of record from data source (sql query result that you have specified above in command)
                        rd.Read();
    
                        //-- display data in controls
                        TextBox1.Text = Convert.ToString(rd["col1"]);
                        TextBox2.Text = Convert.ToString(rd["col2"]);
                        lbl1.Text = Convert.ToString(rd["col3"]);
                    }
                    con.Close();
                }
            }
    

    Monday, August 14, 2017 3:30 PM

Answers

  • User2103319870 posted

    fetch some data from data table to display the result in a gridview,

    You may need to rethink the approach. If all you need is to load the data to gridview then you dont need the code which you have now. Please find sample code which loads data from Adventrue Workds datatable.

            <asp:Button ID="Button1" runat="server" BackColor="#3399FF" Font-Bold="True" ForeColor="White" Height="21px" Text="Search" Width="156px" OnClick="Button1_Click" />
    
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <span class="auto-style4">Name</span>
                            <asp:TextBox ID="TextBox1" runat="server" Width="156px"  Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:TextBox>
                            <asp:Label ID="lbl1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
                            <span class="auto-style4">Town</span>
                            <asp:TextBox ID="TextBox2" runat="server" Height="16px"  Width="158px" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:TextBox>
                            <br />
                            </span>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    C#:

     protected void Button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ToString());
                string sSQL = "Select top 10 FirstName,LastName FROM DimCustomer";
                SqlCommand cmd3 = new SqlCommand(sSQL, con);
                con.Open();
                DataTable dt = new DataTable();
                dt.Load(cmd3.ExecuteReader());
    
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
            }

    Change the column names as per your design

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 14, 2017 4:04 PM

All replies

  • User2103319870 posted

    Enzyme

    <asp:Button ID="Button1" runat="server" BackColor="#3399FF" Font-Bold="True" ForeColor="White" Height="21px" Text="Search" Width="156px" OnClick="Button5_Click" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField>

    You have button control outside the gridview, and its on the page now. Thats the reason you are getting this exception. You need to use a different approach to access the gridview from a button outside the gridview control

    OR An easy option is to move the button inside of gridview like below

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <span class="auto-style4">Name</span>
                            <asp:TextBox ID="TextBox1" runat="server" Width="156px" OnTextChanged="TextBox1_TextChanged" Text='<%# DataBinder.Eval(Container.DataItem,"TextBox1") %>'></asp:TextBox>
                            <asp:Label ID="lbl1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"lbl1") %>'></asp:Label>
                            <span class="auto-style4">Town</span>
                            <asp:TextBox ID="TextBox2" runat="server" Height="16px" OnTextChanged="TextBox2_TextChanged" Width="158px" Text='<%# DataBinder.Eval(Container.DataItem,"TextBox2") %>'></asp:TextBox>
                            
                            <br />
                           
                            </span>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField>
                        <ItemTemplate>
                             <asp:Button ID="Button1" runat="server" BackColor="#3399FF" Font-Bold="True" ForeColor="White" Height="21px" Text="Search" Width="156px" OnClick="Button1_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    Monday, August 14, 2017 3:52 PM
  • User2103319870 posted

    fetch some data from data table to display the result in a gridview,

    You may need to rethink the approach. If all you need is to load the data to gridview then you dont need the code which you have now. Please find sample code which loads data from Adventrue Workds datatable.

            <asp:Button ID="Button1" runat="server" BackColor="#3399FF" Font-Bold="True" ForeColor="White" Height="21px" Text="Search" Width="156px" OnClick="Button1_Click" />
    
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <span class="auto-style4">Name</span>
                            <asp:TextBox ID="TextBox1" runat="server" Width="156px"  Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:TextBox>
                            <asp:Label ID="lbl1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
                            <span class="auto-style4">Town</span>
                            <asp:TextBox ID="TextBox2" runat="server" Height="16px"  Width="158px" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:TextBox>
                            <br />
                            </span>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    C#:

     protected void Button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ToString());
                string sSQL = "Select top 10 FirstName,LastName FROM DimCustomer";
                SqlCommand cmd3 = new SqlCommand(sSQL, con);
                con.Open();
                DataTable dt = new DataTable();
                dt.Load(cmd3.ExecuteReader());
    
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
            }

    Change the column names as per your design

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 14, 2017 4:04 PM