Add two parameters in SqlParameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nzsquall
    New Member
    • Oct 2009
    • 22

    Add two parameters in SqlParameter

    Hi Code Masters,

    I am an ASP beginner, I have a stored precodure which accepts two input parameters. In my ASP project, I have a button click event
    Code:
    protected void OnAddClick(object sender, EventArgs e)
        {
            string concept = txtConcept.Text;
            string keyword = txtKeyword.Text;
             .......
        int result = Helper.ConceptInsert(concept,keyword);
        }
    In my Helper.cs, I have a method as
    Code:
            public static int ConceptInsert(string description, string keyword)
            {
                int result = -1;
                string connString = ConfigurationManager.ConnectionStrings["MySpaceConnectionString"].ConnectionString;
                string commString = "exec dbo.Concept_Insert @description";
    
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand comm = new SqlCommand(commString, conn))
                    {
                        comm.Parameters.Add(new SqlParameter("@description", description));
                        comm.Parameters.Add(new SqlParameter("@keyword", keyword));  
                        conn.Open();
                        result = comm.ExecuteNonQuery();
                    }
                }
    
                return result;
            }
    But when I run the code, error message is always Procedure or function 'Concept_Insert ' expects parameter '@keyword', which was not supplied.

    How can I pass those two parameters together to the stored procedure? Thanks in advanced.
    Last edited by jhardman; Oct 14 '10, 05:48 AM. Reason: Accidentally posted in the classic asp forum. I'm moving you to the asp.net forum.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You could add a parameter to the SqlCommand.Para meters using one of the Add() methods or using the AddWithValue() method

    Here is an example using an Add method:
    Code:
    public static int ConceptInsert(string description, string keyword)
            {
                int result = -1;
                string connString = ConfigurationManager.ConnectionStrings["MySpaceConnectionString"].ConnectionString;
                string commString = "exec dbo.Concept_Insert @description";
     
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand comm = new SqlCommand(commString, conn))
                    {
    
    
                       comm.Parameters.Add("@description", SqlDbType.VarChar)
                       comm.Parameters("@description").Value = description
                 
                       'comm.Parameters.Add("@keyword", SqlDbType.VarChar)
                       'comm.Parameters("@keyword").Value = keyword
    
                        conn.Open();
                        result = comm.ExecuteNonQuery();
                    }
                }
     
                return result;
            }
    -Frinny

    Comment

    • Nzsquall
      New Member
      • Oct 2009
      • 22

      #3
      Hi Frinny,

      Thanks for your reply, but sorry it didn't appear to be working.

      I got error messages as no SqlDbType and Error 2 Non-invocable member System.Data.Sql Client.SqlComma nd.Parameters' cannot be used like a method.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Post what you have.

        -Frinny

        Comment

        • danp129
          Recognized Expert Contributor
          • Jul 2006
          • 323

          #5
          On this line: string commString = "exec dbo.Concept_Ins ert @description";

          Add ", @keyword" after @description

          Comment

          Working...