At wits end! Please help: gridview.databind() gives no data

L

Lynn

Please help!

My gridview displays with two empty rows. There is no error message,
just no data. I am at wits end and really wondering why we work so
hard to have our lives made so easy by these pre-packaged controls.

I have a gridview with 4 bound columns. Each column is defined as
bound and has been given a header title, but that is all. I also have
a dataset with two rows, 4 columns each. I have verified that the
dataset has data. Yet the DataBind method does not seem to work.

Thanks for any help.

public partial class DataGridPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindViaDataHelper();
}
}

public void BindViaDataHelper()
{
DatabaseHelper objDataHelper = new DatabaseHelper();
string[] arrProcName = new string[1];
string[] arrTableName = new string[1];
arrProcName[0] = "Select PICKERUPPER.FNAME,
PICKERUPPER.LNAME, PICKERUPPER.PHONE, RELATIONSHIP.NAME from
PICKERUPPER, RELATIONSHIP where PICKERUPPER.RELATIONSHIPID =
RELATIONSHIP.ID order by PICKERUPPER.LNAME";
arrTableName[0] = "AuthorizedPickerUppers";

DataSet ds = objDataHelper.GetDataSet(arrProcName,
arrTableName);
this.GridView1.DataSource = ds;
if (ds.Tables[0].Rows.Count > 0)
{
this.GridView1.DataBind();
}
else
{
// do something else
}
}
 
M

Mark Rae [MVP]

if (!Page.IsPostBack)
{
BindViaDataHelper();
}

So you don't want that method to run when the page is posted back - what
happens then...?

if (ds.Tables[0].Rows.Count > 0)

Set a breakpoint on that line - what is the value of the Count property...?
 
L

Lynn

Mark!

Got that, thanks for tryign to help! Now a new issue has come up:

A field or property with the name 'PICKERUPPER.ID' was not found on
the selected data source.

Here is my SQL:

"Select PICKERUPPER.ID, FNAME, LNAME, PHONE, NAME from PICKERUPPER,
RELATIONSHIP where PICKERUPPER.RELATIONSHIPID = RELATIONSHIP.ID order
by LNAME";

so it is there, and yet it isn't being understood by the gridview for
some reason. Does it not like the dot in it, perhaps?

here is my gridview definition
<asp:GridView ID="GridView1" runat="server"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="PICKERUPPER.ID"
HeaderText="ID" />
<asp:BoundField DataField="FNAME" HeaderText="First
Name" />
<asp:BoundField DataField="LNAME" HeaderText="Last
Name" />
<asp:BoundField DataField="PHONE" HeaderText="Phone" /<asp:BoundField DataField="RELATIONSHIP"
HeaderText="Relationship" />
<asp:ButtonField Text="Edit" />
<asp:ButtonField Text="Update" />
<asp:ButtonField Text="Cancel" />
</Columns>
</asp:GridView>
 
M

Mark Rae [MVP]

A field or property with the name 'PICKERUPPER.ID' was not found on
the selected data source.
so it is there, and yet it isn't being understood by the gridview for
some reason. Does it not like the dot in it, perhaps?

It's not the dot that is causing the problem - it's the fact that column
headers don't include the name of the table from which they are fetched.

PICKERUPPER.ID is fine in the SQL query because that tells the database
engine to fetch the ID field from the PICKERUPPER table as opposed to the
RELATIONSHIP table.

However, when the database engine populates the recordset, the column header
is just ID, not PICKERUPPER.ID - and this is what is being returned to
ASP.NET, so your bound control will not find a column called PICKERUPPER.ID

Do this instead:
<asp:BoundField DataField="PICKERUPPER.ID" HeaderText="ID" />

<asp:BoundField DataField="ID" HeaderText="ID" />

If you like, you could amend your SQL to be "SELECT PICKERUPPER.ID AS
PICKERUPPER_ID, .........

and then change your markup to:

<asp:BoundField DataField="PICKERUPPER_ID" HeaderText="ID" />
 
L

Lynn

Mark

Good idea, this, and I used it successfully. Thanks! Now one more... I
am trying to populate a dropdown listbox in my datagrid, and I get
this error:

DataBinding: 'System.Data.DataRowView' does not contain a property
with the name 'relationship'.

I am not sure what this means at all or how to fix it. Here is my
aspx's xml, and thanks for your help. I am an old-school programmer
who would rather do it myself. I do find the new tools easy once you
know them but learning them takes as long as it used to take to do it
yourself!

anyway, thanks again

<asp:TemplateField HeaderText="Relationship"
SortExpression="relationship">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSource="<%# PopulateDropDownList() %>"
DataTextField="relationship" DataValueField =
"RelationshipID">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
 
L

Lynn

PS if it helps here, is my populate function

public DataSet PopulateDropDownList()
{
DatabaseHelper objDataHelper = new DatabaseHelper();
string[] arrProcName = new string[1];
string[] arrTableName = new string[1];
arrProcName[0] = "SELECT ID, Name FROM Relationship";
arrTableName[0] = "Relationships";

DataSet ds = objDataHelper.GetDataSet(arrProcName,
arrTableName);
return ds;
}
 
M

Mark Rae [MVP]

DataBinding: 'System.Data.DataRowView' does not contain a property
with the name 'relationship'.

I am not sure what this means at all

It means exactly what it says - it can't find a field called 'relationship'.

Which is not surprising since, according to your additional post, you're
using the following SQL: "SELECT ID, Name FROM Relationship";
or how to fix it.

.... DataTextField="Name" DataValueField = "ID">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top