views:

307

answers:

3

I am creating gridviews using C# and ASP.net ... I need help in creating 2 gridviews which they have same processID and getting data from different SQL tables in the same database..When I click on the one first row of the first gridview it should give me the list of files with the same processID and its details in the second gridview

One process ID has many different xml files stored in it for example processID = 7A413EA4-8ECE-472D-92BE-F58C22E5C567 in the first gridview so all the xml files with the username, date, login date having the same processID in the second table should show up on selecting the row in the first table

This is what I have so far done:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FS_PS2FS._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>PS2FS Client</title>
</head>
<body bgcolor="lightgrey">
    <form id="form1" runat="server">
    <div>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
        <asp:Label ID="heading" runat="server" Font-Bold="True" Font-Names="Times New Roman"
            Font-Size="XX-Large" Font-Underline="True" ForeColor="MidnightBlue" Text="PS2FS CLIENT" CssClass="normal" Height="41px" Width="227px" BorderStyle="Outset" BorderWidth="5px"></asp:Label><br />
        <br />
        <br />
        <asp:GridView ID="LOG_GridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            CellPadding="4" DataSourceID="FS_PS2FS" ForeColor="#333333" Height="300px" Width="943px" HorizontalAlign="Center" CssClass="AlternatingRowStyle" PageSize="6" SelectedIndex="0" OnSelectedIndexChanging="LOG_GridView_SelectedIndexChanging" BorderColor="#999999">
            <FooterStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" CssClass="normal" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" CssClass="RowStyle" />
            <EditRowStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="normal" Font-Names="Cambria" BackColor="#999999" />
            <SelectedRowStyle BackColor="#FFFFC0" Font-Bold="True" ForeColor="#333333" BorderStyle="Outset" CssClass="SelectedRowStyle" BorderColor="Yellow" />
            <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" VerticalAlign="Middle" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="HeaderStyle" />
            <AlternatingRowStyle BackColor="White" HorizontalAlign="Center" ForeColor="#284775" />
            <Columns>
                <asp:CommandField HeaderText="Select" ShowSelectButton="True" />
                <asp:BoundField DataField="LOG_ProcessId" HeaderText="Process ID" SortExpression="LOG_ProcessId" />
                <asp:BoundField DataField="LOG_Id" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="LOG_Id" />
                <asp:BoundField DataField="LOG_FileName" HeaderText="File Name" SortExpression="LOG_FileName" />
                <asp:BoundField DataField="LOG_Date" HeaderText="Date" SortExpression="LOG_Date" />
                <asp:BoundField DataField="LOG_Description" HeaderText="Description" SortExpression="LOG_Description" />
                <asp:BoundField DataField="LOG_UserId" HeaderText="User ID" SortExpression="LOG_UserId" />
            </Columns>
        </asp:GridView>
        &nbsp;
        <asp:SqlDataSource ID="FS_PS2FS" runat="server" ConnectionString="<%$ ConnectionStrings:FreightsmartWebConnection %>"
            SelectCommand="SELECT [LOG_Id], [LOG_FileName], [LOG_Description], [LOG_Date], [LOG_ProcessId], [LOG_Details], [LOG_UserId] FROM [FS_LOG_PS2FS] ORDER BY [LOG_Date] DESC">
        </asp:SqlDataSource>
        <br />


