Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem populating DataGrid with results of DataSet

Status
Not open for further replies.

pgosse

Programmer
Sep 26, 2001
42
0
0
CA
Hi, folks. I'm new to C# and the .NET framework, so if I've made a stupid mistake that's causing this problem please forgive and bear with me.

I have a simple query that I'm trying to output in a datagrid. The query is executing correctly, the correct number of results is displayed, and the datagrid is indeed generated -- it's just not being filled with the data.

Here's the code for the datagrid in the .aspx file:

Code:
<asp:datagrid id="dgSearchResults" runat="server" AutoGenerateColumns="False" CellPadding="4"
	GridLines="Horizontal" ShowHeader="False" Width="98%" BorderStyle="Dashed">
	<AlternatingItemStyle BorderWidth="1px" BorderStyle="Solid" BorderColor="Transparent" BackColor="#EEEEEE"></AlternatingItemStyle>
	<Columns>
		<asp:BoundColumn HeaderText="Grant Description">
			<HeaderStyle Width="35%"></HeaderStyle>
		</asp:BoundColumn>
		<asp:BoundColumn HeaderText="Granting Agency">
			<HeaderStyle Width="35%"></HeaderStyle>
		</asp:BoundColumn>
		<asp:BoundColumn HeaderText="Principle Investigator">
			<HeaderStyle Width="20%"></HeaderStyle>
		</asp:BoundColumn>
		<asp:BoundColumn HeaderText="Additional Researchers">
			<HeaderStyle Width="5%"></HeaderStyle>
		</asp:BoundColumn>
		<asp:BoundColumn HeaderText="Grant Amount">
			<HeaderStyle Width="5%"></HeaderStyle>
		</asp:BoundColumn>
	</Columns>
</asp:datagrid>

And here's the section of the C# code that is filling (or not filling) the datagrid (not that string strSQL is ommitted to save space):

Code:
private void btnSearch_Click(object sender, System.EventArgs e)
{
	string strSQL;

	using (OracleConnection dbConn = new OracleConnection(strLogin))
	{
		dbConn.Open();

		using (OracleCommand dbCmd = new OracleCommand(strSQL))
		{
			dbCmd.Connection = dbConn;
			dbCmd.CommandType = CommandType.Text;

			dbAdp = new OracleDataAdapter();
			dbAdp.SelectCommand = dbCmd;

			DataSet dsGrants = new DataSet();

			dbAdp.Fill(dsGrants, "result");

			dgSearchResults.DataSource = dsGrants.Tables[0].DefaultView;
			dgSearchResults.DataMember = "result";
			dgSearchResults.DataBind();

			lblError.Text = dsGrants.Tables[0].DefaultView.Count.ToString();

			dbCmd.Dispose();
		}

		dbConn.Close();

	dbConn.Dispose();

	}
}

The grid is output, there are four rows with five cells in each row, but there is no data in the cell. I know the data is being returned as I output the number of records returned in the lblError label.

Can anyone give me an idea as to where I've gone wrong here? I'm assuming it's something small that I'm missing, but I've looked through a plethora of tutorials and guides on this and can't see what I'm overlooking.

Thanks very much in advance.

Cheers,
Pablo
 
try this:
Code:
dgSearchResults.DataSource = dsGrants;
            dgSearchResults.DataMember = "result";
            dgSearchResults.DataBind();

or
Code:
dgSearchResults.DataSource = dsGrants.Tables["result"].DefaultView;
            dgSearchResults.DataBind();
you should only need a datamember if you specify only a dataset (I believe). If the DataSource is a Table or View, you should leave DataMember null.

From the MSDN class library:
If the DataSource is a DataTable, DataView, collection, or array, setting the DataMember property throws an exception.
 
Tried both but the result is the same.

I don't know that this is the problem, as there is no exception being thrown. The datagrid is displayed, the correct number of rows is printed, hell the colors even alternate!

But there's no data in them ...

In doing this I'm actually rewriting some code that was written in VB.NET, and in that code they use a DataMember so I was just assuming that this would also be the case in C#.

Any other ideas?

Cheers and TIA,

Pablo
 
OH...

hmm.. maybe your font color? I've never seen the rows show up blank before. Could be all null values in your datarows for some reason??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top