0
SqlConnection connection = Connect();

SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = sprocName;
command.Connection = connection;
//loop through the dictionary
foreach (string key in paramList.Keys)
{
        command.Parameters.AddWithValue(key, paramList[key]);
}
SqlDataReader dataReader = command.ExecuteReader();

I am trying read data from database, when I am passing same parameters and execute the SP then I am getting proper result.But here SqlDataReader dataReader = command.ExecuteReader(); when I am checking (dataReader.HasRows) it returns false!

14
  • That's not possible ... check if you are running against same DB/Table Commented Feb 12, 2019 at 6:48
  • 1
    have you checked that your query actually returns value from the DB? Commented Feb 12, 2019 at 6:48
  • You may need to call NextResult. Commented Feb 12, 2019 at 6:48
  • @styx yes I have checked query actually returns value from db. Commented Feb 12, 2019 at 7:02
  • Can you please add the SP too if possible? Commented Feb 12, 2019 at 7:05

2 Answers 2

0

Does you key contains '@', for example if key parameter name EmployeeId then needs to add parameter as below

commnad.Parameters.AddWithValue('@EmployeeId', 1);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes It contains @.
0

Exec up_GetAtt @pageNumber=1, @pagesize=10, @PID=1000, @DeID=NULL, @DeName =NULL, @SortByColumn='ReleaseDate', @SortOrder='DESC' this are the parameters I am passing

You need to convert the C# null values into DBNull

SqlConnection connection = Connect();

SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = sprocName;
command.Connection = connection;
//loop through the dictionary
foreach (string key in paramList.Keys)
{
        command.Parameters.AddWithValue(key, paramList[key] == NULL? DBNull.Value : paramList[key]);
}
SqlDataReader dataReader = command.ExecuteReader();

Given the lack of information to help you better, check also for common mistakes

  • Typos in proc and parameter names
  • Sending a query with CommandType.StoredProcedure: sprocName value must be just "up_GetAtt", is a common mistake to include the EXEC word or parameters.
  • Check Value Types, your paramList should be a dictionary, and the value type of the items that you put in that list should match with the sql equivalent type, not all types have implicit conversion.
  • Check nullability of paramList items, use nullable int and datetime
  • Check that the parameter names start with @
  • Omitting a parameter does not mean that it is null, unless your store procedure definition default it as null
  • Omitting a parameter with no default value in procedure definition
  • Unnecessary quotation of values, you are sending parameter values, it is not a concat

3 Comments

Yes I have already did that I am passing dbnull values only for c# null values.
When I am trying to view Result View of SqlDataReader it shows error "Enumaration yielded no result"
Troubleshot the list I posted in the last edition, nothing else come to my mind if you do not post more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.