0
How to use one button to insert true into column Status and when clicked the second time will update column Status with false
    This is the table clicked the first time
        ======================================================
        UserName                  |                 Status
       ---------------------------------------------------------------
         Steve1                    |                  false
     ----------------------------------------------------------------
    This is the table clicked the second time
        ============================================================
        UserName                  |                 Status
        ---------------------------------------------------------------
         Steve1                    |                  true
        ---------------------------------------------------------
        protected void btncount_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != string.Empty)
            {

                string username = Session["UserName"].ToString();
                Follow(username);
            }
            else
            {

            }

            public void Follow(string username)
            {


                string str = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
                string getADPOST = "Insert INTO USERFollow (UserName,FriendUserName,Status) values (@UserName,@Id,1)";

                using (SqlConnection con = new SqlConnection(str))
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                    {

                        cmd.Parameters.AddWithValue("@UserName", Session["UserName"].ToString());
                        cmd.Parameters.AddWithValue("@Id", Request.QueryString["Id"].ToString());
                        //  cmd.Parameters.AddWithValue("@FriendUserName", Request.QueryString["UserName"].ToString());
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataTable ds = new DataTable();
                        da.Fill(ds);
                    }
                }
        }
        }

Edited by migold

2
Contributors
1
Reply
16
Views
1 Year
Discussion Span
Last Post by AleMonteiro
0

I think the best option to you is to check if the user is already in the database.

Basically you could only update your insert query to be something like this:

IF ((SELECT Count(*) FROM USERFollow WHERE UserName = @UserName AND FriendUserName = @Id ) > 0 )
Begin
    UPDATE USERFollow SET Status = 0 WHERE UserName = @UserName AND FriendUserName = @Id 
end
Else
Begin
    Insert INTO USERFollow (UserName,FriendUserName,Status) values (@UserName,@Id,1)
End

You can also make the select separatly, get the result in C# and make the decision there. But I think it's cleaner doing it all one the SQL Query.

And one remark, you shouldn't use DataSet to make an insert or update, DataSets are for data retrieval. For executing commands, just use cmd.Execute().

Edited by AleMonteiro

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.