B
bthumber
I'm trying to test this methods in my class using a console application. Here
is the console app.
static void Main(string[] args)
{
// This has two overload.
AppointmentClass ac = new AppointmentClass();
int first = 0;
string date = "10/9/09";
string city = "Buffalo";
string foot = "foot";
string dr = "Dr. Foot";
int distance = 20;
string meds = "none";
//int state = 0;
ac.InsertAppointment(0, date, city, foot, dr, 20, meds);
Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}", first,
date, city, foot, dr, distance, meds);
}
Here is the class method it inserts data:
public void InsertAppointment(int id, string timedate, string location,
string subject, string doctor, int distance, string newMeds)
{
string str = "INSERT INTO AppointmentsInformation (ID, TimeDate,
Location, Subject, DoctorID, Distance, NewMedicines) VALUES (@id, @timedate,
@location, @subject, @doctor, @distance, @newMeds)";
try
{
SqlConnection cn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(str, cn);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@timedate", timedate);
cmd.Parameters.AddWithValue("@location", location);
cmd.Parameters.AddWithValue("@subject", subject);
cmd.Parameters.AddWithValue("@doctor", doctor);
cmd.Parameters.AddWithValue("@distance", distance);
cmd.Parameters.AddWithValue("@newMeds", newMeds);
using (cn)
{
cn.Open();
cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
string msg = ex.Message;
}
catch (Exception e)
{
throw new Exception("Error on insertion " + e);
}
}
The insert is not working, what is wrong??? Is the exception handling correct?
is the console app.
static void Main(string[] args)
{
// This has two overload.
AppointmentClass ac = new AppointmentClass();
int first = 0;
string date = "10/9/09";
string city = "Buffalo";
string foot = "foot";
string dr = "Dr. Foot";
int distance = 20;
string meds = "none";
//int state = 0;
ac.InsertAppointment(0, date, city, foot, dr, 20, meds);
Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}", first,
date, city, foot, dr, distance, meds);
}
Here is the class method it inserts data:
public void InsertAppointment(int id, string timedate, string location,
string subject, string doctor, int distance, string newMeds)
{
string str = "INSERT INTO AppointmentsInformation (ID, TimeDate,
Location, Subject, DoctorID, Distance, NewMedicines) VALUES (@id, @timedate,
@location, @subject, @doctor, @distance, @newMeds)";
try
{
SqlConnection cn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(str, cn);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@timedate", timedate);
cmd.Parameters.AddWithValue("@location", location);
cmd.Parameters.AddWithValue("@subject", subject);
cmd.Parameters.AddWithValue("@doctor", doctor);
cmd.Parameters.AddWithValue("@distance", distance);
cmd.Parameters.AddWithValue("@newMeds", newMeds);
using (cn)
{
cn.Open();
cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
string msg = ex.Message;
}
catch (Exception e)
{
throw new Exception("Error on insertion " + e);
}
}
The insert is not working, what is wrong??? Is the exception handling correct?