Testing a class method

  • Thread starter Thread starter bthumber
  • Start date Start date
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?
 
bthumber said:
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?

Hard to tell. But I have a few comments/questions:

Are you sure your connection string is correct?
Your first catch is capturing the exception's message, but (just FYI) you
should not be displaying exception messages to an end user, as it is a
security hole.
Your last catch is trying to concatenate an Exception instance to a string,
rather than some data that the exception has (like it's message) being
concatenated to the string.
Why are you trying to write an ID into the database, rather than just
setting up an auto-increment primary key field in the database?
ExecuteNonQuery returns an Integer that represents how many records were
affected by the query. Have you checked it to see if it's coming back as 0
or 1?
I would remove the try...catch completely and try this so that if it fails,
the exception will blow up in your face and you'll have a better idea of
what's going wrong. I normally don't add try...catch until after I've
worked out the bugs of my code.

-Scott
 
Scott
Thanks, for your help. I forgot the ID was an Identity.

Scott M. said:
bthumber said:
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?

Hard to tell. But I have a few comments/questions:

Are you sure your connection string is correct?
Your first catch is capturing the exception's message, but (just FYI) you
should not be displaying exception messages to an end user, as it is a
security hole.
Your last catch is trying to concatenate an Exception instance to a string,
rather than some data that the exception has (like it's message) being
concatenated to the string.
Why are you trying to write an ID into the database, rather than just
setting up an auto-increment primary key field in the database?
ExecuteNonQuery returns an Integer that represents how many records were
affected by the query. Have you checked it to see if it's coming back as 0
or 1?
I would remove the try...catch completely and try this so that if it fails,
the exception will blow up in your face and you'll have a better idea of
what's going wrong. I normally don't add try...catch until after I've
worked out the bugs of my code.

-Scott
 
Back
Top