</div>
    <asp:SqlDataSource ID="PS2FS_FS" runat="server" ConnectionString="<%$ ConnectionStrings:FreightsmartWebConnection %>"
        SelectCommand="SELECT [P2FV_ID], [P2FV_MessageType], [P2FV_MessageText], [P2FV_FileName], [P2FV_ProcessId], [P2FV_CreateDate], [P2FV_CreateUser] FROM [FS_LOG_PS2FS_Validation] ORDER BY [P2FV_CreateDate] DESC">
    </asp:SqlDataSource>

    &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
    <asp:GridView ID="LogValidation_GridView" runat="server" AutoGenerateColumns="False" CellPadding="4"
        DataSourceID="PS2FS_FS" ForeColor="#333333" Height="300px" Width="940px" AllowPaging="True" HorizontalAlign="Center" PageSize="6" OnRowUpdated="LogValidation_GridView_RowUpdated" BorderColor="#999999">
        <FooterStyle BackColor="#CCCCCC" Font-Bold="True" ForeColor="Black" CssClass="normal" />
        <Columns>
            <asp:BoundField DataField="P2FV_ProcessId" HeaderText="Process ID" SortExpression="P2FV_ProcessId" Visible="False" />
            <asp:BoundField DataField="P2FV_ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                SortExpression="P2FV_ID" />
            <asp:BoundField DataField="P2FV_MessageType" HeaderText="Message Type" SortExpression="P2FV_MessageType" />
            <asp:BoundField DataField="P2FV_MessageText" HeaderText="Message Text" SortExpression="P2FV_MessageText" />
            <asp:BoundField DataField="P2FV_FileName" HeaderText="File Name" SortExpression="P2FV_FileName" />
            <asp:BoundField DataField="P2FV_CreateDate" HeaderText="Create Date" SortExpression="P2FV_CreateDate" />
            <asp:BoundField DataField="P2FV_CreateUser" HeaderText="Create User" SortExpression="P2FV_CreateUser" />
        </Columns>
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" BorderStyle="Outset" CssClass="normal" Wrap="True" />
        <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
    </asp:GridView>

</form>

A: 

You could do that the following way:

When you click on one row from Grid 1 you should trigger an event on codebehind file (look at SelectedIndexChanged shown bellow). Inside this event you should query your XML file table to find the ones that have the same process Id of the row selected on Grid 1. With the result of such a query you bind it to Grid 2.

  void LOG_GridView_SelectedIndexChanged(Object sender, EventArgs e)
  {
    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = CustomersGridView.SelectedRow;

    // Do your logic here to query and databind to Grid 2...
  }

Write this in your Grid1 declaration:

<asp:GridView ID="LOG_GridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            CellPadding="4" DataSourceID="FS_PS2FS" ForeColor="#333333" Height="300px" Width="943px" HorizontalAlign="Center" CssClass="AlternatingRowStyle" PageSize="6" SelectedIndex="0" OnSelectedIndexChanging="LOG_GridView_SelectedIndexChanging" BorderColor="#999999" onselectedindexchanged="LOG_GridView_SelectedIndexChanged">

For more information on events of GridView, look at these pages at MSDN:

GridView..::.SelectedRow Property

GridView Class

Leniel Macaferi
A: 

Basically you want to create a Master/Detail screen... is this homework? On any case, you want to add an event (selected index changed) to the first grid. Then add code there that will either set the data source or "filter" the data in the second grid, you might also choose to make the second grid visible at this point if is not already.

The data source of the second grid needs a "WHERE" clause which will compare the processID in your datasource with the one selected in the first grid. You can set the first grid's data key to be this field (processID) for easy retrieval.

Good luck!

Ricardo
no its not my homework I am working and got stuck as im not used to c#.net mostly worked with java
sweetsecret
Cool! it was more of a j/k :)
Ricardo
i really need help in it ... as im stuck and not able to figure out what to do. It is a master/detail grdview i wud say
sweetsecret
the problem is that the processId in in the 1st table is named LOG_ProcessId and in the second table P2FV_ProcessId how can do it this way
sweetsecret
A: 

You should be able to do this all from markup. Here is a working example using the Northwind database

Define the master GridView:

<asp:GridView ID="master" runat="server" DataSourceID="sdsMaster" 
        AutoGenerateColumns="False" DataKeyNames="OrderID" AllowPaging="True" >
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" 
            ReadOnly="True" SortExpression="OrderID" />
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
            SortExpression="CustomerID" />
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
            SortExpression="EmployeeID" />
        <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" 
            SortExpression="OrderDate" />
        <asp:BoundField DataField="RequiredDate" HeaderText="RequiredDate" 
            SortExpression="RequiredDate" />
        <asp:BoundField DataField="ShippedDate" HeaderText="ShippedDate" 
            SortExpression="ShippedDate" />
        <asp:BoundField DataField="ShipVia" HeaderText="ShipVia" 
            SortExpression="ShipVia" />
        <asp:BoundField DataField="Freight" HeaderText="Freight" 
            SortExpression="Freight" />
    </Columns>
    </asp:GridView>

