既存のファイルIDがアイテムに関連付けられているかどうかを確認するストアドプロシージャがあります。selectステートメントが値を返す場合、それはtrueであり、boolに「true」を割り当てる必要があります。ただし、selectステートメントが存在しないためにnullを返す場合でも、コードビハインドは.Executeを「true」に戻します。
これは私のストアドプロシージャです:
ALTER PROCEDURE [dbo].[Events_TaskIDExists]
@EventID int
AS
BEGIN
select TaskID from Events where EventID = @EventID
END
そしてここに私のコードがあります:
public void hasTaskAssociatedToNote()
{
String[] Notes = hidSelectedEventIDs.Value.Split(',');
bool exists = false;
foreach (var note in Notes)
{
var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
var command = new SqlCommand("Events_TaskIDExists", connection);
command.Parameters.Add(new SqlParameter("@EventID", SqlDbType.Int));
command.Parameters["@EventID"].Value = Convert.ToInt32(note.Trim());
command.CommandType = CommandType.StoredProcedure;
try
{
connection.Open();
exists = command.ExecuteScalar() != null;//causes true when it returns null......
var temp = command.ExecuteScalar();//this was just to check something else
if (exists)
{
exhibitWarning.Visible = true;
Warning1.Text = "There is an existing Task associated 0.";
}
}
catch (SqlException sql)
{
lblStatus.Text = "Couldn't connect to the Database - Error";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
catch (Exception ex)
{
lblStatus.Text = "An error occured";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
}