I have the following code that uses the SqlClient.ExecuteScalar method to return an ID from a table.
using (var conn = new SqlConnection(connectionString))
using (var cmdContrib = new SqlCommand("SELECT ContributorId FROM Contributor WHERE Code='" + folderSystem.ContributorCode + "'", conn))
{
conn.Open();
var contribId = cmdContrib.ExecuteScalar();
}
Originally it was working but now contribId is null. I tested the SQL in management studio after extracting from Profiler and it returned the ID as expected.
Next I added an additional command to retrieve an ID from a different table (Product).
productId is not null while contribId continues to be null.
using (var conn = new SqlConnection(connectionString))
using (var cmdContrib = new SqlCommand("SELECT ContributorId FROM Contributor WHERE Code='" + folderSystem.ContributorCode + "'", conn))
using (var cmdTest = new SqlCommand("SELECT productId FROM Product WHERE [filename] = 'bda00001.jpg'", conn))
{
conn.Open();
var contribId = cmdContrib.ExecuteScalar();
var productId = cmdTest.ExecuteScalar();
}
I am sure it is something obvious and I'll kick myself for not noticing it, but for now I'm stumped.