Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public static void AddNewStudent(int Stu_id, string Stu_Name, string Class, string Major, Datetime Enddate)
  {
  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.AddInParameter(cmd, "I_Enddate", DbType.Date, Enddate, Enddate == null? DBNull.Value : Enddate);
    db.ExecuteNonQuery(cmd);
            }
  }


User Interface

private void Savebutton_Click(object sender, EventArgs e)
{
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;
	    Datetime enddate = Datetime.Parse(this.textbox4.text);
 if (this.textbox4.Text == "")
            {
                enddate = null;// DBNull.Value;
            }
            else
            {
                enddate = DateTime.Parse(this.textbox4.Text);
            }

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

}
}
}



Procedure

Create or replace PRPCEDURE AddNewStudent (
I_STU_ID    Number,    
I_STU_Name  Varchar2,
I_Address  Varchar2,
I_Phone  Varchar2,
I_Enddate   date,
)
As
Begin
Insert into I_Student
(
Student_ID, STU_Name, Address, Phone, Enddate
)
Values
(
I_Student_ID, I_STU_Name, I_Address, I_phone, I_Date , I_Enddate
);


Finally I got it thanks for helping
Posted
Updated 9-Aug-11 5:39am
v3

1 solution

Try:
C#
db.AddInParameter(cmd, "I_Enddate", DbType.Date, Enddate == null ? DBNull.Value : Enddate);
 
Share this answer
 
Comments
rbjanaki 9-Aug-11 9:53am    
Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'System.DateTime' now I am getting this error
OriginalGriff 9-Aug-11 9:58am    
Sorry, yes, I forgot that! You will have to expand it into a "proper" if..else:
if (Enddate == null)
db.AddInParameter(cmd, “I_Enddate”, DbType.Date, DBNull.Value);
else
db.AddInParameter(cmd, “I_Enddate”, DbType.Date, Enddate);
Wendelius 9-Aug-11 10:11am    
If I recall correctly, you don't have to expand the statement, just cast both to objects:
Enddate == null ? (object)DBNull.Value : (object)Enddate
rbjanaki 9-Aug-11 10:23am    
Hi Mika

Do you mean I have to write like this Enddate == null ? (enddate)DBNull.Value : (enddate)Enddate

thanks
Wendelius 9-Aug-11 10:47am    
No, I meant that you can cast them to objects. So the whole line should look something like:
db.AddInParameter(cmd, "I_Enddate", DbType.Date, Enddate == null ? (object)DBNull.Value : (object)Enddate);

About the string error, check that the apostrophes are correctly. For some reason they are differently (a bit like italic) in the post.

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