Update Query: Concat string to existing string

  • Thread starter Thread starter FriQenstein
  • Start date Start date
F

FriQenstein

Guest
Greetings all,

I am working on a 'simple' UPDATE query where a user can change the quantity of an item, its status, and the notes field.

So far, I have the QTY and STATUS taken care of. However, I am trying to figure out how to get the NOTES field to update by appending the new text to the existing text in that field.

Here is an example of the code I am using:

private void btn_Save_Click(object sender, EventArgs e)
{
string date = DateTime.Now.ToString("MM/dd/yyyy");
Console.WriteLine("Cell #1: " + var1);
Console.WriteLine("Cell #2: " + var2);
Console.WriteLine("Cell #3: " + var3);
Console.WriteLine("Cell #4: " + var4);
Console.WriteLine("Cell #4: " + notes);

connection.Open();
OleDbCommand command = new OleDbCommand();
SqlParameter param1 = new SqlParameter("@itemID", var1);
SqlParameter param2 = new SqlParameter("@date", date);
//SqlParameter param3 = new SqlParameter("@notes", notes);
command.Parameters.AddWithValue("@itemID", SqlDbType.Int);
command.Parameters["@itemID"].Value = Convert.ToInt32(var1);
//command.Parameters.AddWithValue("@date", SqlDbType.VarChar);
//command.Parameters["@date"].Value = date;
//command.Parameters.AddWithValue("@notes", SqlDbType.VarChar);
//command.Parameters["@notes"].Value = (notes + date); /// FIX THIS ASAP!!!!!!!!!!!!
command.Connection = connection;

string queryUpdate = @"UPDATE tblClientInventory
SET Qty = 0, Status = 5
WHERE ClientItemID = @itemID";

command.CommandText = queryUpdate;
OleDbDataReader reader = command.ExecuteReader();

MessageBox.Show("Record Updated\nQty of " + var3 + " to 0\nStatus of " + var4 + " to SCRAP");
connection.Close();
}

I have attempted to use CONCAT but the IDE complains about it. As you can see from the commented lines, I have attempted to create and use the variables to avoid injection, but the Query string is where I think it is falling apart.

I tried adding the following to the query string (with the corresponding lines uncommented):

string queryUpdate = @"UPDATE tblClientInventory
SET Qty = 0, Status = 5, Notes = @notes
WHERE ClientItemID = @itemID";

The program still runs without any errors, but the update does not happen. As soon as I add the Notes portion to the query string, it simply just goes no where. I am assuming it has something to do with the way I am combining the strings/variables prior to sending the changes to the DB.

Basically, what needs to happen is, the data will be read from the datagridview and store the appropriate values. The Notes field is read and stored and then needs to have the appended text added to the end of the existing notes field.

Any help is greatly appreciated. I have looked through the MSDN documentation and various examples on the internet, but I am unable to nail down the solution.

Regards.

Continue reading...
 
Back
Top