Solved

Send Parameters to Stored Procedure From Web Service

Posted on 2009-04-06
3
578 Views
Last Modified: 2012-05-06
I have a web service method that recieves an object called Requirement.  This object has an ID, Name, Description, Type and Category attribute.  I need to pass each of these items to a stored procedure that will run.  I'm running intot an error that says " no overload for method 'add' takes '2' arguments"

Can someone tell me what I'm doing wrong?  I just need to have this information broken down and sent to the stored procedure I have defined as usp_Insert_Requirement.
[WebMethod]
        public bool InsertRequirementData(Requirement requirementData)
        {
            string RequirementID;
            string RequirementName;
            string Description;
            int Category;
            int Type;
 
            ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["dbConnection"];
            DbProviderFactory pf = DbProviderFactories.GetFactory(cs.ProviderName);
           
 
            try
            {
                 using (DbConnection c = pf.CreateConnection())
                 {
 
                    c.ConnectionString = cs.ConnectionString;
                    DbCommand cmd = c.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                
                    //cmd.Connection = cn;
                    cmd.CommandText = "[dbo].[usp_Insert_Requirement]";
                    cmd.CommandType = CommandType.StoredProcedure;
 
                    #region Populate Parameters
                    //SqlParameter prmReturnValue = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
                    //prmReturnValue.Direction = ParameterDirection.ReturnValue;
 
                    SqlParameter prmRequirementID = cmd.Parameters.Add(RequirementID, SqlDbType.Int);
                    prmRequirementID.Direction = ParameterDirection.Input;
 
                        prmRequirementID.Value = 0;
                     
                    SqlParameter prmRequirementName = cmd.Parameters.Add("@RequirementName", SqlDbType.NVarChar);
                    prmRequirementName.Direction = ParameterDirection.Input;
                    
                        //prmRequirementName.Value = requirementData.RequirementName;
                        prmRequirementName.Value = requirementData.Name;
 
                    SqlParameter prmDescription = cmd.Parameters.Add("@Description", SqlDbType.NVarChar);
				    prmDescription.Direction = ParameterDirection.Input;
    				
					    prmDescription.Value = requirementData.Description;
 
                     SqlParameter prmCategory = cmd.Parameters.Add("@Category", SqlDbType.Int);
                     prmCategory.Direction = ParameterDirection.Input;
 
                        prmCategory.Value = requirementData.Category;
 
                     SqlParameter prmType = cmd.Parameters.Add("@Type", SqlDbType.Int);
                     prmType.Direction = ParameterDirection.Input;
 
                        prmType.Value = requirementData.Type;
                }
                #endregion
 
                /*
                ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["dbConnection"];
                DbProviderFactory pf = DbProviderFactories.GetFactory(cs.ProviderName);
                using (DbConnection c = pf.CreateConnection())
                {
 
                    c.ConnectionString = cs.ConnectionString;
                    DbCommand cmd = c.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    
                    cmd.CommandText = "uspInsert_Requirement " + "0" + "," + " '" + RequirementName + "' " + ", " + Category + ", " + Type;
                    c.Open();
                    int recordsAffected = cmd.ExecuteNonQuery();
                }
                 */
            }
            catch (Exception)
            {
 
                return false;
            }
 
            return true;
        }

Open in new window

0
Comment
Question by:tallfiji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 500 total points
ID: 24082488
Maybe you should just do cmd.Parameters.AddWithValue("@RequirementName",requirementData.Name);    

The AddWIthValue was added in 2.0 I believe.   If you cannot do that, you must follow the overloads that are given.  There is no overload for just 2 parameters with the add method.  Either drop the type or add on the extra parameters of the size and direction in the original call.
0
 
LVL 13

Expert Comment

by:sm394
ID: 24082517
where you calling web service
create object
Requirement requirementData =new Requirement();
 requirementData.ID=1111;
requirementData.Name="afasfasf";
requirementData .Description="adfafsdf";
requirementData .Type="fasfdasfa";

then pass only this object to service requirementData
also change your service method to
to use object attributes not local variable

in case you dont want to use object then replace method param from object to these local variables
string RequirementID;
            string RequirementName;
            string Description;
            int Category;
            int Type;
0
 

Author Comment

by:tallfiji
ID: 24402780
Here is the solution I came up with.  I had to create some stored proceudres that take in values and then execute some functions to create virtual tables that insert the appropriate data.
[WebMethod]
        public bool InsertRequirementData(Requirement requirementData)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
             string ServiceArea_Array;
            
            try
            {
                    ServiceArea_Array = "";
                    int count_ServiceAreas = requirementData.ServiceAreas.Count;
                    foreach (Requirement_ServiceArea reqServiceArea in requirementData.ServiceAreas)
                    {
 
                        if (count_ServiceAreas > 1)
                        {
                            //AddServiceAreaRelationship(reqServiceArea, ReqID);
                            ServiceArea_Array = ServiceArea_Array + reqServiceArea.ServiceAreaId + ", ";
                            count_ServiceAreas = count_ServiceAreas - 1;
                        }
                        else
                        {
                            ServiceArea_Array = ServiceArea_Array + reqServiceArea.ServiceAreaId;
                        }
 
                    }
 
                    conn = new SqlConnection("Server= dev-mossdb;DataBase=Hotel_IT_Blueprint;User Id=Hotel_IT_Blueprint;Password=Hotel_IT_Blueprint;");
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("uspINSERT_Requirement", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Name", requirementData.name));
                    cmd.Parameters.Add(new SqlParameter("@Description",  requirementData.description));
                    cmd.Parameters.Add(new SqlParameter("@Category",  requirementData.Category));
                    cmd.Parameters.Add(new SqlParameter("@Type",  requirementData.Type));
                    cmd.Parameters.Add(new SqlParameter("@ServiceArea_Array", ServiceArea_Array));
                    cmd.Parameters.Add(new SqlParameter("@Created_By", requirementData.createdBy));
                    cmd.Parameters.Add(new SqlParameter("@Legacy", requirementData.Legacy));
                    cmd.Parameters.Add(new SqlParameter("@Legacy_Req",requirementData.legacyReq));
               
                    rdr = cmd.ExecuteReader();
 
    
            }
            catch (Exception)
            {
 
                return false;
            }
 
            return true;
        }

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.NET: How to pass typed serialize using JavaScriptSerializer ==> Deserialize in C# 6 37
Amazon S3 .Net error 5 43
WPF Select Items 3 27
calculate running total 8 15
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question