Reputation: 23
I try to make a simpe app with asp and I have a problem.
Procedure or function 'stored_pr' expects parameter '@name', which was not supplied.
My Stored Procedure:
ALTER proc [dbo].[spq]
@name nvarchar(max)
as
insert into tableq (name)
Values
(@name)
GO
My code in ASP:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
SqlConnection con;
//Here we declare the parameter which we have to use in our application
SqlCommand cmd = new SqlCommand();
SqlParameter @name = new SqlParameter();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("server=(local); database=**;uid=DefaultAppPool;pwd=*****");
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)this.Page.FindControl("Pole")).Text;
cmd = new SqlCommand("spq", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
and
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Pole" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit Record" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Please any help.. thank you
Upvotes: 1
Views: 63
Reputation: 22743
Your problem more than likely lies in these 2 lines, although there might be other issues in the code:
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)this.Page.FindControl("Pole")).Text;
cmd = new SqlCommand("spq", con);
You're basically adding a parameter to the command and then effectively clearing it by newing it up again. Swap the 2 lines around and you should get past that error.
Take a look at the documentation for a sample:
MSDN: SqlCommand.Parameters Property
Upvotes: 1
Reputation: 166
Bug! Swap these two lines:
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)this.Page.FindControl("Pole")).Text;
cmd = new SqlCommand("spq", con);
Upvotes: 1