0

i try to use userControl to display SqlDataReader data.

in the main page

  public SqlDataReader Data2;

...

 <uc1:WebUserControl ID="WebUserControl1" RData1="<%=Data2 %>" runat="server" />

and in the userControl

  Repeater1.DataSource = RData1;

        Repeater1.DataBind();



     <asp:Repeater ID="Repeater1" runat="server">    <ItemTemplate> 
 <div class="row">   <b> 
 <%#DataBinder.Eval(Container.DataItem,
 "replay_subject")%></b><br />   
 <%#DataBinder.Eval(Container.DataItem,
 "replay_text")%><hr/> </div>   
 </ItemTemplate>
      </asp:Repeater>

But i keep getting this error

Cannot create an object of type 'System.Data.SqlClient.SqlDataReader' from its string representation '<%=Data2 %>' for the 'RData1' property.

1 Answer 1

4

You can't assign RData1 using that inline code on the ASPX. The compiler try to convert Data2 to a string representation in order to set the property and RData1 is expecting a SqlDataReader so it fails.

You have to assign it on the code behind like this

WebUserControl1.RData1 = Data2;
Sign up to request clarification or add additional context in comments.

6 Comments

it didn't recognize WebUserControl1 object from the aspx file
@Bob: You have to assign the property on the codebehind, on the CS
i did. in the same page the <uc1:WebUserControl ID="WebUserControl1" runat="server" /> is. but it didn't recognize it. any reason?
Can you paste a little more of the ASPX? Specifically, the code around WebUserControl1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ask_show.aspx.cs" Inherits="Default2" %> <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="w3.org/1999/xhtml"> <body> <div> <uc1:WebUserControl ID="WebUserControl1" runat="server" /> </div> <asp:Label ID="lbl_tags" runat="server" ></asp:Label> <br /> <asp:Label ID="lbl_reply_table" runat="server" style="color: #FF0000" ></asp:Label>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.