ExecuteScalar return value
-
Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?
-
Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?
-
Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?
I'm surprised 2 & 3 don't work. The thing to do to figure out why they don't: 1. What is the error message you are getting? 2. In version 3, have you put a breakpoint on to see what value you get back - it is a good idea to check this in code even if it works on the SQL side.
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?
-
Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?
If everything is working when you run the query manually (sounds like you have verified this already), use method #3 and check if name is null, and if not, check name.GetType() to see what the framework thinks the type being returned from the command is.
-
Hello, I have used this method but to return a single int value. Now I am returuing a string. I have tried to define the local variable as a string as well as object. It just does not seem to be returning a string value. I have tried the following different ways: 1. string name = sqlcommRights.ExecuteScalar() as string; 2. string name = (string)sqlcommRights.ExecuteScalar(); 3. object name = sqlcommRights.ExecuteScalar(); I am printing the SQL statement and running in sql server, that returns a single row value like 'John Smith'. To check value - Response.Write(name); Am I missing something here?
Those look reasonable, but check for System.DBNull.Value Here's my current code for ExecuteScalar:
public virtual T ExecuteScalar<T> ( T IfNull ) { lock ( this.command ) { try { this.OpenCount++ ; object result = this.command.ExecuteScalar() ; if ( ( result == null ) || ( result == System.DBNull.Value ) ) { result = IfNull ; } else if ( typeof(T) != result.GetType() ) { result = System.Convert.ChangeType ( result , typeof(T) ) ; } this.RaiseEvent ( this.OnSuccess , 1 , null ) ; return ( (T) result ) ; } catch ( System.Exception err ) { throw ( this.WrapException ( "ExecuteScalar" , this.command , err ) ) ; } finally { this.OpenCount-- ; } } }