I have a section of code in my program that is supposed to pull the most recent time for a batch at a specific event. All I'm getting however is a blank result. I know there's data in the table for the batch in question and SQL Profiler shows it is being executed.
public void UpdateBeginStir()
{
string beginStir = null;
string connectionString = "(Omitted)";
string commandLine = "SELECT MAX(Event_Time) AS Time " +
"FROM dbo.Custom_EventLog " +
"WHERE Container_ID = @LOTNUMBR " +
"AND Event_ID = 1 " +
"GROUP BY BadgeNo, Container_ID, Event_ID";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(commandLine, connection))
{
command.Parameters.Add("@LOTNUMBR", SqlDbType.NChar, 50).Value = TextBoxLot.Text;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
beginStir = reader["Time"] as string;
break;
}
}
connection.Close();
}
MessageBox.Show(beginStir, "test", MessageBoxButton.OK);
LabelBeginStir.Content = beginStir;
}
beginStir = command.ExecuteScalar() as string;and simplify tocommand.Parameters.AdWithValue("@LOTNUMBR", TextBoxLot.Text);while @NemanjaPerovic's answer will likely fix your issue and explains why it doesn't work as it is now.