Define the details GridView:

<asp:GridView ID="detail" runat="server" DataSourceID="sdsDetail" 
        AutoGenerateColumns="False" DataKeyNames="OrderID,ProductID" >
    <Columns>
        <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" 
            SortExpression="OrderID" />
        <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
            SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
            SortExpression="UnitPrice" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
            SortExpression="Quantity" />
        <asp:BoundField DataField="Discount" HeaderText="Discount" 
            SortExpression="Discount" />
    </Columns>
    </asp:GridView>

Here is the SqlDataSource for the master table. Just basic stuff to select and display the records:

<asp:SqlDataSource ID="sdsMaster" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia], [Freight] FROM [Orders]" />

Now in the details SqlDataSource, use a SQL where clause bound to the selected primary key in the master GridView:

<asp:SqlDataSource ID="sdsDetail" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
        SelectCommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity], [Discount] FROM [Order Details] WHERE ([OrderID] = @OrderID)" >
    <SelectParameters>
        <asp:ControlParameter ControlID="master" DefaultValue="0" Name="OrderID" 
            PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

Notice the SelectParameters and the use of a ControlParameter which looks for the data key of the selected record in the master GridView.

EDIT Since you are using your own database with different field names then modifiy the SQL accordingly.

FROM [FS_LOG_PS2FS_Validation] WHERE ([P2FV_ProcessId] = @LOG_ProcessId)

and change the SelectParameter

<SelectParameters>
    <asp:ControlParameter ControlID="master" DefaultValue="0" Name="LOG_ProcessId" 
        PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>

Also, you will need to set the DataKeyNames attribute in the master GridView

  <asp:GridView ID="master" runat="server" DataSourceID="sdsMaster" 
            AutoGenerateColumns="False" DataKeyNames="LOG_ProcessID" AllowPaging="True" >
        <Columns>

I don't think you need to set the DataKeyNames of the detail GridView. You can change it there too for completeness. I used the smart tag in Visual Studio 2008 design view to generate the example code. Let me know if this isn't clear.

EDIT 2 Here are some links that describe configuring ASP.NET data controls.

Master/Detail Using a Selectable Master GridView with a Details DetailView

Querying Data with the SqlDataSource Control

Data Access Tutorials

DaveB
In your example you have the OrderID in both the table in mycase i mean question in the 1st table the processID is named LOG_ProcessId and in the second table P2FV_ProcessId how can do it this way
sweetsecret
I have updated my answer. I hope this makes things clearer.
DaveB
Let me try if it helps thanks alot
sweetsecret
i cant change the name of the fields set in table though its my own database as this databse is used in different other programms handled by other people in the company any other way i can do other than modifying the sql
sweetsecret
No need to change your field names. You NEED to replace the SQL I have shown in my sample code with your own. I recomend using the smart tag(that's what I call it anyways) for your controls in Visual Studio's design mode to configure the controls. If you want more information on this aspect. I have added some links to my answer.
DaveB
i tried doing it the way you said it gives me a server errorServer Error in '/' Application.Object must implement IConvertible. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Object must implement IConvertible.
sweetsecret
Source Error:An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
sweetsecret
Stack Trace: [InvalidCastException: Object must implement IConvertible.] System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +2514354 System.Web.UI.WebControls.Parameter.GetValue(Object value,
sweetsecret
String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +264 System.Web.UI.WebControls.Parameter.get_ParameterValue() +66 System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +254 System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +276
sweetsecret
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +754 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +17 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
sweetsecret
System.Web.UI.WebControls.GridView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +69 System.Web.UI.Control.EnsureChildControls() +87 System.Web.UI.Control.PreRenderRecursiveInternal() +41 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Control.PreRenderRecursiveInternal() +161 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360
sweetsecret
@DaveB: Do you configure all your controls directly in the markup? I tend to use the designer more, any advantage in not using designer mode?
GenEric35
@GenEric: I too use the designer which will generate a lot of the boilerplate markup for me. If the task is straightforward, I tend to use the designer/markup.
DaveB