DataBinding problems

M

Mundo

Hi

I'm pulling my hair out with a problem caused when DataBinding a
DataSet to a web control. I first tried using a GridView but when that
failed, I tried a Repeater. I'm getting the classic
"'System.Data.DataRowView' does not contain a property with the name
X" error message which suggests I'm not putting X in the SELECT
statement. But this is not the case. If I set the GridView to
AutoGenerateColumns="true", it works fine and all the columns in my
SELECT statement appear without problem. But if I try to customise the
columns and use <asp:BoundField/> tags, it all goes pear shaped.
Here's some code:

Data Component:
~~~~~~~~~~~~
private DataSet FillDataSet(string query, string tableName)
{
OdbcConnection con = new OdbcConnection(dmsConString);
OdbcDataAdapter adapter = new OdbcDataAdapter(query, con);

DataSet ds = new DataSet();
try
{
con.Open();
adapter.Fill(ds, tableName);
}
catch (Exception err)
{
//throw err;
}
finally
{
con.Close();
}
return ds;
}

public object GetVehicles(string dealerID)
{
dmsConString = GetDealerConString(dealerID);
string query = "SELECT VehicleDescriptio001,
StockbookNumber, RegistrationNumber ";
query += "FROM VS_" + dealerID + "_UsedVehicleStock ";
query += "WHERE ACSStatus = 'A' AND deliverydate IS NULL";
return FillDataSet(query, "Vehicles");
}


..aspx page:
~~~~~~~~
<asp:GridView ID="gridVehicles" runat="server"
AutoGenerateColumns="False" AllowSorting="False" Font-Size="10pt"
DataKeyField="StockbookNumber" DataKeyNames="StockbookNumber" >
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True"
ForeColor="#F7F7F7" />
<SelectedRowStyle BackColor="#4A3C8C" ForeColor="#E7E7FF" />
<Columns>
<asp:BoundField HeaderText="Description"
DataField="VehicleDescriptio001" />
<asp:BoundField HeaderText="Stock No."
DataField="StockbookNumber" />
<asp:BoundField HeaderText="Reg. No."
DataField="RegistrationNumber" />
</Columns>
</asp:GridView>


..aspx.cs page:
~~~~~~~~~~
protected void lstDealers_SelectedIndexChanged(object sender,
EventArgs e)
{
if (lstDealers.SelectedValue != "")
{
DatabaseComponent.SytnernetDBUtil DB = new
DatabaseComponent.SytnernetDBUtil();

try
{
gridVehicles.DataSource =
DB.GetVehicles(lstDealers.SelectedValue.ToString());
gridVehicles.DataBind();
}
catch (Exception err)
{
lblError.Text = "Exception encountered getting
vehicles: " + err.Message;
}
pnlVehicles.Visible = true;
}
}


Exact error message:
~~~~~~~~~~~~~~~
System.Web.HttpException was unhandled by user code
Message="DataBinding: 'System.Data.DataRowView' does not contain a
property with the name 'StockbookNumber'."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.DataBinder.GetPropertyValue(Object container,
String propName)
at System.Web.UI.DataBinder.Eval(Object container, String[]
expressionParts)
at System.Web.UI.DataBinder.Eval(Object container, String
expression)
at System.Web.UI.TemplateControl.Eval(String expression)
at ASP.getusedvehicles_aspx.__DataBind__control12(Object
sender, EventArgs e) in c:\Documents and Settings\edmundward\My
Documents\Visual Studio 2005\Projects\VehicleData
\GetUsedVehicles.aspx:line 39
at System.Web.UI.Control.OnDataBinding(EventArgs e)
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.Control.DataBindChildren()


As you can see I'm definitely SELECTing StockbookNumber in my query,
and the VehicleDescriptio001 field comes back fine, whatever approach
I take. I thought it might be something to do with the ODBC driver I
was using to connect to the remote database, so I tried printing out
the columns in the DataSet to see what they were called from within
there, using the following code in my
lstDealers_SelectedIndexChanged() function:

DataSet ds =
(DataSet)DB.GetVehicles(lstDealers.SelectedValue.ToString());

DataColumnCollection columns =
ds.Tables["Vehicles"].Columns;

foreach (DataColumn column in columns)
{
lblError.Text += column.ColumnName + " : " +
column.DataType + "<br />";
}


This prints out all three column names quite happily as expected. This
suggests to me that the columns are fine in the DataSet, so it can't
be any problem with what the ODBC driver is returning. Am I right in
making this assumption? What else can I try to get to the nitty gritty
of what is going on? Please, please, please somebody out there help if
you can.

Cheers
Mundo
 
M

Mundo

Does anybody know how I can view the names of the columns in the
DataSet just before the binding attempt is made? There must be some
properties of some object I can print out. I just can't find the right
one.
Mundo
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top