最近被朋友问到一个SqlDataSource调用带参数存储过程为什么不成功,代码如下:

代码
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()

        是啊,为什么不成功呢,提示的消息是太多的参数,这段代码看起来没什么问题啊。几轮搜索以后,觉得网上说的种种原因,最可能的是参数名要和字段名一致和参数要加@于是改成:

 

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("@empNo", TypeCode.String, user_name);  
SqlDataSource1.InsertParameters.Add("@empName", TypeCode.String, pass_word);  
 
SqlDataSource1.Insert()

存储过程里也进行相应的修改,把定义的传入参数和调用的地方都改为@empNo,@empName,仍然失败!错误消息变成了

Procedure or Function 'asdfg' expects parameter '@empNo', which was not supplied.就是没找到@empNo,此时,我有点迷茫。

于是把@去掉,再试,成功。代码如下:

 

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("empNo", TypeCode.String, user_name);
        SqlDataSource1.InsertParameters.Add("empName", TypeCode.String, pass_word);

        SqlDataSource1.Insert()



相关文章:

  • 2021-10-22
  • 2022-12-23
  • 2021-05-26
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案