Click here to Skip to main content
15,896,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello,
I want to save data in sql database using ADO. I have a Base class and some child classes, and I'm calling stored procedure in my Save method written in Base class. in the first and second run of the program , everything was fine,but after that when I want to save data, there is an error said: the Gender_Save method has too many arguments specified !
sometimes I've got another error says: procedure or function Gender_Save needs @Id parameter which was not supplied !

the number of parameters in sql procedure and Save method are the same(I think), and the IDENTITY of "Id" field in sql table is on.so it must be added automatically.The procedure is Executed and works fine in SQL.it seems there is no problem in sql.By the way,I' using DataLogic class in Data Layer for connecting to Database. my connection to sql is on cause I can load data from table in my form(using sql Load procedure)
I am new to programming, and really need your help to solve this :(


_____________________________________
here is my SQL Procedure:

SQL
CREATE PROC Gender_Save (@Id INT ,@Title NVARCHAR(50))			
AS
BEGIN
		SET NOCOUNT ON
		IF(EXISTS(SELECT 1 FROM Gender
			  WHERE Id=@Id ))
		BEGIN
		     UPDATE Gender
		     SET   Title=@Title
		     WHERE Id=@Id
		END
		
		ELSE
		BEGIN
		       INSERT Gender
		       SELECT @Title
		      
		END
		
		PRINT 'Save Done Successfuly'
END
_______________________________________________________
below code is Base class:
C#
public class Base
   {
       public Base()
       {

       }
       public string Message { get; set; }

       // SAVE method
       public  void Save()
       {

           DataLogic dl= new DataLogic();

           dl.command.CommandType = CommandType.StoredProcedure;
           dl.command.CommandText = string.Format("{0}_Save",this.GetType().Name);
           foreach(PropertyInfo propertyinfo in this.GetType().GetProperties())
           {


           dl.command.Parameters.Add(new SqlParameter(string.Format("@{0}", propertyinfo.Name), propertyinfo.Name));
                   //dl.command.Parameters.Clear();
           }
           try
           {
               dl.connection.Open();
               dl.command.ExecuteNonQuery();
                this.Message = "Save id done successfully";

               dl.connection.Close();
           }
           catch (SqlException ex)
           {
               this.Message = ex.Message;
           }

       }
}


________________________________________________________________________
here is the child class called Gender:
C#
public class Gender: Base
    {
        public Gender()
        {

        }
        public Gender(int id, string title)
        {
            Id = id;
            Title = title;
        }
        public int Id { get; set; }
        public string Title { get; set; }

    }

______________________________________________________
finally, when I click Save button on my form, this event happens:
C#
private void button1_Click(object sender, EventArgs e)
        {
            Gender gender = new Gender (-1,txtBoxTitle.Text);
            gender.Save();
            MessageBox.Show(gender.Message);
            this.Close();
        }
Posted
Updated 28-Mar-13 6:23am
v2

1 solution

All you need to do (probably) is add a Parameter to the Base class Save method to transfer the Id value:
C#
dl.command.Parameters.AddWithValue("@ID", Id);
You will probably have to move the Id from teh Gender class to the Base to avoid it not being defined.
 
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