Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m trying to load my grid from database. But the grid is not showing. I debugged thoroughly but It shows that the GridViewRow Is getting Null value. I checked my query that i used for loading the grid. It shows data in sqlserver for the particular query.

My Design Page:
ASP.NET
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False"  DataSourceID="sql"
                       DataKeyNames="ID" ShowFooter="true"
                       onrowcreated="grid_RowCreated" AllowPaging="false" OnRowCommand="grid_RowCommand" OnRowDataBound="grid_RowDataBound"
                       OnRowUpdating = "grid_RowUpdate" OnRowDeleting="grid_RowDeleting">


                   <Columns>

                   <asp:TemplateField HeaderText="ID" SortExpression="ID">
                       <ItemTemplate>
                           <asp:Label ID="labela" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                       </ItemTemplate>

                   </asp:TemplateField>


                   <asp:TemplateField HeaderText="Item" SortExpression="Item">
                       <ItemTemplate>
                           <asp:LinkButton ID="label6" runat="server" Text='<%# Bind("Item") %>' CommandName="Item" CommandArgument='<%# Eval("Item","{0}") %>'></asp:LinkButton>
                       </ItemTemplate>

                   </asp:TemplateField>

                   <asp:TemplateField HeaderText="B" SortExpression="B">
                       <EditItemTemplate >
                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("B") %>'></asp:TextBox>

                       </EditItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("B","{0:n}") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>

                   <asp:TemplateField HeaderText="E" SortExpression="E">

                       <ItemTemplate>
                           <asp:Label ID="label8" runat="server" Text='<%# Bind("E","{0:n}") %>'></asp:Label>
                       </ItemTemplate>

                   </asp:TemplateField>

                   <asp:TemplateField HeaderText="R" SortExpression="R">
                       <ItemTemplate>
                           <asp:Label ID="label10" runat="server" Text='<%# Bind("R","{0:n}") %>'></asp:Label>
                       </ItemTemplate>

                   </asp:TemplateField>

                    <asp:CommandField  ShowEditButton="True" EditText="Edit" />
                     <asp:ButtonField CommandName="Transfer" Text="Tr" />
                     <asp:CommandField  ShowDeleteButton="True" EditText="Delete" />

               </Columns>
               </asp:GridView>

 <asp:SqlDataSource ID="Sql" runat="server"
           ConnectionString="server=x.y.z;uid=xuser;pwd=ypass;database=x"

           SelectCommand="something"
           UpdateCommand="something1"
           DeleteCommand="somethin2"
           ProviderName="System.Data.SqlClient">

           <SelectParameters>
              <asp:QueryStringParameter  Name="ID" DbType="Int32" QueryStringField="ID"/>
               <asp:QueryStringParameter  Name='No' DbType="Int32" QueryStringField="SF"/>
           </SelectParameters>

       </asp:SqlDataSource>


CS Page:

 protected void Details()
    {
        string strConnection = ConfigurationManager.ConnectionStrings["String"].ConnectionString;
        SqlConnection sqlCon = new SqlConnection(strConnection);

 SqlCommand cmd = new SqlCommand("SELECT  [Name],[FA],[FF],[FO],[FP],FAM FROM [GRANTS_X] WHERE GID=" + Convert.ToInt32(Request.QueryString["GID"]), sqlCon);
     
        SqlDataReader sqlDR;

        try
        {
            sqlCon.Open();
            sqlDR = cmd.ExecuteReader();

            while (sqlDR.Read())
            {
//This portion is for loading some of the level above the grid. which is not our concern coz those level are populating data.
                Name.Text = sqlDR["Name"].ToString();
                FA.Text = sqlDR["FA"].ToString();
                GFP.Text = sqlDR["FF"].ToString() + "-" + sqlDR["FO"].ToString() + "-" + sqlDR["FP"].ToString();

                FAM.Text = string.Format("{0:n}", sqlDR["FAM"]);
            }
            sqlDR.Close();

// Here the  GridViewRow is showing null. Even though my query is fine. and i supposed to get some valuse here. 

            GridViewRow grid= grid.FooterRow;
           

            if (grid! = null)

                 lblExpended.Text = string.Format("{0:n}", Convert.ToDecimal(grid.Cells[3].Text)); 
              
            else
             
lblNoDataExp.Visible = true; 
            
          if (!(lblExpended.Text.Equals("")))
            lblBalAmount.Text = string.Format("{0:n}", (Convert.ToDecimal(FAM.Text) - Convert.ToDecimal(lblExpended.Text)));
            if (!(lblBalAmount.Text.Equals("") & lblEncb.Text.Equals("")))
            LEBalance.Text = string.Format("{0:n}", (Convert.ToDecimal(lba.Text) - Convert.ToDecimal(lblEncb.Text)));

        }
        catch { }
        finally
        {
            sqlCon.Close();
        }
    }


