Use sqldatasource to call a stored procedure with parameters to insert data

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.