0

I'm trying to update an image field where the id is equal to the row selected.

I've tried using the below code

String query = "UPDATE ArticlesTBL SET ArticleImg = VALUE (@ArticleImg) WHERE ArticleID = VALUE (@id)";              
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@ArticleImg", "UploadedUserFiles/" + FileUpdate.FileName);
myCommand.Parameters.Add(new SqlParameter("@id", ThisID));

myinfo.Text = query;

But I don't know where to place commas, brackets etc in the query string. I have a text box outputting my command and it shows that i'm using UPDATE ArticlesTBL SET ArticleImg = @ArticleImg WHERE ArticleID = @id But I'm not actually accessing the parameters. So, how do I access these parameters? Thanks

4
  • You get your @id parameter in your TextBox? You don't need to use VALUE by the way. Commented May 6, 2014 at 12:28
  • Yes TextBox RecordID = editarticle.EditItem.FindControl("ArticleIDTextBox") as TextBox; Int32 ThisID = Convert.ToInt32(RecordID.Text); Commented May 6, 2014 at 12:30
  • What are the types of ArticleID and ArticleImg columns? Commented May 6, 2014 at 12:31
  • ID is an int and img is a string Commented May 6, 2014 at 12:32

1 Answer 1

2

The correct syntax for your update statement is

String query = @"UPDATE ArticlesTBL 
                 SET ArticleImg = @ArticleImg 
                 WHERE ArticleID = @id";

And to avoid confusion with the constructor of an SqlParameter that accepts a SqlDbType and an Object I suggest to use AddWithValue also for the ID

SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@ArticleImg", "UploadedUserFiles/" + FileUpdate.FileName);
myCommand.Parameters.AddWithValue("@id", ThisID);
myCommand.ExecuteNonQuery();

Of course, the command needs to be executed after you have prepared the command text, added its parameters and associated the connection

Sign up to request clarification or add additional context in comments.

8 Comments

I'm still getting the same message in my info textbox and the table isn't updated. I don't think it is recognising the parameters
Did you execute the command? I mean, do you call myCommand.ExecuteNonQuery()?
Yeah I did. By trying this instead: String query = "UPDATE ArticlesTBL SET ArticleImg = '" + newimg + "' WHERE ArticleID ='" + ThisID +"'"; I now get the message UPDATE ArticlesTBL SET ArticleImg = 'UploadedUserFiles/Lighthouse.jpg' WHERE ArticleID ='53' returned, which is progress but it doesn't update my table
What is your connectionstring and what is your user interface (WinForms, ASP.NET)?
I also suggest to use the debugger. Put a breakpoint at the entry line of this code and check if your variable values are correct before the execute
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.