No value given for one or more required parameters(Execute SQL statement error)

Lionsure 2020-08-20 Original by the website

When executing SQL statements in C#, sometimes an error like "No value given for one or more required parameters" will be reported. This error message is very vague, and it can't tell where it is. If you have never encountered such an error, it is a bit difficult to troubleshoot.

"No value given for one or more required parameters" error usually appears in the query statement(i.e. Select statement), and will not appears in the insert or update statement. Let's look at the specific error first, and then look at the solution.

 

1. "No value given for one or more required parameters" error

If you execute the following SQL statement:

string strSQL = "Select Title,model,detail,adddate From products Order By adddate Desc";

 

Description: System.Exception: No value given for one or more required parameters.

Source Error:

line 203:                             line       catch (Exception e)
       line 204:                             line       {
       line 205:                                           line       throw new Exception(e.Message);
       line 206:                             line       }
       line 207:                             line       finally

Source File: D:\Webdata\classes\SqlExe.csline       line:  205

 

Stack Trace:

[Exception: No value given for one or more required parameters.]
line       EpWeb.privates.classes.SqlData.dataTable(String sqlstr) in D:\Webdata\classes\SqlExe.cs:205
line       EpWeb.feedback.index.BindFeedback() in D:\Webdata\product\index.aspx.cs:24
line       EpWeb.feedback.index.Page_Load(Object sender, EventArgs e) in D:\Webdata\product\index.aspx.cs:16
line       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
line       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
line       System.Web.UI.Control.OnLoad(EventArgs e) +91
line       System.Web.UI.Control.LoadRecursive() +74
line       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

 

2. Solution of "No value given for one or more required parameters"

In fact, the error is very simple. One of the fields is mistaken. The detail field is not defined in the database, but the content field is defined. Therefore, the problem is solved by changing detail to content. The modified SQL statement becomes:

string strSQL = "Select Title,model,content,adddate From products Order By adddate Desc";

For vague errors such as executing SQL statements, you can use the method of one-by-one elimination. First, check whether the Select keywords are wrong, then check whether there is the field in the data table, and finally check whether the sort field is selected.