Recently, a friend asked why a sqldatasource call with a parameter stored procedure failed,CodeAs follows:
Code
String User_name = (Textbox) This . Detailsview1.rows [ 1 ]. Cells [ 1 ]. Controls [ 0 ]). Text. tostring (). Trim ();
String Pass_word = (Textbox) This . Detailsview1.rows [ 2 ]. Cells [ 1 ]. Controls [ 0 ]). Text. tostring (). Trim ();
Sqldatasource1.insertcommand = " Pro_newuser " ;
Sqldatasource1.insertcommandtype = Sqldatasourcecommandtype. storedprocedure;
Sqldatasource1.insertparameters. Add ( " X " , Typecode. String, user_name );
Sqldatasource1.insertparameters. Add ( " Y " , Typecode. String, pass_word );
Sqldatasource1.insert ()
Yes, why not? The prompt message is too many parameters. This code looks okay. After several rounds of searching, I think the most likely reason I have mentioned on the internet isThe parameter name must be consistent with the field name and the parameter must be added @,Changed:
String user_name = (textbox) This. detailsview1.rows [1]. cells [1]. controls [0]). Text. tostring (). Trim ();
String pass_word = (textbox) This. detailsview1.rows [2]. cells [1]. controls [0]). Text. tostring (). Trim ();
Sqldatasource1.insertcommand = "pro_newuser ";
Sqlperformance1.insertcommandtype = sqlperformancecommandtype. storedprocedure;
Sqldatasource1.insertparameters. Add ("@ empno", typecode. String, user_name );
Sqldatasource1.insertparameters. Add ("@ empname", typecode. String, pass_word );
Sqldatasource1.insert ()
In the stored procedure, modify the defined input parameters and call locations to @ empno and @ empname! The error message is changed
Procedure or function 'asdfg' expects parameter '@ empno', which was not supplied. It is not found @ empno. At this time, I am a little confused.
Remove @ and try again. The Code is as follows:
Code
String user_name = (textbox) This. detailsview1.rows [1]. cells [1]. controls [0]). Text. tostring (). Trim ();
String pass_word = (textbox) This. detailsview1.rows [2]. cells [1]. controls [0]). Text. tostring (). Trim ();
Sqldatasource1.insertcommand = "pro_newuser ";
Sqlperformance1.insertcommandtype = sqlperformancecommandtype. storedprocedure;
Sqldatasource1.insertparameters. Add ("empno", typecode. String, user_name );
Sqldatasource1.insertparameters. Add ("empname", typecode. String, pass_word );
Sqldatasource1.insert ()
However, two pieces of data are generated. This code is written in detailsviewincluiteminserting. Based on previous experience, this method is triggered when you click the insert button of detailsview1. As long as there is code in insertcommand, It will be executed, the last SQL statement, sqldatasource1.insert (), has the same effect. Therefore, it is equivalent to executing insertcommand twice, so there will be two pieces of data.
It is strange that I always think that I have encountered problems that may be encountered by others, but this exception is not a problem because I have tried various search conditions. AllArticleThe @ placeholder is used to call the stored procedure for passing parameters.