I want to insert some data in a database that fetches the information to insert from the Form that I created. I am using the following command:
String query = @"INSERT INTO Visits(Name,Surname,DocType,DocNumber,Gender,Company,Delivery,Entrance,Visiting) VALUES( "
+ NameBox.Text.Split(null)[0]+ " , "
+ SurnameBox.Text.Split(null)[0] + " , "
+ type + " , "
+ Convert.ToDouble(idNBox.Text) + " , "
+ gender + "," + Companybox.Text + " , "
+ delivery + " , " + DateTime.Now + " ,"
+ VisitingCombo.Text
+ " )";
The table was created with the following structure,
CREATE TABLE [dbo].[Visits] (
[Name] NVARCHAR (50) NOT NULL,
[Surname] NVARCHAR (50) NOT NULL,
[DocType] NVARCHAR (50) NOT NULL,
[IdNumber] BIGINT NOT NULL,
[Gender] NCHAR (1) NOT NULL,
[Company] NVARCHAR (50) NOT NULL,
[Delivery] BIT NOT NULL,
[Entrance] DATE NOT NULL,
[Out] DATE NULL,
[Visting] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([IdNumber] ASC, [DocType] ASC, [Entrance] ASC),
FOREIGN KEY ([Visting]) REFERENCES [dbo].[Person] ([Name]));
When running the code I try to insert the following error appears,
System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.'
For the insertion I used some code that I found in a response by Nicholas Carey that could solve my problem. How to directly execute SQL query in C#? Have example batch file
I know that the error occurs in the very first value of the query. What am I doing wrong in here?