Reputation: 781
I have a method here I created for homework. I believe it works, and I want to test it to work. So here's the method:
public static bool UpdatePerson (Personnel person, out string result)
{
result = "update not successful";
bool flag = false;
System.Data.SqlClient.SqlCommand updatePerson = new System.Data.SqlClient.SqlCommand();
updatePerson.Connection = Data.con;
//updatePerson.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("perFirstName", person.First);
SqlParameter p2 = new SqlParameter("perMiddleName", person.Middle);
SqlParameter p3 = new SqlParameter("perLastName", person.Last);
SqlParameter p4 = new SqlParameter("ID", person.PersonnelID);
updatePerson.Parameters.Add(p1);
updatePerson.Parameters.Add(p2);
updatePerson.Parameters.Add(p3);
updatePerson.Parameters.Add(p4);
updatePerson.CommandText = "Update tblPersonnel Set perFirstName = " + p1 + " perMiddleName = " + p2 + " perLastName = " + p3 + "Where ID = " + p4;
try
{
Data.con.Open();
updatePerson.ExecuteNonQuery();
result = "Update Successful";
flag = true;
}
catch (Exception ex)
{
result = ex.Message;
}
finally
{
if (Data.con.State == System.Data.ConnectionState.Open)
Data.con.Close();
}
return flag;
}
Now here's the test coding:
using MovieLibrary;
namespace Test
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string result = " ";
MovieLibrary.Personnel update = MovieLibrary.Personnel.UpdatePerson(MovieLibrary.Personnel, out result);
if (update != null)
this.Label1.Text = result;
}
}
The only thing giving me a problem is inserting data in the parameters. This gives me an error saying MovieLibrary.Personnel is a type and does not belong there.
Upvotes: 0
Views: 64
Reputation: 65097
You need to pass an instance of Personnel
. Currently, you're using the type's name as an argument.. which you cannot do. Also.. your method returns bool
.. not a Personnel
object.
Hopefully this makes it more clear. This is what you have, which is wrong:
MovieLibrary.Personnel update =
MovieLibrary.Personnel.UpdatePerson(MovieLibrary.Personnel, out result);
// ^^^^^^^^^^^^^^^^^^^^^^ Wrong
However, this is what you need:
Personnel p = new Personnel();
// set properties here
// it returns bool
bool updated =
MovieLibrary.Personnel.UpdatePerson(p, out result);
// ^^ Right.. an instance
Also, you are returning bool
, but also have an out
parameter for the result. Consider making it return the result (or deciding what to do based on the boolean return value.. or possibly let the exception get thrown out of the method).
Upvotes: 3