简体   繁体   中英

Could not find stored procedure - SQLDataSource

Other similar questions haven't really been relevant to my problem so I'm making a new one...

<asp:SqlDataSource ID="SqlDataSource2"  runat="server" ConnectionString="<%$ ConnectionStrings:CatalogosConnectionString %>" 
                          InsertCommand="dbo.GRM_Novo_Catalogo" InsertCommandType="StoredProcedure"> 
    <InsertParameters>
        <asp:Parameter Name="Nome" Type="String" />
        <asp:Parameter Name="NomeAbreviado" Type="String" />
        <asp:Parameter Name="Estado" Type="Int64" />
        <asp:Parameter Name="OrganismoID" Type="Int64" />
        <asp:Parameter Name="TipoCatalogoID" Type="Int64" />
    </InsertParameters>
</asp:SqlDataSource>

Code-Behind:

protected void Button2_Click(object sender, EventArgs e)
{
    SqlDataSource2.InsertParameters["Nome"].DefaultValue = TextBox13.Text;
    SqlDataSource2.InsertParameters["NomeAbreviado"].DefaultValue = TextBox14.Text;
    SqlDataSource2.InsertParameters["Estado"].DefaultValue = TextBox15.Text;
    SqlDataSource2.InsertParameters["OrganismoID"].DefaultValue = TextBox16.Text;
    SqlDataSource2.InsertParameters["TipoCatalogoID"].DefaultValue = TextBox17.Text;
    SqlDataSource2.Insert();
}

So What I want is that when the user clicks on Button2 it runs that stored procedure from the Database, however when testing it brings up an error message saying it could not find the stored procedure.

I am using the same connection string on another SQLDataSource and it works (this also uses a stored procedure which is in the same place as the one in the code above)

Stack trace:

Message:
DotNetNuke.Services.Exceptions.PageLoadException: Could not find stored procedure 'dbo.GRM_Novo_Catalogo'. ---> System.Data.SqlClient.SqlException: Could not find stored procedure 'dbo.GRM_Novo_Catalogo'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation)
   at System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values)
   at System.Web.UI.WebControls.SqlDataSource.Insert()
   at EmptyWebsite3.Modules.DNNModule1.Edit.Button2_Click(Object sender, EventArgs e) in c:\Users\me\Documents\My Web Sites\EmptySite3\DesktopModules\DNNModule1\Edit.ascx.cs:line 79
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---

I realise this is grasping at straws - have you tried different syntax for calling your SP?

Here is a typical call that I use in a DataAccess class:

public int setUserTimescale(DataObjects.UserDetails myUserDetails)
{
    int ReturnValue = -1;
    SqlConnection myConnection = new SqlConnection(connectionString);
    SqlCommand myCommand = myConnection.CreateCommand();
    myCommand.CommandText = "User_Timescale_Up";
    myCommand.CommandTimeout = 120;
    myCommand.Connection = myConnection;
    myCommand.CommandType = CommandType.StoredProcedure;

    myCommand.Parameters.Add("UserID", SqlDbType.Int).Value = myUserDetails.UserID;
    myCommand.Parameters.Add("TimescaleID", SqlDbType.Int).Value = myUserDetails.TimescaleID;

    SqlParameter parameterReturnValue = new SqlParameter("@ReturnValue", SqlDbType.Int);
    parameterReturnValue.Direction = ParameterDirection.Output;
    myCommand.Parameters.Add(parameterReturnValue);

    try
    {
        myConnection.Open();
        myCommand.ExecuteNonQuery();

        // Get the return value
        ReturnValue = (int)myCommand.Parameters[2].Value;
    }
    catch (Exception e)
    {
        setDBError(myCommand, e.Message.ToString(), "User_Timescale_Up");
        ReturnValue = -2;
    }
    finally
    {
        myConnection.Close();
    }
    return ReturnValue;
}

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