Remove or change the zero before table of contents

With the KOMA-Script class scrbook there is no need to load package tocbibind.

The TOC entries of the lists can be done by option listof=totoc. And if you really want an entry for the TOC in the TOC you can use \setuptoc{toc}{totoc}.

To ensure that \lstlistoflistings respects settings of KOMA-Script option listof load package scrhack.

\documentclass[
  listof=totoc
]{scrbook}
\setuptoc{toc}{totoc}
\usepackage{scrhack}
\usepackage{listings}
\usepackage{chngcntr}

\DeclareTOCStyleEntry[
  level=1,
  indent=1.5em,
  numwidth=4em
]{tocline}{lstlisting}

\usepackage{xpatch}
\makeatletter
\AtBeginDocument{%
  \counterwithin{lstlisting}{subsection}%
  \xpatchcmd{\fnum@lstlisting}{\thelstlisting}{\thelstlisting\autodot}{}{\PatchFailed}%
}
\makeatother


\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter[foo]{stuff}
\chapter{Foo stuff}
\section{Foo section}
\subsection{Foo subsection}
\begin{lstlisting}[caption={A Listing}]
Boo
\end{lstlisting}
\listoffigures
\lstlistoflistings
\end{document}

enter image description here


If you really want the special numbering of the listings you could use

\documentclass[
  listof=totoc
]{scrbook}
\setuptoc{toc}{totoc}
\usepackage{scrhack}
\usepackage{listings}
\usepackage{chngcntr}

\DeclareTOCStyleEntry[
  level=1,
  indent=1.5em,
  numwidth=4em
]{tocline}{lstlisting}

\usepackage{xpatch}
\makeatletter
\AtBeginDocument{%
  \counterwithin*{lstlisting}{subsection}%
  \renewcommand\thelstlisting{%
    \ifnum\value{section}=0
      \thechapter
    \else
      \ifnum\value{subsection}=0
        \thesection
      \else
        \thesubsection
      \fi
    \fi
    .\arabic{lstlisting}%
  }%
  \xpatchcmd{\fnum@lstlisting}{\thelstlisting}{\thelstlisting\autodot}{}{\PatchFailed}%
}
\makeatother

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter[foo]{stuff}
\begin{lstlisting}[caption={A Listing in Chapter 1}]
  Boo
\end{lstlisting}
\chapter{Foo stuff}
\begin{lstlisting}[caption={A Listing in Chapter 2}]
  Boo
\end{lstlisting}
\section{Foo section}
\begin{lstlisting}[caption={A Listing in Section 2.1}]
  Boo
\end{lstlisting}
\subsection{Foo subsection}
\begin{lstlisting}[caption={A Listing in Subsection 2.1.1}]
  Boo
\end{lstlisting}
\listoffigures
\lstlistoflistings
\end{document}

enter image description here


The command \tocchapter lets \tableofcontents use \chapter instead of \chapter* heading, so this will lead to 0 Contents

However, it is a really bad idea to change \tableofcontents since listings hijacks \tableofcontents and will use basically the same code for \listoflistings.

In my point of view the redefinition of \tableofcontents should not be done at all, since tocbibind already adds LoF, ToC etc. to the ToC (if not prevented with notoc etc.

The \counterwithin statements before \section are not necessary, since \counterwithin{lstlisting}{subsection} is sufficient, since subsection is in the counter reset list of section and chapter, so a resetting of any of those counters will reset lstlisting as well.

listings defines the lstlisting counter only at \AtBeginDocument so \counterwithin can't be used before \begin{document} for this counter.

\documentclass{book}

\usepackage{listings}
\usepackage{chngcntr}
\usepackage{tocbibind}


% Actually not really needed!
% \AtBeginDocument{
%   \renewcommand*{\tableofcontents}{%
%    \begingroup
%    \tocfile{\contentsname}{toc}
%    \endgroup
%  }
%}



%\input{header}

\makeatletter
\AtBeginDocument{%
  \counterwithin{lstlisting}{subsection}
  \def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{4em}{#1}{#2}}
}
\makeatother

\begin{document}


\frontmatter


%\include{kapitel/titelseite_Standard}

%\include{kapitel/erklaerung}


\tableofcontents


\mainmatter

\chapter[foo]{stuff}
\chapter{Foo stuff}

\section{Foo section}

\subsection{Foo subsection}

\begin{lstlisting}[caption=Foo]
Boo
\end{lstlisting}

\subsection{Other foo subsection}

\begin{lstlisting}[caption=Foo again]
Boo
\end{lstlisting}

\section{Other foo section}

\begin{lstlisting}[caption=Foo again]
Boo
\end{lstlisting}

\begin{lstlisting}[caption=Foo again \thesection]
Boo
\end{lstlisting}


\chapter{Other foo chapter}


\begin{lstlisting}[caption=Foo again]
Boo
\end{lstlisting}



%\input{kapitel/Einleitung}


%\input{kapitel/Grundlagen}
%\input{kapitel/Programmierumgebungen}
%\input{kapitel/Analyse}
%\input{kapitel/Implementierung}
%\input{kapitel/Test}
%\input{kapitel/Fazit}
%\input{kapitel/anhang}

\listoffigures
\lstlistoflistings
\bibliography{biblio/biblio}

\end{document}

enter image description here