一个简单的存储过程来更新表找不到参数

时间:2015-04-20 14:27:31

标签: asp.net sql-server stored-procedures parameters

我有一个表和一个存储过程来更新表的几列。这是存储过程:

   Create proc spReg
   @UserId nvarchar(10),
   @Experience nvarchar(5),
   @Timings nvarchar(10),
   @Notes nvarchar(max),
   @PrefferedState nvarchar(20),
   @PrefferedCity nvarchar(20),
   @ResumePath varchar(256)
   as
   begin
   Update tblRegCoachTrainers Set Experience=@Experience,Timings=@Timings,Notes=@Notes,PrefferedState=@PrefferedState,PrefferedCity=@PrefferedCity,ResumePath=@ResumePath where UserId=@UserId;
   end

以及相同的代码隐藏:

  string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("spReg", con);
            cmd.Parameters.AddWithValue("@UserId", txtUserId.Text);
            cmd.Parameters.AddWithValue("@Experience", ddlExperience.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@Timings", ddlPartime.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@Notes", txtNotes.Text);
            cmd.Parameters.AddWithValue("@PrefferedState", ddlState.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@PrefferedCity", ddlCity.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@ResumePath", "/Attachments/" + hfResumePath.Value);
            cmd.ExecuteNonQuery();
            lblRegMessage.Text = "Your profile was created successfully.";
        }

我通过设置一个断点调试程序,并将所有必要的值传递给它们各自的参数。它仍然会抛出异常

    cmd.ExecuteNonQuery();

错误消息:'spReg'需要参数'@UserId',这是未提供的。

1 个答案:

答案 0 :(得分:2)

默认为Text命令类型,添加:

cmd.CommandType = CommandType.StoredProcedure;