RowDataBound Function:

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
// UT some usertype
            if (Session["UT"] != null)
            {
                string strType = Session["UT"].ToString();
                if (!(strType.Equals("x") || strType.Equals("y") || strType.Equals("z")))
                {
                    LinkButton lbkBtnE = (LinkButton)e.Row.Cells[5].Controls[0];
                    LinkButton lbkBtnT = (LinkButton)e.Row.Cells[6].Controls[0];
                    LinkButton lbkBtnD = (LinkButton)e.Row.Cells[7].Controls[0]; 
                    lbkBtnE.Enabled = false;
                    lbkBtnT.Enabled = false;
                    lbkBtnD.Visible = false;

                }
            }
            Btotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "B"));
            Etotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "E"));
            Rtotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "R"));

            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                 
                    LinkButton button = control as LinkButton;
                    if (button != null && button.CommandName == "Delete")
                        // Add delete confirmation
                        button.OnClientClick = "if (!window.confirm('Are you sure you want to delete this record?')) return false;";
                }
            }
        }
         else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[1].Text = "Totals:";
            // for the Footer, display the running totals
            e.Row.Cells[2].Text = string.Format("{0:n}",Btotal);
            e.Row.Cells[3].Text = string.Format("{0:n}",Etotal);
            e.Row.Cells[4].Text = string.Format("{0:n}",Rtotal);
          

            e.Row.BackColor = System.Drawing.Color.Beige;
            e.Row.Font.Bold = true;
         }

    }



I don't know why i m getting null in GridViewRow! Any help would be appreciated!
Posted

This is too much code for anyone to really dig into.
Not sure what it is your code is supposed to, but if you want to bind data to a GridView there are easier ways to do it. See GridView.DataBind Method ()[^]
I you check that link out you will have an example that is using a DataAdapter to fill a DataSet which is then bound to the GridView.

That said, this code snippet looks a bit strange to me.
C#
GridViewRow grid= grid.FooterRow;   // What is this code doing?


if (grid! = null)

The code above should not even compile. There should be a name conflict.
 
Share this answer
 
Comments
Hapisha 17-Nov-15 15:06pm    
Yeah there is a little mistake there. I was supposed to write
GridViewRow grid1= grid.FooterRow;
if (grid1! = null)
{
//
}

But, I also have another Grid which I bind the following way. That Grid is loading data. But this Grid Isn't.
As I seen your code in your designer for gridview you mapped the DataSourceId Property to SqlDataSource, and in your code behind you called your query, 2 ways you are pointing your database, which one you want.

If you want SqlDataSource then pass your select query in selectcommand property of sqldatasource.

If you want to bind from server side then remove DataSourceId property of gridview and then check.
 
Share this answer
 
Comments
Hapisha 17-Nov-15 14:14pm    
The sql query that i m writing on designer is for loading the gridview. and on the cs part I also have a query by which I m populating some lebel on my page(which i didn't include on my design).

So I need both the queries

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