application of Ampere's circuital law for finite thin straight current carrying conductor [duplicate]

Why can we not apply ampere circuital law in case of finite ,thin, straight current carrying conductor as we do in case of infinite length of current carrying wire? when I apply for finite straight wire I get the same value of magnetic field as in case of infinte one. Which is wrong. Why is this happening?

It is not a matter of not being allowed to use Ampere's law rather it is a matter of finding that applying Ampere's law correctly is not easy.

Ampere's law can be written as

$$\oint\mathbf{B}\cdot d\mathbf{S}=\mu_0 I + \mu_0\epsilon_0\frac{\partial}{\partial t}\iint\mathbf{E}\cdot d\mathbf{A}$$

You have a wire of finite length carrying a constant current $I$ but have not stated how that current is generated.
What is required is charge being moved from one end of the wire to the other end.

If it actually is a finite length of wire then the amount of charge at each end of the wire has to change with time.
One end of the wire becoming more positive whilst the other end becoming more negative.
The charges at each end of the wire will produce a time varying electric field which passes through the amperian loop and so must be accounted for in the $\mu_0\epsilon_0\frac{\partial}{\partial t}\iint\mathbf{E}\cdot d\mathbf{A}$ term.

If you have wires connected to a battery at each end of your finite length wire those wires will produce extra magnetic fields through the amperian loop which have to be included in the $\oint\mathbf{B}\cdot d\mathbf{S}$ term.