0

I know this thread will probably get flagged as duplicate but I have reviewed all similar posts but none seems to help resolve this issue:

I have this code snip:

Dim loc As String = DirectCast(GridView1 _
            .FooterRow.FindControl("txtLocation"), TextBox).Text

cmd.CommandText = "insert into Locations(Location " & _
") values(@Location);" & _
"select LocationID, Location from locations"

cmd.Parameters.AddWithValue("@Location", SqlDbType.VarChar).Value = loc

Just to keeping it simple.

This works great.

Then I turned this inline code into stored procedure:

ALTER PROCEDURE [dbo].[spx_AddNewLoc]   
            @Location varchar(150)
AS
set nocount on
    BEGIN   --add new request type
     INSERT INTO Locations(Location)VALUES(@Location)
     SELECT LocationID, Location from locations
     END
set nocount off

Then called it in my codefile:

Dim loc As String = DirectCast(GridView1 _
            .FooterRow.FindControl("txtLocation"), TextBox).Text
cmd.CommandText = "spx_AddNewLoc "
cmd.Parameters.AddWithValue("@Location", SqlDbType.VarChar).Value = loc

I just keep getting "Procedure expects @location which was not supplied."

Any ideas what I am missing?

<FooterTemplate>
    <asp:TextBox ID="txtlocation" runat="server" placeholder="Please enter location here..." style="width:400px;"></asp:TextBox><br />
</FooterTemplate>     
<FooterTemplate>
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick = "AddNewLocation" />
</FooterTemplate>

'//** latest code below

Protected Sub AddNewLocation(ByVal sender As Object, ByVal e As EventArgs)
    Dim IsAdded As Boolean = True
    Dim loc As String = DirectCast(GridView1 _
            .FooterRow.FindControl("txtLocation"), TextBox).Text
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "insert into Locations(Location " & _
    ") values(@Location);" & _
    "select LocationID, Location from locations"
    cmd.Parameters.Add("@Location", SqlDbType.VarChar).Value = loc
    If IsAdded = True Then
        lblMsg.Text = (Convert.ToString("'") & loc) + "' location added successfully!"
        lblMsg.ForeColor = System.Drawing.Color.Green
    Else
        lblMsg.Text = (Convert.ToString("Error while adding '") & loc) + "' locaton!"
        lblMsg.ForeColor = System.Drawing.Color.Red
    End If

    GridView1.DataSource = GetData(cmd)
    GridView1.DataBind()
