I keep getting an error that says Incorrect syntax near ','. When I try to insert values into a database table. Here's the code:
===================================================================
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
public class MemberFileReader
{
private MemberFileReader()
{
}
public static void ParsePositional(string FileName)
{
String strSQLInsert = "INSERT INTO Members VALUES(" +
tempCType+ "," +tempNRDS+ "," +tempLastName+ "," +
tempFirstName+ "," +tempMiddleName+ "," +tempStatus+ "," +
tempEmail+ ")";
System.Data.SqlClient.SqlConnection connStr =
new System.Data.SqlClient.SqlConnection();
connStr.ConnectionString = "data source=WALTERPPK\SQLEXPRESS;Integrated Security=True;AttachDBFilename=C:\\Documents and Settings\\ugo\\My Documents\\Visual Studio 2005\\Projects\\DataExchange_3\\DataExchange_3\\stlCR_temp.mdf;user instance=true";
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = strSQLInsert;
cmd.Connection = connStr;
connStr.Open();
cmd.ExecuteNonQuery();
connStr.Close();
}
}
==============================================================
Any ideas
Thx

Error while trying insert values into a database table. Help!
royalhale
M Thomas
jshepler
Thanks Guys/Gals.
I updated the strings in the code and it worked.
chakravarthy_b
Yes it should be:
String strSQLInsert = "INSERT INTO Members VALUES('" +
tempCType+ "', '" +tempNRDS+ "', '" +tempLastName+ "', '" +
tempFirstName+ "', '" +tempMiddleName+ "', " +tempStatus+ ", '" +
tempEmail+ "')";
But you're syntax for your insert is also incorrect...it should be:
String strSQLInsert = "INSERT INTO Members(Ctype, NRDS, LastName, FirstName, MiddleName, Status, Email)
VALUES(' +
tempCType+ ", '" +tempNRDS+ "', '" +tempLastName+ "', '" +
tempFirstName+ "', '" +tempMiddleName+ "', " +tempStatus+ ", '" +
tempEmail+ "')";
The above assumes tempCtype and tempStatus are not strings.
Adamus