Master/Detail in 2.0

Discussion in 'ASP .Net' started by Rick, Oct 5, 2005.

  1. Rick

    Rick Guest

    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
     
    Rick, Oct 5, 2005
    #1
    1. Advertising

  2. Rick

    Brock Allen Guest

    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.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen

    > 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
    >
     
    Brock Allen, Oct 5, 2005
    #2
    1. Advertising

  3. Rick

    Rick Guest

    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 Allen wrote:
    > 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.
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    > > 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
    > >
     
    Rick, Oct 5, 2005
    #3
  4. Rick

    Brock Allen Guest

    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.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen

    > 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 Allen wrote:
    >
    >> 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.
    >>
    >> -Brock
    >> DevelopMentor
    >> http://staff.develop.com/ballen
    >>> 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
    >>>
     
    Brock Allen, Oct 5, 2005
    #4
  5. Rick

    alf

    Joined:
    Jun 26, 2006
    Messages:
    1
    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
     
    alf, Jun 26, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGVtZXRyaQ==?=

    Master - Detail Examples

    =?Utf-8?B?RGVtZXRyaQ==?=, Oct 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    672
    Saravana [MVP]
    Oct 20, 2003
  2. Doug McCann
    Replies:
    1
    Views:
    1,036
    William Brogden
    Aug 5, 2004
  3. asmj
    Replies:
    0
    Views:
    509
  4. Arun Kumar Menon

    Master Detail detail

    Arun Kumar Menon, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    204
    Arun Kumar Menon
    Aug 6, 2003
  5. Frederik Borg

    ASp.net data binding: Master-detail-detail

    Frederik Borg, Jun 9, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    362
    Frederik Borg
    Jun 9, 2006
Loading...

Share This Page