Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok so I did some research and at first chase up the possibilty of using <code>executeScalar();</code> as I can return a value where I thought <code>executeNonQuery();</code> didn't return any at all. Seems I was mistaken and it actually returns the number of rows affected. Once I found this out It was a simple case of modifying my code to do what I needed. Here's working code!</p> <pre><code> private void ExecuteInsert(string name, string type) { int rows = 0; //Creates a new connection using the HWM string using (SqlConnection HWM = new SqlConnection(GetConnectionStringHWM())) { //Creates a sql string with parameters string sql = " IF NOT EXISTS " + " ( SELECT 1 " + " FROM tblSoftwareTitles " + " WHERE Softwarename = @SoftwareName " + " AND SoftwareSystemType = @Softwaretype " + " ) " + " BEGIN " + " INSERT tblSoftwareTitles (SoftwareName, SoftwareSystemType) " + " VALUES (@SoftwareName, @SoftwareType); " + " END ; "; //Opns the connection HWM.Open(); try { //Creates a Sql command using (SqlCommand addSoftware = new SqlCommand{ CommandType = CommandType.Text, Connection = HWM, CommandTimeout = 300, CommandText = sql}) { //adds parameters to the Sql command addSoftware.Parameters.Add("@SoftwareName", SqlDbType.NVarChar, 200).Value = name; addSoftware.Parameters.Add("@SoftwareType", SqlDbType.Int).Value = type; //Executes the Sql try { rows = addSoftware.ExecuteNonQuery(); if (rows &gt;= 1) { Alert.Show("Software Saved"); } else { Alert.Show("Software already exists"); } } catch (Exception ex) { Alert.Show(ex.Message); } } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } } } </code></pre> <p>And there you have it! If the combination of software type and software name are already in the database a message box will appear telling the user of that fact. If the Combination of name and type aren't present then the information will be saved and a message box will be displayed telling the user of the successful insert.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload