BleepingComputer.com: dataAdapter.Update problem

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

dataAdapter.Update problem

#1 User is offline   BobLewiston 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 69
  • Joined: 07-November 08

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
			try
			{
				dataAdapter.InsertCommand = conn.CreateCommand ();
				dataAdapter.InsertCommand.CommandText = 
					"INSERT INTO Person.Contact (ContactID, FirstName, LastName, Phone, 
					EmailAddress) VALUES (@ContactID, @FirstName, @LastName, @Phone, 
					@EmailAddress)";
				AddParams (dataAdapter.InsertCommand, "ContactID, FirstName, LastName, 
					Phone, EmailAddress");
			}
			catch (Exception exc) { MessageBox.Show (exc.Message, "InsertCommand"); }

			// Preparing Update SQL Command
			try
			{
				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?

#2 User is offline   BobLewiston 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 69
  • Joined: 07-November 08

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:

System.Runtime.InteropServices.ExternalException

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

#3 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

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.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users