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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datalist binding from multiple tables

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
I have a dataset that returns multiple tables. Some of these tables have the same column names. I'm trying to bind the columns to itemtemplate of datalist. How can I tell it to bind Table1 ColumnA instead of Table3 ColumnA?

I've played with the Container.DataItem but can't get it to work. Any advice? Thanks
 
I am not sure how you are constructing your binding code. I tested using the properties window. I saw both tables with the same column. I just picked the column from the table I wanted.
 
could you elaborate please. Are you talking about picking the column after you created a dataset via visual studio designer? I created the dataset via code:

Codebehind:
Dim cmd as new sqlcommand("sproc1", conn)
Dim da as new sqldataapdapter(cmd)
Dim ds as dataset
da.Fill(ds)
Dim cmd2 as new sqlcommand(sproc2", conn)
da = new sqldqldataadapter(cmd2)
da.Fill(ds)

aspx page I usually bind via
<%#Databinder.Eval(container.Dataitem, "columnName")%>

but since I have same columnNames in different table how can I set the column name for a particular table.




 
<%# DataBinder.Eval(ds, "Tables[<table name>].DefaultView.[0].<ColumnName") %>
 
I've tried the above but it returns 'ds' is not declared.

Also tried:
<%# DataBinder.Eval(Container.DataItem, "Tables(FunChi7).DefaultView.[0].Hourly") %>

but returns 'System.Data.DataRowView' does not contain a property with the name Tables.

Am I missing something here? Thanks
 
Make your dataset (ds) a page level dataset. In other words, don't declare it in a Sub or Event, declare it at the top of the page.
 
k, getting closer. The following:

<%# Container.DataItem("Tables(FunChi7).DefaultView.[0].Hourly") %>

returns: Tables(FunChi7).DefaultView.[0].Hourly is neither a DataColumn nor a DataRelation for table FunChi7.

but at least it's recognizing the table. Just need the syntax to pinpoint the column.
 
You mean declaring the dataset within the aspx page. I would rather not do that, as all my code will be in a codebehind.

As in my previous posting, it's already directing to the correct table. Just need to figure out how to reference the column now.
 
No, what I mean is the scope of the dataset. If you declare it in a sub, it is only valid within that sub. If you then try to reference it outside of the sub, yoiu will get the "not defined" error. So, make it a page level variable. Declare ds as dataset after all your Imports statements, but before any event code.
 
Sorry for taking so long, had to work on another project.

I've used: <%# DataBinder.Eval(ds, "Tables[<table name>].DefaultView.[0].<ColumnName") %>

But this returns the firstrow of the datatable for every record in the list. It doesn't read through each row.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top