How do you show many to many relationships in ASP.NET with datasources?
I can retrieve the information from the database easily but I can't manage to save that information into a variable so that i can then call for only information in the M2M table related to the ID Key I want. For example..
1<asp:Label ID="lblTestLabel" Text='<%# DataBinder.Eval(Container.DataItem, "title_id")%>' Runat="server" />23<script language="VB" runat="server">4sub Page_Load(sender as Object, e as EventArgs)5Dim lblTitleID As Label = Repeater1.FindControl("lblTestLabel")6Response.Write(lblTitleID.text)7End Sub8</script>
When I try to run this script it dosn't work and I get the error:
System.NullReferenceException: Object reference not set to an instance of an object.
For line 6 of the code above. Is there a way to just retrieve information from a datasource without storing information into a label first?
PS: I use sqldatasource tags to retrieve information like:
1<asp:SqlDataSource ID="DataSet1" runat="server"2ConnectionString="Driver={MySQL ODBC 3.51 Driver};server=localhost;database=;user=;pwd=;option=3;"3ProviderName="System.Data.Odbc"4SelectCommand="Select * from titles order by title_id">5</asp:SqlDataSource>
You should listen to the ItemDataBound event. That way, you will be able to do any modifications on an item before it is rendered.
See MSDN for reference and an example: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound.aspx
|||I managed to look at the sample code and break it down enough to where I can understand it all. I didn't know about testing the itemtypes before changing the label text. It helped out a lot. I still have some stuff to fix up, but I'm sure I can figure out the rest from here.
Thanks for the help.
No comments:
Post a Comment