tkendall57
asked on
Urgent 500 points! SqlException: Incorrect syntax near '?'
Does anyone see what's wrong with this code snippet that would cause an "SqlException: Incorrect syntax near '?'" when executing the DataAdapter.Update method?
private Guid SetupProject(SqlConnection msSql, string projectName)
{
Guid guid;
// See if the project exists
SqlDataAdapter da = new SqlDataAdapter(
"SELECT id, name FROM Project WHERE name = '" + projectName + "'",msSql);
DataSet ds = new DataSet();
da.Fill(ds, "Project");
if (ds.Tables[0].Rows.Count == 0)
{
// Project does not exist, add it.
da.InsertCommand = msSql.CreateCommand();
da.InsertCommand.CommandTe xt =
"INSERT INTO Project (id, name) VALUES (?,?)";
da.InsertCommand.Connectio n = msSql;
da.InsertCommand.Parameter s.Add(
new System.Data.SqlClient.SqlP arameter(" id", SqlDbType.UniqueIdentifier , 0, "id"));
da.InsertCommand.Parameter s.Add(
new System.Data.SqlClient.SqlP arameter(" name", SqlDbType.NVarChar, 0, "name"));
DataRow newRow = ds.Tables[0].NewRow();
guid = System.Guid.NewGuid();
newRow["id"] = guid;
newRow["name"] = projectName;
ds.Tables[0].Rows.Add(newR ow);
da.Update(ds, "Project");
}
else
guid = (Guid)ds.Tables[0].Rows[0] ["id"];
ds.Dispose();
da.Dispose();
return guid;
}
private Guid SetupProject(SqlConnection
{
Guid guid;
// See if the project exists
SqlDataAdapter da = new SqlDataAdapter(
"SELECT id, name FROM Project WHERE name = '" + projectName + "'",msSql);
DataSet ds = new DataSet();
da.Fill(ds, "Project");
if (ds.Tables[0].Rows.Count == 0)
{
// Project does not exist, add it.
da.InsertCommand = msSql.CreateCommand();
da.InsertCommand.CommandTe
"INSERT INTO Project (id, name) VALUES (?,?)";
da.InsertCommand.Connectio
da.InsertCommand.Parameter
new System.Data.SqlClient.SqlP
da.InsertCommand.Parameter
new System.Data.SqlClient.SqlP
DataRow newRow = ds.Tables[0].NewRow();
guid = System.Guid.NewGuid();
newRow["id"] = guid;
newRow["name"] = projectName;
ds.Tables[0].Rows.Add(newR
da.Update(ds, "Project");
}
else
guid = (Guid)ds.Tables[0].Rows[0]
ds.Dispose();
da.Dispose();
return guid;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.