IF & Else condition in C# with SQL

  • Thread starter Thread starter Caulson
  • Start date Start date
C

Caulson

Guest
Dear All,

i have wrote the condition in C# with SQL.

The condition is select data from table, if the ID is not found or empty in table then insert the ID into table else update the ID Qty and Insert data into other table.

There is no error for the code, but not data insert and update into DB table. Kindly advise, thank you.


public void insertData(string str1, string str2, string str3, string str4, string str5, string str6, string str7, string str8, string str9, string str10, string str11, string str12)

{

string Qty = Inv_Qty.Text;
string Inv_ID = WMItem.SelectedValue;
string Loc = Inv_Loc.SelectedValue;




SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
con.Open();


string selectSQL = "SELECT * FROM [CIMProRPT01].[dbo].[OTH_INV_QTY_LOC] WHERE INV_ID = '" + Inv_ID + "' ";


if (String.IsNullOrEmpty(Inv_ID))
{
con.Open();



string INSERTSQL = "INSERT INTO [CIMProRPT01].[dbo].[OTH_INV_QTY_LOC](INV_ID,INV_LOCATION,INV_QTY) VALUES ('" + Inv_ID + "', '" + Inv_Loc + "','" + Qty + "') ";

SqlCommand cmd2 = new SqlCommand(INSERTSQL, con);

cmd2.ExecuteNonQuery();

con.Close();
}


else

{

string UpdateWMMRSQL = "UPDATE [CIMProRPT01].[dbo].[OTH_INV_QTY_LOC] SET INV_QTY = INV_QTY + '" + Qty + "' WHERE INV_ID = '" + Inv_ID + "' AND INV_LOCATION = '" + Loc + "' ";


string sql = "INSERT INTO OTH_INV_TRANSACTION (INV_TRANS_ID,INV_ID,INV_TRANS_LOCATION,INV_TRANS_QTY,INV_TRANS_REQUESTOR,INV_TRANS_REFNO,INV_TRANS_REMARK,INV_REASON_ID,INV_REASON_REMARK,INV_CREATE_DATE,INV_CREATE_USER,INV_VENDORS) VALUES ('" + str1 + "','" + str2 + "','" + str3 + "','" + str4 + "','" + str5 + "','" + str6 + "','" + str7 + "','" + str8 + "','" + str9 + "','" + str10 + "','" + str11 + "','" + str12 + "')";



SqlCommand cmd1 = new SqlCommand(selectSQL, con);

SqlCommand cmd3 = new SqlCommand(UpdateWMMRSQL, con);
SqlCommand cmd4 = new SqlCommand(sql, con);


cmd1.ExecuteNonQuery();

cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();


con.Close();


}

}

Continue reading...
 
Back
Top