SQL Server error -- Associated statement is not prepared

I have an sql stored procedure that runs fine when tested in the Query Analyzer. However, when I attempt to call this stored procedure from a c# web application I get the following error

ERROR [HY007] [Microsoft][ODBC SQL Server Driver]Associated statement is not prepared

Does anyone know a possible cause or solution to this error.

Thanks



Answer this question

SQL Server error -- Associated statement is not prepared

  • Yogesh Kumar

     

    * The chunks of code below contains just relevant portion of my codes. I basically pass in my stored procedures along with necessary parameter into the code and they all work fine, except for when I slightly modified a particar stored procedure and i started geting the afore mentioned error. The error occurs when I call the fill method: daVendorInfo.Fill(dsVendorInfo, "ResultSet");

    =================================================================================
    private const string  SP_TRAN_HRO = "usp_MyStoredProcudure";

    private void btnTransactions_Click(object sender, System.EventArgs e)

    {

    if (this.ddlHROorPEO.SelectedIndex != 0 && this.ddlTaxGroup.SelectedIndex != 0)

    {

    string sProc;

    //Check if user inputs are valid, and pass user inputs

    sCompanyCode ="";

    if(UIControlParameters(sPCBegin, sPCEnd, sCompanyCode) == false)

    {

    ClearDataGrid();

    lblMessage.Text = "Invalid entry, please check percontrol inputs";

    }

    else

    {

    if(sCompanyCode == "ASO")

    {

    GenerateInsertCommand(sCompanyCode);

    //Create string for ODBC stored procedure parameters

    //To be edited

    sProc = "{call " + SP_TRAN_HRO + "( , )}";

    }

    else

    {

    sProc = "{call " + SP_TRAN + "( , , )}";

    }

    FillDataGrid( sProc, sPCBegin,sPCEnd, sCompanyCode);

    }

    }

    else

    {

    if(this.ddlHROorPEO.SelectedIndex == 0)

    {

    lblHROorPEO.ForeColor = System.Drawing.Color.Red;

    lblHROorPEO.Text = "* Select an item";

    }

    else

    {

    lblTaxGroup.ForeColor = System.Drawing.Color.Red;

    lblTaxGroup.Text = "* Select an item";

    }

    }

    }

    private void FillDataGrid(string sSelectStatement, string PerControlBegin,

    string PerControlEnd, string GreatPlainsCompanyCode)

    {

    //Create connection to appropriate sql server

    OdbcConnection cnData = CreateDataConnection(GreatPlainsCompanyCode);

    OdbcDataAdapter daVendorInfo;

    DataSet dsVendorInfo;

    ClearDataGrid();

    OdbcCommand cmdVendorInfo = new OdbcCommand(sSelectStatement , cnData);

    if(hasParameter)

    {

    //hasParameter default is set to true

    //Add parameters to cmdVendorInfo if boolean hasParameter value is set to true

    //hasParameter value is set to false if store procedure been called requires no parameters

    AddParameters(cmdVendorInfo,PerControlBegin, PerControlEnd,GreatPlainsCompanyCode);

    }

    daVendorInfo = new OdbcDataAdapter(cmdVendorInfo);

    dsVendorInfo = new DataSet("ResultSet");


    try

    {

    daVendorInfo.Fill(dsVendorInfo, "ResultSet");

    dgDataFromGP.DataSource = dsVendorInfo;

    dgDataFromGP.DataMember = "ResultSet";

    dgDataFromGP.CurrentPageIndex = 0;

    dgDataFromGP.DataBind();

    }

    catch(Exception ex)

    {

    this.lblMessage.Text = ex.Message;

    }

    finally

    {

    if(dsVendorInfo != null)

    dsVendorInfo.Dispose();

    if(daVendorInfo != null)

    daVendorInfo.Dispose();

    }

    }

    private OdbcCommand AddParameters(OdbcCommand cmd, string PerControlBegin,

    string PerControlEnd, string code)

    {

    OdbcParameter param1 = new OdbcParameter( @PerControlBegin, OdbcType.Char, 9);

    param1.Value = PerControlBegin;

    cmd.Parameters.Add(param1);

    OdbcParameter param2 = new OdbcParameter(@PerControlEnd,OdbcType.Char,9);

    param2.Value = PerControlEnd;

    cmd.Parameters.Add(param2);

    if(code.Equals("ASO"))

    {

    ;

    }

    else

    {

    OdbcParameter param3 = new OdbcParameter(@code, OdbcType.VarChar,20);

    param3.Value = code;

    cmd.Parameters.Add(param3);

    }

    return cmd;

    }


    Thanks


  • sydes141

    Hopefully you've resolved this by now, but here's what I found:

    I got this same exact error message while trying to do inserts that were not allowed due to a foreign key constraint.

    For some reason, the proper SQL error does not come back, only this bizarre Associated Statement is not prepared...

    Anyways, try to grab the SQL that you're executing before it fires, and then run it in Query Analyzer, and you'll likely find out what the TRUE error is...

    ...Jeff


  • MateuszKierepka

    Jeff,

    Yes, I did relsove the error a little while back with the same approach that you have mentioned.

    Thanks though.


  • megoo80

    Could you post the code that executes your SP

  • SQL Server error -- Associated statement is not prepared