【问题标题】:Receiving error in Datasource when attempting insert command尝试插入命令时在数据源中接收错误
【发布时间】:2019-03-24 16:39:45
【问题描述】:

我在尝试数据源的 InsertCommand 时收到以下错误消息:

过程或函数“student_insert”需要参数“@password”,但未提供。

我在 ASCX 页面中设置了大部分参数,除了“密码”,因为我需要在将其保存到数据库之前对其进行一些操作:

<InsertParameters>
        <asp:Parameter Name="email" Type="String" />
        <asp:Parameter Name="first_name" Type="String" />
        <asp:Parameter Name="last_name" Type="String" />
        <asp:Parameter Name="university" Type="String" />
        <asp:Parameter Name="program" Type="String" />
        <asp:Parameter Name="student_number" Type="String" />
        <asp:ControlParameter Name="graduation_date" Type="DateTime" ControlID="ctl00$ContentPlaceHolder$ctl01$frmStudents$txtGradDate" PropertyName="Text" />
        <asp:ControlParameter Name="student_card_image_name" Type="String" ControlID="ctl00$ContentPlaceHolder$ctl01$frmStudents$FUStudent" PropertyName="FileName" />
        <asp:ControlParameter Name="id_card_image_name" Type="String" ControlID="ctl00$ContentPlaceHolder$ctl01$frmStudents$FUID" PropertyName="FileName" />
    </InsertParameters>

C#:

protected void datasource_Inserting(object sender, SqlDataSourceCommandEventArgs e) {
            ASCIIEncoding encoding = new ASCIIEncoding();
            ControlParameter cp = new ControlParameter("@password", DbType.Binary, "ctl00$ContentPlaceHolder$ctl01$frmStudents$txtPassword", "Text");
            FormView frmStudents = (FormView)FindControl("frmStudents");
            TextBox txtPassword = (TextBox)(frmStudents.FindControl("txtPassword"));

            datasource.InsertParameters.Add("password", DbType.Binary, encoding.GetBytes(Cryptogrpahy.Encrypt(txtPassword.Text, basepage.crypt_password)).ToString());
        }

当我使用调试器执行该方法时,我看到了我的整个 InsertParameters,带有值...

另外,顺便说一句,由于 DB 类型,我不确定这是否会成功执行,因为我通常在常规 SQLParamater 中使用 VarBinary。

【问题讨论】:

    标签: c# asp.net sql-server datasource


    【解决方案1】:

    我找不到关于它的文档,但我怀疑发生的事情是在 Insert 事件期间创建的 SqlCommand 对象在执行事件处理程序代码之前从 SqlDataSource.InsertParameters 对象的状态获取其参数。您正在将参数添加到 SqlDataSource,但由于 SqlCommand 是在您将参数添加到 SqlDataSource 实例之前创建并从 SqlDataSource 实例接收其参数的,因此 SqlCommand 永远不会获取密码参数。

    要解决此问题,请尝试将密码参数添加到事件处理程序中的 e.Command.Parameters 而不是 datasource

    protected void datasource_Inserting(object sender, SqlDataSourceCommandEventArgs e) {
                ASCIIEncoding encoding = new ASCIIEncoding();
                ControlParameter cp = new ControlParameter("@password", DbType.Binary, "ctl00$ContentPlaceHolder$ctl01$frmStudents$txtPassword", "Text");
                FormView frmStudents = (FormView)FindControl("frmStudents");
                TextBox txtPassword = (TextBox)(frmStudents.FindControl("txtPassword"));
    
                e.Command.Parameters.Add("password", DbType.Binary, encoding.GetBytes(Cryptogrpahy.Encrypt(txtPassword.Text, basepage.crypt_password)).ToString());
            }
    

    【讨论】:

    • 谢谢,但这不起作用。 e.Command.Parameters.Add 只接受一个参数,我尝试传递该值但没有运气。
    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多