End Sub
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tairoc
  • 635
  • 1
  • 6
  • 18
  • Are you 100% sure `loc` is not `Nothing` And what does the footer of the GridView look like? – VDWWD Feb 08 '17 at 20:38
  • @VDWWD, first of all, thank you for your response. I am absolutely sure that the loc is getting value passed to it from the gridview. Everything works perfectly when I use the embedded I posted. Here is what the GridView footer looks like. Please see above for updated code. – Tairoc Feb 08 '17 at 20:47
  • Possible duplicate of [When executing command.Prepare() I have “SqlCommand.Prepare method requires all parameters to have an explicitly set type” error](http://stackoverflow.com/q/36554082/11683) – GSerg Feb 08 '17 at 20:58
  • You are using `AddWithValue`. You [should not](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/). It [does not do what you think](http://stackoverflow.com/questions/36554082/when-executing-command-prepare-i-have-sqlcommand-prepare-method-requires-all#comment60708626_36554082) it does. – GSerg Feb 08 '17 at 20:59
  • @GSerg, there is a conflict then. I was using this: cmd.Parameters.Add then I got a message that it is deprecated. Suggested I used AddWithValue. – Tairoc Feb 08 '17 at 21:05
  • Everything looks right. What I would do is, first, set a breakpoint and run your code right up to where your `AddWithValue` line is and make sure `loc` isn't Nothing. If it isn't Nothing, then I would run a tool such as SQL Profiler or Fiddler and look at the actual SQL code that's getting passed to the SQL Server is. – Icemanind Feb 08 '17 at 21:06
  • @Icemanind, I will try setting the breatpoint. I did however, try SQL Profiler and everything seems work fine. This is pure mystery. – Tairoc Feb 08 '17 at 21:15
  • Have you set the command type to StoredProcedure? – Bugs Feb 08 '17 at 21:17
  • 1
    @Bugs, yes: cmd.CommandType = CommandType.StoredProcedure – Tairoc Feb 08 '17 at 21:35
  • @Tairoc Only [one overload of `Parameters.Add`](https://msdn.microsoft.com/en-us/library/9dd8zze1(v=vs.110).aspx) is deprecated. There are [other overloads](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.add(v=vs.110).aspx) that are perfectly fine, please use them. [reposted due to wrongly pasted links]. – GSerg Feb 08 '17 at 22:00
  • I wonder if it's anything to do with the nocount? That's the only thing I can see being the cause in that procedure without actually testing. Might be worth removing those lines in the SP and trying it although I can't see why it would cause a problem. – Bugs Feb 08 '17 at 22:00
  • 1
    @Bugs `set nocount on` suppresses sending informational noise to the client. Everything works better with it. `set nocount off` in the end is not required because `set` settings are automatically reverted when the SP exits, but it does not harm either. – GSerg Feb 08 '17 at 22:03

4 Answers4

2

The reason for the error is because you have not set the CommandType to use CommandType.StoredProcedure. I have mocked up both examples to highlight this error.

This is the code prior to me setting the CommandType:

enter image description here

This is the code once I had set the CommandType:

enter image description here

This is the working code:

Using con As New SqlConnection(strConnString),
      cmd As New SqlCommand With {.CommandText = "spx_AddNewLoc", .CommandType = CommandType.StoredProcedure,
                                  .Connection = con}

    cmd.Parameters.Add("@Location", SqlDbType.VarChar).Value = location

    con.Open()

    cmd.ExecuteNonQuery()

End Using
Bugs
  • 4,491
  • 9
  • 32
  • 41
1

Quickly translated from C#. I've tested it locally and it works. You should eventually wrap the code in a Using block to dispose and close the objects/connection properly.

Dim loc As String = "test"

Dim Command As New SqlCommand("spx_AddNewLoc")
Command.Parameters.Add("@Location", SqlDbType.VarChar).Value = loc

Dim Connection As New SqlConnection(connStr)
Command.Connection = Connection
Command.CommandType = CommandType.StoredProcedure
Connection.Open()

Command.ExecuteNonQuery()

Connection.Close()

C#

string storedProcedure = "spx_AddNewLoc";
string location = "Amsterdam";

using (SqlConnection connection = new SqlConnection(connStr))
using (SqlCommand command = new SqlCommand(storedProcedure, connection))
{
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@Location", SqlDbType.VarChar).Value = location;

    connection.Open();
    command.ExecuteNonQuery();
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
0

As far as you have changed it to a Stored Procedure, you should change .CommandType of your SqlCommand.

cmd.CommandType = CommandType.StoredProcedure;

SqlCommand.CommandType Property

McNets
  • 10,352
  • 3
  • 32
  • 61
  • This was my first thought. Not sure why someone would downvote. – Bugs Feb 08 '17 at 21:18
  • And if not set correctly produces funny errors like this. It's a legitimate answer. – Bugs Feb 08 '17 at 21:21
  • @McNets, sorry I did not downvote you. I don't downvote those helping me. By the way, I already have exact line on my code, I just did not post it here. – Tairoc Feb 08 '17 at 21:32
  • @Tairoc I'm not blaming you. Please, don't worry about it. Usually, if I downvote an answer, I'd like to let a note about the reason. – McNets Feb 08 '17 at 21:35
  • @Tairoc please don't think it was aimed at you. My argument was it's a valid answer and someone has simply come along and downvoted. It would be worth updating your question with the line however :) – Bugs Feb 08 '17 at 21:36
  • I downvoted this answer because it could have not been correct. Even though the OP did not not explicitly state they had `CommandType.StoredProcedure`, they would have been getting a different error if they hadn't had it. There is no way they would be getting the "Procedure expects..." error when they were not calling a procedure. The actual problem must be [with `AddWithValue`](http://stackoverflow.com/questions/42122977/procdure-expects-location-parameter-which-was-not-supplied-it-is-driving-me-nu#comment71415036_42122977). – GSerg Feb 08 '17 at 21:50
  • @Bugs, et al, you guys have been incredibly helpful and patient and I really appreciate it. There appears to be something screwy somewhere with using stored procedure which is my preferred way. However, I added to go ahead keep using inline INSERT statement right now as that's that only code that works. SP doesn't. I have posted the exact the works currently as is. – Tairoc Feb 08 '17 at 21:51
  • @Tairoc Please use any other overload of `Parameters.Add` as I have [suggested](http://stackoverflow.com/questions/42122977/procdure-expects-location-parameter-which-was-not-supplied-it-is-driving-me-nu#comment71415627_42122977). You keep trying to use the wrong and deprecated one. – GSerg Feb 08 '17 at 21:52
  • Hi @GSerg, I think the main problem is to set properties of SqlCommand correctly, if not, it is not able to acquire and configure the correct parameters. For me, this is the first issue to fix. – McNets Feb 08 '17 at 21:53
  • @GSerg I'm not sure that's entirely correct. I can understand why you think that but I've seen funny errors myself when not explicitly setting the command type. It wasn't stated in the question and so it was worth mentioning. I'm not actually on my dev pc to replicate the error but I'll be looking tomorrow as it is interesting nonetheless. – Bugs Feb 08 '17 at 21:54
  • @McNets Yes, it is important to set properties of `SqlCommand`. What I am saying is that from the evidence it was clear that the OP already had `cmd.CommandType = CommandType.StoredProcedure;`. – GSerg Feb 08 '17 at 21:55
  • @GSerg I'm sorry, but when I answered the question it wasn't. But, it doesn't matter, I never used AddWithValue and maybe this is the correct answer. – McNets Feb 08 '17 at 22:04
  • @GSerg This is the reason. I've just tested and by not setting the `CommandType` I get the error. The moment I set the `CommandType` to `CommandType.StoredProcedure` error goes away. – Bugs Feb 09 '17 at 12:54
0

Rename AddWithValue to Add. In your code AddWithValue accepts value as second param and I suspect it makes your parameter @Location of other type than varchar, that's why it cannot be found on a procedure execute. https://msdn.microsoft.com/en-us/library/wbys3e9s(v=vs.110).aspx

  • like I said in my last post, I had Add before but due to this same issue, I changed it to AddWithValue. I have changed it again back to Add but still no love. – Tairoc Feb 08 '17 at 21:13
  • Try to set breakpoint right after parameter has been added and lookup for it in the parameters collection to ensure it has correct name, type and size. – Alexander Bakhar Feb 08 '17 at 21:18
  • cmd.Parameters.AddWithValue("@Location", SqlDbType.VarChar) creates parameter of type Int and value 22 because SqlDbType.VarChar passed as value. Then .Value = loc makes this parameter of type NVarChar. Try to change the type of @Location to NVarchar in the procedure definition and it should work – Alexander Bakhar Feb 08 '17 at 21:34