Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am getting this error<<Procedure or function 'SearchTestName' expects parameter '@TestName', which was not supplied.>>
when trying to search value in my table using a stored procedure and c# code.

T-SQL:
SQL
CREATE PROC [dbo].[SearchTestName]
@TestName nvarchar(50)
AS
  BEGIN
     SELECT * FROM Test where (TestName like '%'+ @TestName +'%');
  END

c# code:

<pre> protected void Seach_Onclick(object sender, EventArgs e)
        {
           sqlcon.Close();
            sqlcon.Open();
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataTable ds = new DataTable();
           
            try
            {
                SqlCommand sqlcom = new SqlCommand("[SearchTestName]", sqlcon);
                sqlcom.Parameters.Add("@TestName", SqlDbType.NVarChar, 50).Value = txtSearchTestName.Text.Trim();
                adapter.SelectCommand = sqlcom;
                adapter.Fill(ds);
                labelSearch.Text = "Data Found!";
                Response.Redirect("ConnectionString.aspx", true);
                 
           
            }
            catch (Exception ex)
            {
                labelSearch.Text = ex.ToString();
            }
             
        }

What I am trying to do is to enter a value in a textbox then when hit on the button Search,I must get the data from the table test.
Please can someone help fixing this
Posted
Updated 9-Apr-13 10:03am
v4
Comments
Richard C Bishop 9-Apr-13 15:59pm    
That means you are not sending the parameter to your stored procedure. You need to create an SQLParameter and give it a value and name. See my solution below. You are adding a parameter as if you were doing a query, not using a stored procedure.
joshrduncan2012 9-Apr-13 16:01pm    
Besides I believe parameters.add is deprecated. See if you can try parameters.AddWithValue is preferred (especially if you are using Visual Studio 2012).
El Dev 9-Apr-13 16:22pm    
I have changed like that but I am getting this error:Procedure or function 'SearchTestName' expects parameter '@TestName', which was not supplied.
This how I have changed:
c# code:
SqlDataAdapter adapter = new SqlDataAdapter();
DataTable ds = new DataTable();

try
{
SqlCommand sqlcom = new SqlCommand("[SearchTestName]", sqlcon);
sqlcom.Parameters.AddWithValue("@TestName", 50).Value = txtSearchTestName.Text.Trim();
adapter.SelectCommand = sqlcom;
adapter.Fill(ds);
labelSearch.Text = "Data Found!";
Response.Redirect("ConnectionString.aspx", true);

}
catch (Exception ex)
{
labelSearch.Text = ex.ToString();
}
kishore sharma 10-Apr-13 2:21am    
I too faced this error but for me i closed the solution and reOpend and it worked.
I knew this is not the solution but for me that's the solution every time ,when i get this error.
Thanks

Do something like this:
SqlParameter dataParameter = new SqlParameter();
dataParameter.Value = 50;
dataParameter.ParameterName = "@TestName";

SqlCommand cmd = new SqlCommand(("[SearchTestName]", sqlcon);
SqlDataReader reader = null;

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(dataParameter);

conn.Open();
reader = cmd.ExecuteReader();
 
Share this answer
 
v3
Hello,

Replace your with this
C#
SqlDataAdapter adapter = new SqlDataAdapter();
            DataTable ds = new DataTable();
            SqlCommand sqlcom = new SqlCommand("selectdata", sqlcon);
            sqlcom.CommandType = CommandType.StoredProcedure;
            sqlcom.Parameters.Add(new SqlParameter("name", "Juhi Paunikar"));
            ds.Load(sqlcom.ExecuteReader());
            sqlcon.Close();


you are missed the line command type -4th line in my code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900