Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, could anyone spot where i am going wrong, i am trying to get my c# program to look at a table, and determine if the selected value already exists and if so update the value associated with it, if not add new value.
Having tried several methods i think i am code blind, any help would be greatly appreciated, thank you.

Code below:
C#
// *********************************************************************
// Calculate Product & Amount then pass results to 'Temptotalmade' table
// *********************************************************************

string product = ProductSelect.Text;

SqlCommand readTempTable = new SqlCommand();
string sqlReadTempTable = "Select Product,amount from TempTotalMade where FactoryID = '" + FactoryID.TrimEnd() + "' and AreaID = '" + Program.MAKE + "' and DaysNightsID = '" + Program.DAYS + "' and Product = '" + product.TrimEnd() + "'";
MessageBox.Show(sqlReadTempTable);
readTempTable.Connection = databaseConnection;
readTempTable.CommandText = sqlReadTempTable;

if  (readTempTable == null)
{
    string productdays = ProductSelect.Text;
    SqlCommand addNewTotMade = new SqlCommand();
    addNewTotMade.Connection = databaseConnection;
    string sqlAddNewTotMade = "Insert into TempTotalMade (FactoryID,AreaID,DaysNightsID,Product,amount) values ('" + FactoryID.TrimEnd() + "','" + Program.MAKE + "','" + Program.DAYS + "','" + productdays.TrimEnd() + "'," + make.totalBrickDays + ")";
    MessageBox.Show(sqlAddNewTotMade);
    addNewTotMade.CommandText = sqlAddNewTotMade;
    addNewTotMade.ExecuteNonQuery();
}

else
{
    SqlDataReader getproductdata;

    getproductdata = readTempTable.ExecuteReader();
    getproductdata.Read();

    string productexists = (getproductdata[("Product")].ToString());
    string productstring = productexists.TrimEnd();
    string amountexist = (getproductdata[("amount")].ToString());
    int isamount = int.Parse(amountexist);
    getproductdata.Close();

    double newtotal = isamount + make.totalBrickDays;
    SqlCommand updateproduct = new SqlCommand();
    updateproduct.Connection = databaseConnection;
    string sqlUpdateProduct = "Update TempTotalMade set amount = " + newtotal + " where FactoryID ='" + FactoryID.TrimEnd() + "' and AreaID = '" + Program.MAKE + "' and DaysNightsID = '" + Program.DAYS + "' and Product = '" + productstring.TrimEnd() + "'";
    updateproduct.CommandText = sqlUpdateProduct;
    updateproduct.ExecuteNonQuery();
    label33.Text = newtotal.ToString();

}

this.mADEPRODUCTDAYSTableAdapter.Fill(this.PIMPDataSet.MADEPRODUCTDAYS, @FactoryID);

textBox1.Text = "";
textBox2.Text = "";
Posted
Updated 22-May-12 5:53am
v2

1 solution

As you haven't said which version of SQL Server you are targetting, I'm assuming it's a later version - which is handy because they provide a very useful MERGE[^] command which allows you to perform what are sometimes known as upserts.

An upsert is a SQL command that can either be an insert or an update, depending on the selection criteria.

An extra point - never concatenate strings to create SQL statements. You leave yourself wide open to SQL Injection attacks.
 
Share this answer
 
v2
Comments
VJ Reddy 22-May-12 12:14pm    
Good answer. 5!
Pete O'Hanlon 22-May-12 12:18pm    
Cheers.
Sandeep Mewara 24-May-12 11:05am    
Comment from Op:
Thanks Pete, a good nights sleep was needed fresh eyes sorted my problem out. The injection attacks you refer to, am i write in thinking

string sqlUpdateProduct = "Update TempTotalMade set amount = " + newtotal + " where FactoryID ='" + FactoryID.TrimEnd() + "' and AreaID = '" + Program.MAKE + "' and DaysNightsID = '" + Program.DAYS + "' and Product = '" + productstring.TrimEnd() + "'";
could be manipulated by something like '1'' in the statement?

If yes whats the alternative?

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