[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Binding Multiple records to a Repeater Control in ASP.NET

Posted on 2005-04-15
13
Medium Priority
?
2,038 Views
Last Modified: 2008-03-10
Hi geeks
I am working on a ASP.NET datacentric project and using VB.NET.
I have a repeater control in a populatedata method which has to populate records onto four columns in a  HTML table. The dataset I need to use to populate data comes from a stored procedure. And that dataset again has two tables(two select statements in the SP).

Any help regarding how to  bind the data to the repeater control will be very much appreciated.
 How can I pass the data to the datasource method ?
0
Comment
Question by:hardlearner
  • 6
  • 4
  • 3
13 Comments
 
LVL 2

Expert Comment

by:jellis613
ID: 13792336
you need to combine the two select statements into one - try using either a JOIN or a UNION command (depending on the nature of the date - what data is contained in the two tables? can you give us the schemas for the two tables?).
0
 

Author Comment

by:hardlearner
ID: 13792415
Thanx Jellis.... I used JOIN and now I have a single table coming into the datset. Now.... How do I go about binding the datset to the repeater control?
0
 
LVL 2

Assisted Solution

by:jellis613
jellis613 earned 240 total points
ID: 13792439
OleDBConnection conn = new OleDBConnection(connectString);
OleDBCommand comm = new OleDBCommand(sql,conn);
conn.Open();
repeater1.DataSource = comm.ExecuteReader();
repeater1.DataBind();
conn.Close();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:hardlearner
ID: 13792503
Hi Jellis
Thanx for your quick response. I have a problem. I dont need to populate all the records from the stored procedure but a selected 4 items.

I am not sure if the following statement is right but this is what I want. The repeater should be able to access selected items from the DATASET.

      something like this....
                                   repeater1.datasource=(dataset1.Tables(0).Rows(0).Items("Name","Phone","Address","etc")
0
 
LVL 2

Expert Comment

by:jellis613
ID: 13792516
I think that a better approach would be to change you Select statement so that it only returns the items that you need. If you need help with this, please let me know what type of info you need, and what is the schemas of the tables, etc.
0
 

Author Comment

by:hardlearner
ID: 13792557
The webpage that I need to populate has several fields and controls. The SP returns some 20 records out of which a few need to be populated into the repeater control. I have several pages like this and I'm to use only one SP per page. So i cant think about creating a separate SP for this Repeater control alone. Or is that the only option ?
0
 
LVL 2

Expert Comment

by:jellis613
ID: 13793256
how about this:
1) in the sp, add a parameter "@forRepeater bit = 0"
2) in the WHERE clause of the SELECT statement for the sp, add a line like "...AND (@forRepeater=0 OR XXXXXX)
3) Replace XXXXXX with clauses that will specifically identify the rows that are needed for the repeater. That way, when the SELECT statement is evaluated, XXXXX will only be used as a clause for the SELECT if @forRepeater=1
4) Then when you are calling the sp for the repeater, be sure to add a Parameter @forRepeater, set to 1
0
 
LVL 5

Expert Comment

by:ovalsquare
ID: 13794475
jellis613's suggestion is the good way to go, but if for some reason you can't change your SP, you can run a select statement on your dataset in your code:

Dim JobRows() As DataRow = WipMaster.Select("Job = '14566'")

Which you can then bind to your repeater.

Ted

0
 

Author Comment

by:hardlearner
ID: 13794592
Hi oval...

I tried jellis' suggestion but I cant get it running. I just wanted to give your suggestion a try. Can you emphasize it a bit. Whats that wipmaster?
Say I have to SELECT 3 records Name,Phone, Address from the dataset. how do I go about it?

It would be great if you can explain?
0
 
LVL 5

Expert Comment

by:ovalsquare
ID: 13794636
Sorry, just grabbed a snippet of code from a project.

Dim rows() as DataRow = YourTableName.Select("Phone = '123-123-1234'")

As to SELECT 3, do you mean top 3? As to YourTableName, if you're dealing with a dataset, you have to get the table in the DataSet. I.e.

Dim rows() as DataRow = dataset1.tables(0).Select("Phone = '123-123-1234'")

Ted
0
 

Author Comment

by:hardlearner
ID: 13794787
Hi Ted

Thanx a lot ... I think I am almost there. Sorry for not being precise.

I have only one table and one row of data thats being returned into the dataset from through the stored procedure.

Can I Select values from 3 different columns from the table in the dataset ?

like this.....

I have abc, def,xyz columns in the HTML table(tblContent) which needs to be populated and ABC,DEF,XYZ are the dataitems of the repeater control(which are actually some of the columns in the table from the datset)

Dim dr() As DataRow = dataset1.Tables(0).Select("ABC='abc'", "DEF='def'", "XYZ='xyz'")
repeater1.datamember="tblContent"
repeater1.datasource=dr
repeater1.databind()

please correct my select statement

0
 
LVL 5

Accepted Solution

by:
ovalsquare earned 260 total points
ID: 13795404
Now I finally understand your question - you only want to return the one row of data into the repeater? Just realised this at the end of doing all the stuff below so will leave it, but you can completely forget about the select statement. You simply bind the datatable as your datasource and then place the <%# DataBinder.Eval(Container.DataItem, "ABC")%> reference (with the proper column reference) within your aspx page for each column's data.
Thus you are using your repeater not to repeat, but just display one row of data.

If you have more than one row, it will still work, but this time it will loop through each record and display each record's data (as in the following).

-----------------------------------
No need to select that way unless you specifically want to select on each of those items.

First off, to clarify, with a repeater control, you need to specify which item goes where by specifying the container dataitem within the repeater control. For example, you simply bind all the records you want to your repeater control by specifying your datasource (in this case your collection of datarows), and then in the actual body of your repeater, in the .aspx page that is, place the following (assuming a column heading in your original table is "ABC" and another column heading is "DEF", with the primary key column being "XYZ")

<h1>Record <%# DataBinder.Eval(Container.DataItem, "XYZ")%></h1>
<p>This is column ABC's data for this record: <%# DataBinder.Eval(Container.DataItem, "ABC")%></p>
<p>This is column DEF's data for this record: <%# DataBinder.Eval(Container.DataItem, "DEF")%></p>

This would simply repeat for each record in your datasource. Therefore the following would be the output if you only had three records in your datasource:

<h1> Record 1</h1>
<p>This is column ABC's data for this record: This Record's data in ABC column</p>
<p>This is column DEF's data for this record:  This Record's data in DEF column</p>
<h1> Record 2</h1>
<p>This is column ABC's data for this record: This Record's data in ABC column</p>
<p>This is column DEF's data for this record:  This Record's data in DEF column</p>
<h1> Record 3</h1>
<p>This is column ABC's data for this record: This Record's data in ABC column</p>
<p>This is column DEF's data for this record:  This Record's data in DEF column</p>


Okay? For setting in columns, you would just need to layout in <table><tr><td> etc. tags.
-----------------------------------

Sorry the above was confusing. Didn't think it was worth rewriting after I realised you only had one row of data.

Ted



0
 

Author Comment

by:hardlearner
ID: 13796074
Thanks a lot Jellis and Ted...It is all working. I really appreciate your effort and time. Keep it up

Raghu
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question