How can I stop this from happening?

It seems that I have a hell of a time getting the first row right. I cast on and when I’m starting the first row, I get this long piece of thread between the two needles and it messes up the knitting. How can I fix this?

Here’s an example of what’s happening:

I suspect you’re doing the backward loop cast on? That always happens unless it’s just a few stitches. Try using a different one like the knitted cast in if you don’t want to tackle long tail yet. There are videos at the link at the top of the page.