First , i want ask you excuses for my bad english.

Well, i'm a beginner in c# but i need it to my work in this time.

I have a SqlDataSource1 with "SELECT field1 from table1"
I have an Accordion linked with SqlDataSource1, and inside of Accordion1 i have a SqlDataSource2 with "Select field2 from table2 where id=!!!!! here i need use field1 from SqlDataSource1!!!!!!"

If i put in SqlDataSource2 a explicit value it run good. But i don`t get put like parameter in SqlDataSource2 the value of field1 of SqlDataSource1.

Field1 (value1)
Field2
Field2
Field1(value2)
Field2
Field2
Field2
...
...
The code:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:XEUSERACO %>"
ProviderName="<%$ ConnectionStrings:XEUSERACO.ProviderName %>"
OnSelecting="SqlDataSource1_Selecting"
SelectCommand="SELECT field1 from table1"
>


<cc1:Accordion ID="Accordion2" runat="server" SelectedIndex="0" FadeTransitions="true"
FramesPerSecond="40" TransitionDuration="250" AutoSize="None" [B]DataSourceID="SqlDataSource1"[/B] ContentCssClass="Content" HeaderCssClass="HEADER">
<HeaderTemplate>
<table border="1">
<tr>
<td>
[B]<%# Eval("Field1") %>[/B]

</td>
</tr>
</table>
</HeaderTemplate>
<ContentTemplate>
<table border="1">
<tr>
<td>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:XEUSERACO %>"
ProviderName="<%$ ConnectionStrings:XEUSERACO.ProviderName %>" OnSelecting="SqlDataSource2_Selecting"
SelectCommand="SELECT field2 from table2 where [B]id=!!!!here i need put param field1 rom SqlDataSource1!!!!!!"[/B] >


</asp:SqlDataSource>

<asp:Repeater runat="server" DataSourceID="SqlDataSource2" ID="articles">
<ItemTemplate>
[B]<%# Eval("field2") %>[/B]
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</ContentTemplate>
</cc1:Accordion>

Thank you very much.

Dani AI

Generated

Short summary and the safe fix for this layout: the inner data source must get the current row's Field1 from the same naming container (each Accordion pane is a separate container). As pointed out, a parameter is the right idea; avoid building SQL strings by concatenation (the approach you showed with e.Command.CommandText is brittle and opens you to SQL injection). Two practical ways work reliably inside a templated control:

  • Declarative (no code-behind): bind a small server control in the same template to the current row and use a ControlParameter so the SqlDataSource reads that control's value.
  • Programmatic: in the item data-binding step locate the inner Repeater/SqlDataSource, read the current data item, and bind the child repeater with a parameterized query or helper method.

Example (declarative) — put this inside the Accordion template so the controls share the same naming container:

<asp:HiddenField ID="hfField1" runat="server" Value='<%# Eval("Field1") %>' />

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
    SelectCommand="SELECT field2 FROM table2 WHERE id = @field1Param">
  <SelectParameters>
    <asp:ControlParameter Name="field1Param" ControlID="hfField1" PropertyName="Value" Type="String" />
  </SelectParameters>
</asp:SqlDataSource>

Notes and troubleshooting:

  • Place the HiddenField (or Label) and SqlDataSource in the same template (Header vs Content), otherwise the ControlParameter may not find the control.
  • If you prefer code-behind, handle the accordion’s item-binding and call a parameterized data helper to populate the inner Repeater rather than changing SQL text at runtime.
  • If there are many outer rows, beware of N+1 queries — consider fetching parent+child rows with a single JOIN and grouping client-side for better performance.
  • For : follow ’s advice to use parameters; stop concatenating values into SQL.

Recommended Answers

All 5 Replies

If you are using sqldatasource control you need to set the select statment of the SqlDataSource2 like this:

SELECT field2 FROM table2 WHERE id = @id

And then when you want to populate your Accordion1 you need to do like this:

this.SqlDataSource2 .SelectParameters["id"].DefaultValue = "the value that you want in string of course";
            DataView dv = (DataView)this.SqlDataSource2.Select(DataSourceSelectArguments.Empty);

And then do what ever you like with DataView!

If you are using sqldatasource control you need to set the select statment of the SqlDataSource2 like this:

SELECT field2 FROM table2 WHERE id = @id

And then when you want to populate your Accordion1 you need to do like this:

this.SqlDataSource2 .SelectParameters["id"].DefaultValue = "the value that you want in string of course";
            DataView dv = (DataView)this.SqlDataSource2.Select(DataSourceSelectArguments.Empty);

And then do what ever you like with DataView!

Thanks, but the principal problem is how can i get the field1 value from SqlDataSource1 to put it in "the value that you want in string of course"?.
Kind Regards.
Thanks very much again.

Explain exactly what you want, and i mean post some code here and the structure of your DB so i will be able to help more.

Explain exactly what you want, and i mean post some code here and the structure of your DB so i will be able to help more.

If you was to Spain i invite you a full night of cups.:icon_smile:

In my .cs i have this:

protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
   e.Command.CommandText = "SELECT field2 from table2 where id='" + p1 + "'";     
          
}

This code is executed for each row of SqlDataSource1 and if i put a fixed String in p1 it run well.

But i need put instead p1 the value of field1 from table1 of actually row of SqlDataSource1.

It's posible in this method get actually values from row of SqlDatasource1 to get the value of a field?.

Kind Regards again.

oK, it'is not solved, but i think that i'm guilty for my explication of the problem. So i'll open a new thread with a simple question.
Thanks very much all people who try to help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.