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: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())
{
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();
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)
{
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)
{
foreach (Control control in cell.Controls)
{
LinkButton button = control as LinkButton;
if (button != null && button.CommandName == "Delete")
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:";
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!