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