locked
Using one table to compare with another table to display result is not working proparly RRS feed

  • Question

  • User-2074858223 posted

    Using one table to compare with another table to display result is not working proparly, Example see diagram below

    TABLE USERSTATUS
    --------------------------

    ====================================================================================================================
            UserName                       |  FriendUserName            |       Status  |                  VisibleStatus1          |          VisibleStatus2
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
             Steve1                        |               JAKE     |                          true          |                      CONNENTED          |              NOTCONNECTED
     ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    TABLE USER ACCOUNT
    
    ====================================================================================================================
            UserName                       | Name        |              
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
             Steve1                        |         phillip     |               
     ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    SUMMERY

    What i want to achieve here is this, i want to use UserAccount table to check table UserStatus and the display column VisibleStatus1 or VisibleStatus2 , so to that i will give an example, if UserName Steve in

    Table User Account is found in UserName column or FriendUserName column of table UserStatus then show column VisibleStatus1 on lable on page, but if UserName in User Account table is not found in

    UserName or FriendUserName columns in table UserStatus show column VisibleStatus2 on lable in page.
    So from code below i used status column in to determine wether to show VisibleStatus1 or VisibleStatus2 but i dont want to do that any longer, so i just need to modify the code to fit my new requerment

    CODE

    protected void Page_Load(object sender, EventArgs e)
    {
        string userName = Session["UserName"].ToString();
        if (!this.IsPostBack)
        {
            FormView1follow.DataSource = GetData(userName);
            FormView1follow.DataBind();
        }
    }
     
    private DataTable GetData(string userName)
    {
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        string query = "SELECT UserName,FriendUserName,FollowStatus,VisibleStatus1,VisibleStatus2 FROM USERFollow WHERE UserName = '" + userName + "'";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                con.Open();
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    sda.Fill(dt);
                }
                con.Close();
            }
        }
     
        return dt;
    }
     
    protected void OnDataBound(object sender, EventArgs e)
    {
        string userName = (FormView1follow.Row.Cells[0].FindControl("hfUserName") as HiddenField).Value;
        string friendsUserName = (FormView1follow.Row.Cells[0].FindControl("hfFriendsUserName") as HiddenField).Value;
        string followStatus = (FormView1follow.Row.Cells[0].FindControl("hfFollowStatus") as HiddenField).Value;
        LinkButton btnFollow = (LinkButton)FormView1follow.Row.Cells[0].FindControl("btnfollowFollow");
        LinkButton btnUnFollow = (LinkButton)FormView1follow.Row.Cells[0].FindControl("Unbtnfollow");
        if ((userName.Trim() != friendsUserName.Trim()) && (followStatus.ToLower().Trim() == "true"))
        {
            btnFollow.Visible = true;
            btnUnFollow.Visible = false;
        }
        else if ((userName.Trim() != friendsUserName.Trim()) && (followStatus.ToLower().Trim() == "false"))
        {
            btnFollow.Visible = false;
            btnUnFollow.Visible = true;
        }
    }

    HTML MARKUP

      <asp:FormView ID="FormView1follow" runat="server" OnDataBound="OnDataBound" Width="100%">
            <ItemTemplate>
                <asp:HiddenField ID="hfUserName" runat="server" Value='<%# Eval("UserName") %>' />
                <asp:HiddenField ID="hfFriendsUserName" runat="server" Value='<%# Eval("FriendUserName") %>' />
                <asp:HiddenField ID="hfFollowStatus" runat="server" Value='<%# Eval("FollowStatus") %>' />
                <asp:LinkButton ID="btnfollowFollow" runat="server" ToolTip="follow user" CssClass=" btn btn-twitter fa fa-user" Font-Bold="True" >
                    <i class=""  style="margin-right:2px" ></i>  <asp:Label ID="lblFollow" runat="server" Text='<%# Eval("VisibleStatus1") %>' />
                </asp:LinkButton>&nbsp;
                <asp:LinkButton ID="Unbtnfollow" runat="server" ToolTip="follow user" CssClass=" btn btn-twitter fa fa-user-plus" Font-Bold="True">
                    <i class=""  style="margin-right:2px" ></i>  <asp:Label ID="lblUnFollow" runat="server" Text='<%# Eval("VisibleStatus2") %>' />
                </asp:LinkButton></ItemTemplate>
        </asp:FormView>
    
    
    

    Friday, March 25, 2016 8:37 AM

Answers

  • User-271186128 posted

    Hi micah2012,

    First, you could query the database and get the table USERSTATUS and table USERACCOUNT, then, use LINQ where clause to check whether it contains the UserName.

    Please refer to the following code:

             DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("category", typeof(string)), new DataColumn("A10", typeof(Int32)), new DataColumn("A20", typeof(Int32)) });
                dt.Rows.Add('A', 1, 2);
                dt.Rows.Add('B', 5, 3);
                dt.Rows.Add('C', 2, 1);
                dt.Rows.Add('D', 8, 1);
                dt.Rows.Add('E', 7, 5);
    
                string name = "FF";
                var querycount = (from dd in dt.AsEnumerable()
                                  where dd.Field<string>("category") == name
                              select dd).Count();
    
                //if the count is 0, the datatable doesn't contains this name.
    

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 25, 2016 10:02 AM

All replies

  • User-271186128 posted

    Hi micah2012,

    First, you could query the database and get the table USERSTATUS and table USERACCOUNT, then, use LINQ where clause to check whether it contains the UserName.

    Please refer to the following code:

             DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("category", typeof(string)), new DataColumn("A10", typeof(Int32)), new DataColumn("A20", typeof(Int32)) });
                dt.Rows.Add('A', 1, 2);
                dt.Rows.Add('B', 5, 3);
                dt.Rows.Add('C', 2, 1);
                dt.Rows.Add('D', 8, 1);
                dt.Rows.Add('E', 7, 5);
    
                string name = "FF";
                var querycount = (from dd in dt.AsEnumerable()
                                  where dd.Field<string>("category") == name
                              select dd).Count();
    
                //if the count is 0, the datatable doesn't contains this name.
    

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 25, 2016 10:02 AM
  • User-2074858223 posted
    The string name FF is what?, is it the table name?
    Saturday, March 26, 2016 10:16 AM
  • User-271186128 posted

    Hi micah2012,

    The string name FF is what?, is it the table name?

    It is a filter value, the code is used to check whether the category column contains the "FF". You can change it to "A".

    Then, as we can see, the category column contains "A", not "FF".

    Best regards,
    Dillion

    Monday, March 28, 2016 1:59 AM