Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:未知构造函数';参数(字符串、类型代码、Int)和#x27;属于';System.Web.UI.WebControls.Parameter';_C#_Asp.net_Sql Server - Fatal编程技术网

C#:未知构造函数';参数(字符串、类型代码、Int)和#x27;属于';System.Web.UI.WebControls.Parameter';

C#:未知构造函数';参数(字符串、类型代码、Int)和#x27;属于';System.Web.UI.WebControls.Parameter';,c#,asp.net,sql-server,C#,Asp.net,Sql Server,在我的代码隐藏中,我有下面的代码来执行insert命令。当用户单击特定按钮时,将设置其中两个参数值。我这样做是因为我想重用SqlDataSource 我的问题是这行(@“Amount”,TypeCode.Int16,Amount)。具体错误是C#:System.Web.UI.WebControls.Parameter的构造函数参数(字符串、类型代码、Int)未知。有人能指出我做错了什么吗?或者,请告诉我可以采取什么步骤来调试它。我卡住了 protected void InsertFees

在我的代码隐藏中,我有下面的代码来执行insert命令。当用户单击特定按钮时,将设置其中两个参数值。我这样做是因为我想重用
SqlDataSource

我的问题是这行
(@“Amount”,TypeCode.Int16,Amount)
。具体错误是
C#:System.Web.UI.WebControls.Parameter的构造函数参数(字符串、类型代码、Int)未知。有人能指出我做错了什么吗?或者,请告诉我可以采取什么步骤来调试它。我卡住了

    protected void InsertFees_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters["@UserID"].Value = Convert.ToInt16(RadGrid1.SelectedValues["UserID"].ToString());
    e.Command.Parameters["@Type"].Value = "D";
    e.Command.Parameters["@Date"].Value = DateTime.Now;
    e.Command.Parameters["@Amount"].Value = "";
    e.Command.Parameters["@Description"].Value = "";
}

protected void RadButton2_Click(object sender, EventArgs e)
{

    try
    {
        int amount = Convert.ToInt16(RadNumericTextBox1.Text);
        InsertFees.InsertParameters.Add(new Parameter("@Amount", TypeCode.Int16, amount));
        InsertFees.InsertParameters.Add(new Parameter("@Description", TypeCode.String, RadTextBox2.Text));
        InsertFees.Insert();
        Label2.Text = "Insert Successful";
    }

    catch(Exception ex)
    {
        Panel1.Visible = true;
        Label3.Text = ex.ToString();
    }
}
int是一个Int32,而Int16是一个Short

也许试试这个

short amount = Convert.ToInt16(RadNumericTextBox1.Text);
InsertFees.InsertParameters.Add(new Parameter("@Amount", TypeCode.Int16, amount));
或者另一种方式

int amount = Convert.ToInt32(RadNumericTextBox1.Text);
InsertFees.InsertParameters.Add(new Parameter("@Amount", TypeCode.Int32, amount));


我同意
困惑的回答,但您需要以这种方式将参数传递给您的SQLDataSource

InsertFees.InsertParameters["Amount"].DefaultValue = amount.ToString();

当您添加参数时,例如..
InsertFees.InsertParameters.Add(新参数(“@Amount”,TypeCode.Int16,Amount)),它将再次向Insert parameters集合中添加参数,您已经在SQLDataSource InsertParameter集合中定义了该集合

谢谢…我没有意识到int是int32。今天我学到了一些东西。很遗憾,建议的修复程序返回相同的错误。谢谢。如何在上面的代码中指定dbtype?它需要一个字符串,我需要给它一个int。您可以使用amount.ToString()并传递值,因为DefaultValue属性需要字符串值,但SQLDataSource会自动对此进行处理,您在SQLDataSource Insert参数集合中提到的内容。您必须在服务器上修补
.NET framework
,因为此功能已包含在.NET 2.0的更高版本Service Pack中。
InsertFees.InsertParameters["Amount"].DefaultValue = amount.ToString();