You could make PostID a UNIQUEIDENTIFIER column and then pass in a newly generated GUID (Guid.NewGuid()).
Also, please use parameterized queries to avoid SQL injection. Especially if the inputs come directly from WEB users.
To do so, change your ConnectDataBaseToInsert method to not take SQL text, but an SqlCommand which you prepare with the respective parameters.
From your comment to the question: The PostID should be like PO0001. Then the only way to do it properly and to respect for concurrency is to generate a stored procedure that takes the value to insert, which generates the ID itself.
To do so, create a new table that contains the last post ID. Then, use an UPDATE ... OUTPUT statement to increment and return in one go. This is the only way to do an atomic update of the post ID so that no two users create the same ID.
Example Table PostIDTable
Current ======= 0
Example SELECT to update and retrieve the current post ID:
-- We need a temp table, because OUTPUT can not output into a single variable -- This increments Current by one and outputs the value that was set in one go. -- This prevents simultaneous calls to get the same ID DECLARE #postID (ID INT) UPDATE PostIDTable OUPUT INSERTED.Current INTO #postID SET Current = Current + 1 -- Get the value from the temp table and convert it into the desired format DECLARE @pID INT = (SELECT TOP 1 ID FROM #postID) DECLARE @id NVARCHAR(6) = 'PO' + RIGHT('0000' + CONVERT(NVARCHAR, @pID), 4) -- Do the actual INSERT INSERT INTO (PostDet, Votes,UserID) VALUES (@id, ..., ...)
PostIDcreated as an autogenerated column in DB?PostIdfield an identity (if using SQL Server, unsure about other dbs) and it will autogenerate the value for you.