There is no obligation to use the same solicitor for both transactions. However, in most cases it makes sense to do so, particularly when you are simultaneously buying and selling a house. Not only will it usually be much cheaper to use one firm of solicitors rather than two, but both transactions are likely to go much more smoothly when a single firm has oversight of both transactions.
It’s often a delicate exercise to coordinate multiple parties, lenders, and others to ensure contracts are exchanged appropriately, mortgage funds are released at the correct time and searches are updated ahead of completion, so it helps if one solicitor is acting in both the sale and purchase.
Note that the same solicitor should not act for a seller and purchaser of the same property: solicitors must always act in the best interest of their clients, and to act for a seller and purchaser simultaneously is likely to jeopardise that. They also owe a duty of confidentiality to each individual client.