proper use of repeater control question
-
Hi, I'm hoping you guys can help me with a question. I've been reviewing someone else's code, and I found that in some places where they are using a repeater to display data from a database, in places where the Database query will only ever return 1 row of data. They are returning a reader, loading a datatable with the reader data, and binding that datatable to a repeater. now am I nutz, or is that extremely inefficient? it's always been my practice to take the reader, check for rows, and then set individual controls (like literals) to the fields directly. the only reason I can think of to do this, is that if for some reason there isn't any data, the repeater won't display anything. however I've always handled this by wrapping the results in a div, and setting the div's visibility property to false if there is no data returned. please let me know your opinion. thank you
-
Hi, I'm hoping you guys can help me with a question. I've been reviewing someone else's code, and I found that in some places where they are using a repeater to display data from a database, in places where the Database query will only ever return 1 row of data. They are returning a reader, loading a datatable with the reader data, and binding that datatable to a repeater. now am I nutz, or is that extremely inefficient? it's always been my practice to take the reader, check for rows, and then set individual controls (like literals) to the fields directly. the only reason I can think of to do this, is that if for some reason there isn't any data, the repeater won't display anything. however I've always handled this by wrapping the results in a div, and setting the div's visibility property to false if there is no data returned. please let me know your opinion. thank you
There are better ways of the doing that, but it probably doesn't matter. As you say you can always count the rows in the data table. If no rows exists display another div. If this application is going to have 10K+ users then you might want to look at doing this in a more efficient way. But really it is probably fine. However, you really should not use a repeater if you are only going to have one record. That's just common sense, it is called a repeater for a reason. Also, why use a datareader to get a datatable. This is foolish. Use the dataAdapter to get a dataTable. Use DataReader to iterate through read only data or bind a control directly to a datareader object.
I didn't get any requirements for the signature
-
There are better ways of the doing that, but it probably doesn't matter. As you say you can always count the rows in the data table. If no rows exists display another div. If this application is going to have 10K+ users then you might want to look at doing this in a more efficient way. But really it is probably fine. However, you really should not use a repeater if you are only going to have one record. That's just common sense, it is called a repeater for a reason. Also, why use a datareader to get a datatable. This is foolish. Use the dataAdapter to get a dataTable. Use DataReader to iterate through read only data or bind a control directly to a datareader object.
I didn't get any requirements for the signature
Thanks Todd you mentioned a better way? do you mean a better way than binding the reader data to literals? if so, would you mind giving more details? This particular page get's 4-5k views daily, and will see more as the application gets expanded, and marketed more, so I will be re-writing this page at least. I have no idea why they are retrieving the data like that, my guess is the person who wrote it didn't know any other way of binding data than by using a datatable. I have a feeling I'm going to find lots more WTF's when I get the final codeset and can look at everything together.
-
Thanks Todd you mentioned a better way? do you mean a better way than binding the reader data to literals? if so, would you mind giving more details? This particular page get's 4-5k views daily, and will see more as the application gets expanded, and marketed more, so I will be re-writing this page at least. I have no idea why they are retrieving the data like that, my guess is the person who wrote it didn't know any other way of binding data than by using a datatable. I have a feeling I'm going to find lots more WTF's when I get the final codeset and can look at everything together.
icewolf_snowfire wrote:
do you mean a better way than binding the reader data to literals? if so, would you mind giving more details?
Yes I did. It depends on your individual situation, but if you are only viewing one record I wouldn't use any databinding. Just put the controls you need inside a div tag. Write an init method to load the data (obviously only during the initial load and not postbacks). If the data is constantly changing then use a dataReader or output parameters in a stored procedure. If the data is rather static then cache the data. I wrote a sample article about how to do that here[^] Also, check this out http://msdn.microsoft.com/en-us/library/ms998549.aspx[^]
I didn't get any requirements for the signature