1

I'm new to using SqlDataAdpter and I'm trying to execute a stored procedure. The stored procedure executes successfully but no rows are returned. I've used SQL Server Profiler to monitor the call and it runs successfully (I can copy and execute the query from profiler without modifying it and get results).

I have the following:

public ActionResult Index()
{
    SqlConnection conn = null;
    DataSet results = null;

    try
    {
        string connectionString = // ... my connection
        conn = new SqlConnection(connectionString );
        string query = @"usp_mySP";

        conn.Open();

        SqlDataAdapter sqlAdpt = new SqlDataAdapter(query, conn);
        sqlAdpt.SelectCommand.CommandType = CommandType.StoredProcedure;

        var dataDate = new SqlParameter { ParameterName = "@DataDate", Value = DateTime.Now };
        var idList = new SqlParameter { ParameterName = "@IDList", Value = "1231,2324,0833" };                 

        sqlAdpt.SelectCommand.Parameters.Add(dataDate);
        sqlAdpt.SelectCommand.Parameters.Add(idList);

        results = new DataSet();
        sqlAdpt.Fill(results);

        sqlAdpt.Dispose();
    }
    catch (SqlException e)
    {               
        throw new Exception("Exception:" + e.Message);
    }
    finally
    {
        if (conn != null)
            conn.Close();
    }

    return View(results);
}

When I inspect the DataSet through the debugger, it always returns 0 rows.

Please help with what I'm doing wrong?

Note: I've also tried (but do NOT prefer) executing as a SQL command:

EXEC usp_mySP @DataDate, @IDList

and it didn't work either as I got int to varchar conversion errors.

5
  • 4
    Perhaps verify that you have set nocount on at the beginning of your SP. Commented Jun 24, 2016 at 16:43
  • 1
    Tip for debugging: Use sql server profiler and verify what is sent to the sql server instance. Also see what the return is (should have rows or some other property). You can also capture the exact sql command and reexecute in management studio to see what the result was. Commented Jun 24, 2016 at 16:50
  • 1
    did you use SQL Profiler to capture what SQL is going to server? Did you debug your SP? It may be helpful to see your logic inside your SP. Sometimes the date format sent by .Net may not be matching up with the date format expected by DB Server!! Commented Jun 24, 2016 at 16:51
  • 1
    Run your program in debug mode and see what value you get for 'connectionString' - from the looks of your code, string connectionString = ... doesn't seem to have any value set. The line is not terminated with ';' Commented Jun 24, 2016 at 16:55
  • It was the set nocount on. It was not there. It now works Thanks! Commented Jun 24, 2016 at 16:57

2 Answers 2

1

I think you try to add SqlParameter using SqlCommand like this :

SqlCommand cmd = new SqlCommand();
cmd.parameter.addwithvalue(@DataDate,DateTime.Now);
Sign up to request clarification or add additional context in comments.

1 Comment

That's a valid way to do this - but so is the approach used in the question. This is definitely not the reason for the issues ....
1

So the reason was because of set nocount on. I added it to my sp and it works. Thank you everyone for clarifying.

Comments

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.