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:
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:
public class Base
{
public Base()
{
}
public string Message { get; set; }
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));
}
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:
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:
private void button1_Click(object sender, EventArgs e)
{
Gender gender = new Gender (-1,txtBoxTitle.Text);
gender.Save();
MessageBox.Show(gender.Message);
this.Close();
}