gpt4 book ai didi

c# - 如何摆脱 "No value given for one or more required parameters"

转载 作者:行者123 更新时间:2023-11-30 22:31:02 29 4
gpt4 key购买 nike

我有以下代码,

当单击 Gridview 中的更新按钮时,它会抛出一个错误:没有为一个或多个必需参数给出值

但是再次运行时可以看到新更新的数据,连数据库都更新了。

string updateSql = "UPDATE RateCenters SET RateCenterName = @RateCenterName, Province = @Province, QuantityThreshold = @QuantityThreshold" + " WHERE RateCenterID= @RateCenterID";

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

DropDownList ddl = (DropDownList)row.FindControl("DropDownList2"); // assigning the dropdownlist item to 'ddl'

TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName"); // assigning textbox input item

TextBox quantityThreshold = (TextBox)row.FindControl("txtQuantityThreshold"); // assigning textbox input item

Label ratecenterid = (Label)row.FindControl("Label1"); // assigning the label value

// OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());

string scon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\arjun.giridhar\my documents\visual studio 2010\Projects\BillingApplicationNew\BillingApplicationNew\App_Data\db1.mdb;Persist Security Info=False";

OleDbConnection conn = new OleDbConnection(scon);


try
{
OleDbCommand cmd = new OleDbCommand(updateSql, conn);

cmd.CommandText = updateSql;

cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = rateCenterName.Text;
cmd.Parameters.Add("@Province", OleDbType.VarChar).Value = ddl.SelectedItem.Text;
cmd.Parameters.Add("@QuantityThreshold", OleDbType.Integer).Value = Convert.ToInt32(quantityThreshold.Text);
cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = Convert.ToInt32(ratecenterid.Text);

conn.Open();

cmd.ExecuteNonQuery();

GridView1.EditIndex = -1; //refreshing
GridView1.DataBind();
}

catch (OleDbException ex)
{
throw (ex);
}

finally
{
conn.Close();
conn.Dispose();
}

下面是上述错误的堆栈跟踪:

没有为一个或多个必需参数提供值。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。异常详细信息:System.Data.OleDb.OleDbException:没有为一个或多个必需参数提供值。

来源错误:在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。堆栈跟踪:[OleDbException (0x80040e10): 没有为一个或多个必需参数提供值。] System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)+1070856 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +247 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +194 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior 行为,Object& executeResult)+58 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,字符串方法)+167 System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +113 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand命令,DataSourceOperation操作) +394 System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary 键,IDictionary 值,IDictionary oldValues)+697 System.Web.UI.DataSourceView.Update(IDictionary 键、IDictionary 值、IDictionary oldValues、DataSourceViewOperationCallback 回调)+95 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1226 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +716 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs args)+37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121 System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs args)+37 System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs e) +125 System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument) +169 System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176 System.Web.UI.Page.ProcessRequestMain( bool 值 includeStagesBeforeAsyncPoint, bool 值 includeStagesAfterAsyncPoint)+5563**

请检查代码并帮助我解决此任务

问候,阿琼

最佳答案

来自 OleDbCommand.Parameters :

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

因此请尝试使用未命名参数(完整示例请参阅文档)。

关于c# - 如何摆脱 "No value given for one or more required parameters",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9431989/

29 4 0