Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


dataAdapter.Update problem

  • This topic is locked This topic is locked
2 replies to this topic

#1 BobLewiston


  • Members
  • 69 posts
  • Local time:11:40 AM

Posted 21 March 2009 - 03:09 PM

I can read in an SQL table ("Person.Contact") from AdventureWorks and step through it one row at a time, but when I try to save a record, either one I'm inserting or one I'm editting, I get the following exception:

Incorrect syntax near ','. Must declare scalar variable "@ContactID".

Here's the code:
private void btnSave_Click (object sender, EventArgs e)
			DataRow row = dataTable.Rows [currentRecord];
			row.BeginEdit ();

			// get data from input TextBoxes
			row ["ContactID"]	= txtContactID.Text;
			row ["FirstName"]	= txtFirstName.Text;
			row ["LastName"]	 = txtLastName.Text;
			row ["Phone"]		= txtPhone.Text;
			row ["EmailAddress"] = txtEmailAddress.Text;

			row.EndEdit ();

			try { dataAdapter.Update (dataSet, "Person.Contact"); }		// HERE'S THE PROBLEM
			catch (Exception exc) { MessageBox.Show (exc.Message); }

			dataSet.AcceptChanges ();
I don't think the problem is with inializing the SQL commands. Here's the code for that (shown without the "Delete SQL Command" section). No exceptions are thrown.

private void InitializeCommands ()
			// Preparing Insert SQL Command
				dataAdapter.InsertCommand = conn.CreateCommand ();
				dataAdapter.InsertCommand.CommandText = 
					"INSERT INTO Person.Contact (ContactID, FirstName, LastName, Phone, 
					EmailAddress) VALUES (@ContactID, @FirstName, @LastName, @Phone, 
				AddParams (dataAdapter.InsertCommand, "ContactID, FirstName, LastName, 
					Phone, EmailAddress");
			catch (Exception exc) { MessageBox.Show (exc.Message, "InsertCommand"); }

			// Preparing Update SQL Command
				dataAdapter.UpdateCommand = conn.CreateCommand ();
				dataAdapter.UpdateCommand.CommandText = 
					"UPDATE Person.Contact SET FirstName = @FirstName, LastName = 
					@LastName, Phone = @Phone, EmailAddress = @EmailAddress WHERE 
					ContactID = @ContactID";
				AddParams (dataAdapter.UpdateCommand, "ContactID, FirstName, LastName, 
					Phone, EmailAddress");
			catch (Exception exc) { MessageBox.Show (exc.Message, "UpdateCommand"); }

		// add column name(s) supplied in params (prefixed with '@') into Parameters collection of 
		// SqlCommand class
		// SqlDbType.Char: type of parameter, 0: size of parameter, column: column name
		private void AddParams (SqlCommand cmd, params string [ ] columns)
			foreach (string column in columns)
				cmd.Parameters.Add ("@" + column, SqlDbType.Char, 0, column); 

Any ideas?

BC AdBot (Login to Remove)


#2 BobLewiston

  • Topic Starter

  • Members
  • 69 posts
  • Local time:11:40 AM

Posted 22 March 2009 - 01:52 PM

On the off-chance that this will help, this is the exact kind of exception that's occurring:


Are there any other properties of the Exception class besides Message and StackTrace that might help me figure this out?

#3 groovicus


  • Security Colleague
  • 9,963 posts
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:40 AM

Posted 22 March 2009 - 02:38 PM

In the interest of saving our time, I am locking this. It is incredibly inconsiderate of our time to post the exact same question on multiple forums.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users