Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
public static void AddNewStudent(int Stu_id, string Stu_Name, string Class, string Major)
  {
  OracleDatabase db =(OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
 using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("AddNewStudent"))  
           {
  db.AddInParameter(cmd, "I_Stu_id", DbType.Int32, Stu_id);
  db.AddInParameter(cmd, "I_Stu_Name", DbType.String, Stu_Name);
  db.AddInParameter(cmd, "I_Class", DbType.String, Class);
  db.AddInParameter(cmd, "I_Major", DbType.String, Major);
    db.ExecuteNonQuery(cmd);
            }
  }

public static void UpdateStudent(int Stu_id, string Stu_Name, string Class, string Major)
  {
  OracleDatabase db =(OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
 using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("UpdateStudent"))  
           {
  db.AddInParameter(cmd, "I_Stu_id", DbType.Int32, Stu_id);
  db.AddInParameter(cmd, "I_Stu_Name", DbType.String, Stu_Name);
  db.AddInParameter(cmd, "I_Class", DbType.String, Class);
  db.AddInParameter(cmd, "I_Major", DbType.String, Major);
    db.ExecuteNonQuery(cmd);
            }
  }


User Interface

C#
private void Savebutton_Click(object sender, EventArgs e)
        {
            int student_id = Convert.ToInt32(this.SIDtextBox.Text);
            string stu_name = this.textBox1.Text;
            string class = this.textBox2.Text;
            string major= this.textBox3.Text;;

if(stu_id ==0)
{
            
PHTS.DataServices.cslMaterial.AddNewStudent(stu_id, stu_name, class, major);
            MessageBox.Show("Record Saved");
}
else
{

PHTS.DataServices.cslMaterial.UpdateStudent(stu_id, stu_name, class, major);
            MessageBox.Show("Record Saved");
}


        }


I changed the code and insterted a if statement but still not working. Can anyone help me how can a user save and update data using the same button.

Thanks
Posted
Updated 4-Aug-11 5:05am
v2

If your student already exists, call update. If not, call save.
 
Share this answer
 
v2
There's a number of ways.

You know that the Id must be null for a non-existing student (or maybe zero as you're storing it in an int)

So, either in c# code you can say "if the student id is zero call insert stored proc, else call update stored proc"

or you could have a single stored proc that says "if the Id is 0 insert the record, else update the record with the Id passed in.
 
Share this answer
 
Comments
rbjanaki 4-Aug-11 11:42am    
how can I add a zero to non-existing student_id
_Maxxx_ 5-Aug-11 0:27am    
I don't know what you mean? in the code you show, it appears you get the Id from a text box, and that it is zero when creating a new student - you pass that as a parameter to the stored proc.
If I am misunderstanding, maybe try rephrasing your question?

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