nathan

nathan

  • NA
  • 15
  • 0

Insert SQL c#

Dec 4 2006 11:09 AM
Hi peeps, although i'm not new to C# i am new when it comes to using C# to connect to the SQL server and access a database.

Firstly i have made a nice program using OleDb to connect to a local database which works perfectly well with no irregularities, however when i have come to create a Company Contact list and put it on the SQL server we use my statements never work for some reason. They are identical to the statements i used when programming my program in OleDB but for some reasons i keep getting Syntax errors for the EXACT same statements when i'm using the SQL connection rather than the OleDb Connection...

So to be even more basic the SQL strings that work PERFECT using OleDb connection don't work using the SQL connection. Here is a sample one, which throws this error:

SqlException: Line 1: incorrect syntax near '?'.

Here's the event that fires the error:

private void buttonAddNew_Click(object sender, System.EventArgs e)
{
string addNum =
"INSERT INTO ContactList ([Address], [FullName] ,[DirectLine] ,[Mobile]) "+
"VALUES(?,?,?,?)";

sqlInsertCommand1.CommandType = CommandType.Text;
sqlInsertCommand1.CommandText = addNum;
sqlInsertCommand1.Connection = connAdd;

sqlInsertCommand1.Parameters.Add("@addressadd",SqlDbType.Char,255,"Address").Value = comboBoxDepartment.Text;
sqlInsertCommand1.Parameters.Add("@nameadd",SqlDbType.Char,255,"FullName").Value = txtName.Text;
sqlInsertCommand1.Parameters.Add("@directlineadd",SqlDbType.Char,255,"DirectLine").Value = txtDirectLine.Text;
sqlInsertCommand1.Parameters.Add("@mobileadd",SqlDbType.Char,255,"Mobile").Value = txtMobile.Text;

connAdd.Open();
try
{
sqlInsertCommand1.ExecuteNonQuery();
adapterAdd.Fill(dataSet1.ContactList);
dataGrid1.DataSource = dataSet1.ContactList;
}

catch(Exception e1)
{
MessageBox.Show(e1.ToString());
}
connAdd.Close();
sqlInsertCommand1.Parameters.Clear();
}
}

Please can someone help!? I first need telling why it's different when using SQL connection and also why this syntax error keeps firing because it's so annoying!!!

Anyhelp is appreciated
Kind Regards
Nath


Answers (1)