简体   繁体   中英

Why Is this returning no results when executed?

SqlConnection conn = getConnection();
SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_PSLA_SEARCH"; //The stored procedure gets added
cmd.CommandTimeout = 0;
cmd.Connection = conn;

// Start adding the parameters of the stored procedure
cmd.Parameters.AddWithValue("@usrnm", thisUser.Username);

int constit = 0;

if (thisUser.Constituencies.Count > 0)
{
    foreach (KeyValuePair<int, string> kp in thisUser.Constituencies)
    {
        if (kp.Value == ddlConstituency.SelectedValue.ToString())
        {
            constit = kp.Key;
            break;
        }
    }
}

cmd.Parameters.AddWithValue("@cnstncy", constit);

string pdval = null;
int valtype = 0;

if (rbsearchradios.SelectedIndex == 0)
{
    try
    {
        pdval = searchVal;
        cmd.Parameters.AddWithValue("@Search", DBNull.Value);
        cmd.Parameters.AddWithValue("@pd", int.Parse(pdval));
        cmd.Parameters.AddWithValue("@type", valtype);
    }
    catch
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "stop", "alert('Invalid PD Number Supplied! Please Provide A Valid Submission.');", true);
        return;
    }
}
else
{
    valtype = 1;
    cmd.Parameters.AddWithValue("@Search", searchVal);
    cmd.Parameters.AddWithValue("@pd", DBNull.Value);
    cmd.Parameters.AddWithValue("@type", valtype);
}

cmd.Parameters.AddWithValue("@app", 1);

conn.Open();                        

// Creates Dataadapter for execution
SqlDataAdapter dp2 = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

dp2.Fill(ds, "name");

I am trying to the arguments of the stored procedure and the having this stored procedure execute and get this results into a dataset but I get nothing.. Literally. There are no exceptions, just no result from the stored procedure.

This is the stored procedure:

DECLARE @return_value int

EXEC    @return_value = [dbo].[SP_PSLA_SEARCH] 
            @usrnm = N'tstone',
            @cnstncy = 55,
            @Search = N'primary',
            @pd = NULL,
            @type = 1,
            @app = 1

SELECT  'Return Value' = @return_value
GO

To troubleshoot:

  1. Make sure what values has each parameter and execute the same query directly against database in Sql Server Management Studio.
  2. Check if you properly use the result from the dataset (it's not clear from the code)

In general, you can also try to simplify and make your code more clear:

  • the block with return and if (rbsearchradios.SelectedIndex == 0) can be moved at the beginning, it makes no sense to create SqlCommand and then break
  • if SP returns only single value, you can use the ExecuteScalar() method, which is faster and straightforward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM