Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » dataAdapter.Update problem
Closed Thread
Old 03-21-2009, 05:08 PM   #1 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default dataAdapter.Update problem

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:
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"); }  // <--PROBLEM
    catch (Exception exc) { MessageBox.Show (exc.Message); }

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

Code:
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?
BobLewiston is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem installing software in windows xp pro imagesmith Windows Operating Systems and Software 3 02-20-2008 02:46 PM
Serious computer problem. HeeRoMaKi Hardware Troubleshooting 71 07-28-2007 11:42 PM
Wireless Problem! dropper Computer Networking & Internet Access 3 07-08-2007 04:58 PM
Dhcp leasing problem Kloppstock Computer Networking & Internet Access 12 05-13-2007 07:13 PM
cant find the problem... Jhill1 Hardware Troubleshooting 13 05-07-2007 04:51 PM