Search code examples
sqlasp.netwebformsoledb

How to replace IDs of users with their fullNames in the current scenario?


I have two databases with the following schema:

tbl_users(userID, fullName, emailID, password)

userID is the Primary Key

tbl_savings(savingsID, creditorName, amount, interestRate, interestType, interestCalculationMethod, ownerID, dataEnteredByID) 

savingsID is the Primary Key.

ownerID is a Foreign Key on userID of tbl_users with a one-to-many relationship.

ownerID specifies the ID of the owner of the saving. Similarly, dataEnteredByID specifies the ID of the person who entered the data.

I have a GridView with the following specification:

<asp:GridView ID="GridViewSavingsTracker" AutoGenerateColumns="false" runat="server" DataKeyNames="savingsID" OnRowCommand="GridViewSavingsTracker_RowCommand" AllowPaging="true" OnSelectedIndexChanged="GridViewSavingsTracker_SelectedIndexChanged" OnPageIndexChanging="GridViewSavingsTracker_PageIndexChanging" PageSize="10">
    <Columns>
        <asp:CommandField ShowSelectButton="true" />
        <asp:BoundField DataField="creditorName" HeaderText="Creditor Name" />
        <asp:BoundField DataField="amount" HeaderText="Principal Amount" />
        <asp:BoundField DataField="interestRate" HeaderText="Interest Rate" />
        <asp:BoundField DataField="interestType" HeaderText="Interest Type" />
        <asp:BoundField DataField="interestCalculationMethod" HeaderText="Interest Calculation Method" />
        <asp:BoundField DataField="insertedDate" HeaderText="Date" />
        <asp:BoundField DataField="ownerID" HeaderText="Owner" />
        <asp:BoundField DataField="dataEnteredByID" HeaderText="Data Entered By" />

    </Columns>
</asp:GridView>

What I want to do: Instead of printing the ownerID and dataEnteredByID, I want to print the fullNames of the corresponding IDs. How can I do that?

What I already have: I have the following server side code for GridView. I have a method ShowGridView() that populates the data into the GridView from the database(s).

protected void ShowGridView()
{
    string connectionString = GetConnString();
    OleDbConnection con = new OleDbConnection(connectionString);
    con.Open();

    string showGridViewQuery = "(SELECT s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName FROM tbl_savings s LEFT JOIN tbl_users u ON s.ownerID = u.usersID) UNION (select s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName from tbl_savings s LEFT JOIN tbl_users u ON s.dataEnteredByID = u.usersID)";

    OleDbCommand showGridViewCommand = new OleDbCommand(showGridViewQuery, con);
    showGridViewCommand.ExecuteNonQuery();
    DataTable dt = new DataTable();
    OleDbDataAdapter olda = new OleDbDataAdapter(showGridViewCommand);
    olda.Fill(dt);

    GridViewSavingsTracker.DataSource = dt;
    GridViewSavingsTracker.DataBind();

    con.Close();
}

Solution

  • You can create a separate code-behind method that gets the full name and call it from the gridview.

    Create an ItemTemplate for the field. The Text property calls the method. Replace UserID with your field's name.

    <ItemTemplate>
        <asp:Label ID="lblUserNameItem" runat="server" 
            Text='<%# GetUserNameByID(Eval("UserID").ToString()) %>'></asp:Label>
    </ItemTemplate>
    

    Code behind:

    protected string GetUserNameByID(string userID)
    {
        // your data-code...
        ... uname = GetUserByID(int.Parse(userID));
        return uname;
    }
    

    Do the same with the other id, with the same method or another.