Master/Detail in 2.0

R

Rick

Using VWD, I am attempting to create a basic Master/Detail scenario
from a SQL database table that has two primary keys. The Master
control I'm using is a GridView, containing a list of records from the
table in question, with Select enabled. The Detail control is another
GridView. Assume a valid connection string. Here's the control code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="field_name" HeaderText="field_name"
SortExpression="field_name" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT [pkfield1], [field_name], [pkfield2] FROM
[dbtable] WHERE ([pkfield1] = @pkfield1)">
<SelectParameters>
<asp:parameter DefaultValue="5" Name="pkfield1" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
<asp:BoundField DataField="field_symbol" HeaderText="field_symbol"
SortExpression="field_symbol" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT pkfield1, pkfield2, field_symbol FROM
[dbtable] WHERE (([pkfield1] = @pkfield1) AND ([pkfield2] =
@pkfield2))">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="pkfield1"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="pkfield2"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

Assuming that the database objects are all properly structured and the
rest of the code for the page is okay, is there any obvious reason why
the above control code wouldn't produce a visible, populated second
(Detail) GridView ? I have played with many different versions on this
theme, and found that if there's a single parameter being used in the
WHERE for the second data source, it works fine. But with two WHERE
parameters, at least as I did it above, it doesn't work -- nothing
shows up.

Can anyone offer me some assistance with this ? Thanks.

Rick
 
B

Brock Allen

Since the key is a multi part key, the selectedvalue won't give you the right
thing. You'll proibabaly have to write a bit of code to break that key apart
and set it into the Paramater of the other sql data source.


Using VWD, I am attempting to create a basic Master/Detail scenario
from a SQL database table that has two primary keys. The Master
control I'm using is a GridView, containing a list of records from the
table in question, with Select enabled. The Detail control is another
GridView. Assume a valid connection string. Here's the control code:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="field_name" HeaderText="field_name"
SortExpression="field_name" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT [pkfield1], [field_name], [pkfield2] FROM
[dbtable] WHERE ([pkfield1] = @pkfield1)">
<SelectParameters>
<asp:parameter DefaultValue="5" Name="pkfield1" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
<asp:BoundField DataField="field_symbol" HeaderText="field_symbol"
SortExpression="field_symbol" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT pkfield1, pkfield2, field_symbol
FROM
[dbtable] WHERE (([pkfield1] = @pkfield1) AND ([pkfield2] =
@pkfield2))">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="pkfield1"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="pkfield2"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Assuming that the database objects are all properly structured and the
rest of the code for the page is okay, is there any obvious reason why
the above control code wouldn't produce a visible, populated second
(Detail) GridView ? I have played with many different versions on
this theme, and found that if there's a single parameter being used in
the WHERE for the second data source, it works fine. But with two
WHERE parameters, at least as I did it above, it doesn't work --
nothing shows up.

Can anyone offer me some assistance with this ? Thanks.

Rick
 
R

Rick

Thanks Brock, but I'm still lost. Any chance you could either show me
what you mean, or point me toward a resource that does ? This is all
new stuff to me (in the midst of the often challenging migration from
classic ASP) so I apologize for the hand-holding need. Cheers.

Rick


Brock said:
Since the key is a multi part key, the selectedvalue won't give you the right
thing. You'll proibabaly have to write a bit of code to break that key apart
and set it into the Paramater of the other sql data source.


Using VWD, I am attempting to create a basic Master/Detail scenario
from a SQL database table that has two primary keys. The Master
control I'm using is a GridView, containing a list of records from the
table in question, with Select enabled. The Detail control is another
GridView. Assume a valid connection string. Here's the control code:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="field_name" HeaderText="field_name"
SortExpression="field_name" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT [pkfield1], [field_name], [pkfield2] FROM
[dbtable] WHERE ([pkfield1] = @pkfield1)">
<SelectParameters>
<asp:parameter DefaultValue="5" Name="pkfield1" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
<asp:BoundField DataField="field_symbol" HeaderText="field_symbol"
SortExpression="field_symbol" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT pkfield1, pkfield2, field_symbol
FROM
[dbtable] WHERE (([pkfield1] = @pkfield1) AND ([pkfield2] =
@pkfield2))">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="pkfield1"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="pkfield2"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Assuming that the database objects are all properly structured and the
rest of the code for the page is okay, is there any obvious reason why
the above control code wouldn't produce a visible, populated second
(Detail) GridView ? I have played with many different versions on
this theme, and found that if there's a single parameter being used in
the WHERE for the second data source, it works fine. But with two
WHERE parameters, at least as I did it above, it doesn't work --
nothing shows up.

