I have the following code that uses the SqlClient.ExecuteScalar method to return an identifier 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(); }
It originally worked, but now contribId is null. I tested SQL in the management studio after retrieving from Profiler and was returning the identifier as expected.
Then I added an additional command to get the identifier from another table (Product).
productId is not null, and 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 that this is something obvious, and I will hit myself for not noticing this, but so far I'm at a standstill.
c # sqlcommand
Pauly
source share