MARKAND Bhatt
MARKAND Bhatt

Reputation: 2640

Update query: values get updated in wrong columns

I'm making a successfull update into the table, but for some reason the fields are in a messed up position. Working on Visual Studio 2010 C# with an MS Access Database 2007.

here is my code

        OleDbCommand cm = new OleDbCommand();
        cm.Connection = AccessConnection();

        cm.Parameters.AddWithValue("vContactId", c.ContactId);
        cm.Parameters.AddWithValue("vFirstName", c.FirstName);
        cm.Parameters.AddWithValue("vLastName", c.LastName);
        cm.Parameters.AddWithValue("vAddress", c.Address);
        cm.Parameters.AddWithValue("vCity", c.City);
        cm.Parameters.AddWithValue("vState", c.State);
        cm.Parameters.AddWithValue("vCountry", c.Country);
        cm.Parameters.AddWithValue("vPinCode", c.PinCode);
        cm.Parameters.AddWithValue("vMobilePhone", c.MobilePhone);
        cm.Parameters.AddWithValue("vFax", c.Fax);
        cm.Parameters.AddWithValue("vOfficePhone", c.OfficePhone);
        cm.Parameters.AddWithValue("vHomePhone", c.HomePhone);
        cm.Parameters.AddWithValue("vCompany", c.Company);
        cm.Parameters.AddWithValue("vDesignation", c.Designation);
        cm.Parameters.AddWithValue("vEmail", c.Email);
        cm.Parameters.AddWithValue("vNotes", c.Notes);
        cm.Parameters.AddWithValue("vCategoryId", c.CategoryId);
        cm.Parameters.AddWithValue("vDtUpdated", c.dtUpdated);
        cm.Parameters.AddWithValue("vTitle", c.title);

        string q = "UPDATE tblContacts SET FirstName = vFirstName, LastName = vLastName, Address = vAddress, City = vCity, " +
                    "State = vState, Country = vCountry, PinCode = vPinCode, MobilePhone = vMobilePhone, Fax = vFax, " +
                    "OfficePhone = vOfficePhone, HomePhone = vHomePhone, Company = vCompany, Designation = vDesignation, " +
                    "Email = vEmail, Notes = vNotes, DtUpdated = vDtUpdated, CategoryId = vCategoryId, Title = vTitle" +
                    " WHERE ContactId=vContactId";
        cm.CommandText = q;

        if (!Exist(c))
        {
            cm.Connection.Open();
            cm.ExecuteNonQuery();
            cm.Connection.Close();
        }

What i am doing wrong here?

Upvotes: 0

Views: 57

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123809

OleDb ignores the names of parameters. All it recognizes is the order in which the parameters appear in the CommandText. (ref: here)

You need to change the order of the .AddWithValue statements to exactly match the order in which the parameters appear in the CommandText.

Upvotes: 1

Related Questions