AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Formview/Gridview Master/Detail Gridview DataSource Null
Posted by AJones on  12/29/2009 2:56:41 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
Hello All,

I have a gridview inside the edittemplate of a formview.
I want the gridview to display in the formview when there are not records in the datatable so I am creating a dummy row. When I debug the dtHist.Row.Count is 1 after creating the new empty row
but the gridview DataSource is null so I get the famous Object reference not set to an instance of an object. NullReference.

Any Ideas ???
Thanks in Advance

private void MedFill()
{
DataTable dtHist = hist.Fetch(AcctNo, dttrig);
GridView gvMedHist = (GridView)fvHyal.FindControl("gvMedHist");
if (dtHist.Rows.Count > 0)
{
gvMedHist.DataSource = dtHist;
gvMedHist.DataBind();
}
else
{
dtHist.Rows.Add(dtHist.NewRow()); //create empty row
gvMedHist.DataSource = dtHist;
gvMedHist.DataBind();

int TotalColumns = gvMedHist.Rows[0].Cells.Count;
gvMedHist.Rows[0].Cells.Clear();
gvMedHist.Rows[0].Cells.Add(new TableCell());
gvMedHist.Rows[0].Cells[0].ColumnSpan = TotalColumns;
gvMedHist.Rows[0].Cells[0].Text = "No Record Found";
}
}

Post Your Reply Post New Question

 Viewer's Reply
Binding GridView inside FormView Control
Answered By Moderator1 on 12/29/2009 7:29:01 AM
Hi,

I checked your MedFill method. It works like gem. No problem in your code. Just make sure from where you call this method. It has to be called from the FormView's DataBound event as below,

protected void FormView1_DataBound(object sender, EventArgs e)
{
MedFill();
}

Reply me, if problem still exists.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks - Free 156 Page Preview
Integrating Silverlight 4 with SharePoint 2010 - Free 42 Page Sample Chapter
Introduction to Web Applications Development