I have a comma delimited textbox and a loop which parses it out and loops to create a series of SQL INSERT statements. However, only the first SQL INSERT statement in [Message_Bridge] is working. The foreach statement is working fine the first time through, but subsequent iteration do not INSERT. No errors are thrown.
dataCommand.CommandText = "INSERT INTO [Messages]([Subject], [Body], [Date_Time], [Originator], [Canceled], [Message_Index]) VALUES(@Subject, @Body, GETDATE(), @Originator, 0, 0); SELECT SCOPE_IDENTITY();";
dataCommand.Parameters.Clear();
dataCommand.Parameters.AddWithValue("@Subject", ComposeSubjectTexbox.Text);
dataCommand.Parameters.AddWithValue("@Body", ComposeBodyTexbox.Text);
dataCommand.Parameters.AddWithValue("@Originator", 506);
var Message_ID = dataCommand.ExecuteScalar();
string[] Recipients = ComposeToTextBox.Text.Split(',');
dataCommand.CommandText = "INSERT INTO [Message_Bridge]([Message_ID], [User_ID]) SELECT TOP 1 @Message_ID, [User_ID] FROM [Users] WHERE [User_Name] = @User_Receiver; ";
dataCommand.Parameters.Clear();
dataCommand.Parameters.AddWithValue("@Message_ID", Message_ID);
dataCommand.Parameters.Add("@User_Receiver", SqlDbType.NVarChar);
foreach (string User_Receiver in Recipients)
{
dataCommand.Parameters["@User_Receiver"].Value = User_Receiver;
dataCommand.ExecuteNonQuery();
}
myConnection.Close();