Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
take note I'm still a beginner in databases but willing to learn. This time, I want to know how to update a data from database. I've tried this code but it is giving me an error "Incorrect syntax near the keyword 'JOIN'". Kindly help me on solving this one.

Info about the database

Table stringInstrumentItem(The column brandId is the foreign key and references the primary key of table brand, which is also named brandId):

itemId     brandId     model

1             1               xyz

2             1               abc

3             2               hjk




Table brand(which has the primary key called brandId that is referencing by the table strinInstrumentItem):

brandId     name     image

1             Ibanez       xyz.jpg

2             Fender       abc.jpg

3             Gibson       hjk.jpg




Main goal is to update the column called name in table brand through my gridview. Like for example in brandId#1, name is equals to Ibanez, and I want to change it to Jackson. It should be able to work without the exception error. How to query this one?

What I have tried:

C#
string queryGuitarItems = "UPDATE stringInstrumentItem JOIN brand ON stringInstrumentItem.brandId = brand.brandId SET stringInstrumentItem.brandId = @brandId IN (SELECT brand.brandId FROM brand WHERE name = @oldBrandName";
    using (SqlConnection connectionGuitarItems = new SqlConnection(ConfigurationManager.ConnectionStrings["musicStoreConnection"].ToString()))
    {
        using (SqlCommand commandGuitarItems = new SqlCommand(queryGuitarItems, connectionGuitarItems))
        {
            List<SqlParameter> parameterGuitarItems = new List<SqlParameter>();
            parameterGuitarItems.Add(new SqlParameter("@brandId", newName.Text));
            parameterGuitarItems.Add(new SqlParameter("@oldBrandName", oldName));

            connectionGuitarItems.Open();
            GetParameterGuitarItems(commandGuitarItems,parameterGuitarItems.ToArray());
            commandGuitarItems.ExecuteNonQuery();
            connectionGuitarItems.Close();
            commandGuitarItems.Parameters.Clear();
        }
    }
Posted
Updated 10-Jul-17 3:29am

looking at the query in your post, it seems you are trying to update brandid in brand StringInstrumentItem table for the oldBrandName. the query should be like this..

UPDATE SII SET SII.brandId = b.brandId
FROM stringInstrumentItem SII
inner join brand b ON SII.brandId = b.brandId
AND b.name = @oldBrandName
 
Share this answer
 
v2
Comments
BebeSaiyan 10-Jul-17 10:49am    
exactly what i want to achieve! this really works. Thanks bro!
I think your query needs to look something like this:
SQL
UPDATE SII SET SII.brandId = @brandId 
FROM stringInstrumentItem SII JOIN brand ON SII.brandId = brand.brandId 
WHERE brand.name = @oldBrandName
But that would make no sense, as you already JOIN on the same brandId, so maybe this is what you need:
SQL
UPDATE SII SET SII.brandId = @brandId 
FROM stringInstrumentItem SII, brand
WHERE brand.name = @oldBrandName
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900