Can anyone offer me some assistance with this ? Thanks.

Rick
 
B

Brock Allen

You're going to have to manually extract each part of the multi part primary
key from the GridView. You can do this in the GridView's SelectedIndexChanged.
Once you have pk1 and pk2 then you'll need to set those into the values for
the SqlDataSource's SelectParameters.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int pk1 = (int)_grid.DataKeys[_grid.SelectedRow.RowIndex]["pkfield1"];
int pk2 = (int)_grid.DataKeys[_grid.SelectedRow.RowIndex]["pkfield2"];

SqlDataSource2.SelectParameters["pkfield1"].DefaultValue = pk1;
SqlDataSource2.SelectParameters["pkfield2"].DefaultValue = pk2;
}

This is all pseudo-code, so it'll be something like that. You could probabaly
also do this all in the SqlDataSource2's Selecting event as another option.


Thanks Brock, but I'm still lost. Any chance you could either show me
what you mean, or point me toward a resource that does ? This is all
new stuff to me (in the midst of the often challenging migration from
classic ASP) so I apologize for the hand-holding need. Cheers.

Rick

Brock said:
Since the key is a multi part key, the selectedvalue won't give you
the right thing. You'll proibabaly have to write a bit of code to
break that key apart and set it into the Paramater of the other sql
data source.

Using VWD, I am attempting to create a basic Master/Detail scenario
from a SQL database table that has two primary keys. The Master
control I'm using is a GridView, containing a list of records from
the table in question, with Select enabled. The Detail control is
another GridView. Assume a valid connection string. Here's the
control code:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="field_name" HeaderText="field_name"
SortExpression="field_name" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT [pkfield1], [field_name], [pkfield2] FROM
[dbtable] WHERE ([pkfield1] = @pkfield1)">
<SelectParameters>
<asp:parameter DefaultValue="5" Name="pkfield1" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False"
DataKeyNames="pkfield1,pkfield2"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="pkfield1" HeaderText="pkfield1"
ReadOnly="True" SortExpression="pkfield1" />
<asp:BoundField DataField="pkfield2" HeaderText="pkfield2"
ReadOnly="True" SortExpression="pkfield2" />
<asp:BoundField DataField="field_symbol" HeaderText="field_symbol"
SortExpression="field_symbol" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ValidConnectionString %>"
SelectCommand="SELECT pkfield1, pkfield2, field_symbol
FROM
[dbtable] WHERE (([pkfield1] = @pkfield1) AND ([pkfield2] =
@pkfield2))">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="pkfield1"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="pkfield2"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Assuming that the database objects are all properly structured and
the
rest of the code for the page is okay, is there any obvious reason
why
the above control code wouldn't produce a visible, populated second
(Detail) GridView ? I have played with many different versions on
this theme, and found that if there's a single parameter being used
in
the WHERE for the second data source, it works fine. But with two
WHERE parameters, at least as I did it above, it doesn't work --
nothing shows up.
Can anyone offer me some assistance with this ? Thanks.

Rick
 

alf

Joined
Jun 26, 2006
Messages
1
Reaction score
0
I'm argentine, so the poor english. Sorry. I've a very similar problem than Rick but the solution dind't wotk well for me. I've a gridview2 that takes field1 and field2 from GridView1 to execute a stored procedure with two parameters p_field1 and p_field2 of type sql_variant. Then I wrote:

protected void Res_Den_Filtros_SelectedIndexChanged(object sender, EventArgs e)
{
object IdHecho = (object)Res_Den_Filtros.DataKeys[Res_Den_Filtros.SelectedRow.RowIndex]["IdHecho"];
object CodSeg = (object)Res_Den_Filtros.DataKeys[Res_Den_Filtros.SelectedRow.RowIndex]["CodSeg"];

Personas.SelectParameters[0].DefaultValue = (string)IdHecho;
Personas.SelectParameters[1].DefaultValue = (string)CodSeg;
}
}

I'm not sure about the string but I dint't find how to leave them in type object. Thanks.

Alberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top