public bool addNewVoyageData(VoyageData data) { bool result = false; //creating the connection to the database DbConnection connection = new DbConnection(); SqlConnection con = connection.connectToDatabase(); //create the instance to the SqlCommand Object SqlCommand cmd = new SqlCommand(); //Command Object population to the connection and providing the Query string to the sql command object cmd.Connection = con; cmd.CommandText = "INSERT INTO VoyageData(VoyageUserID,ImageData,VoyageContent,VoyageState,VoyageCity)" + "VALUES(@userId,@image,@content,@state,@city)"; //using the parameterized query cmd.Parameters.AddWithValue("@userId", data.UserId); cmd.Parameters.AddWithValue("@image", data.imageData); cmd.Parameters.AddWithValue("@content", data.VoyageContent); cmd.Parameters.AddWithValue("@state", data.VoyageState); cmd.Parameters.AddWithValue("@city", data.VoyageCity); //try block starting try { //open the connection to database con.Open(); //returns the number of rows affected in the database int rows_affected = cmd.ExecuteNonQuery(); //return the result of the operation result = true; } //starting of the catch block catch (Exception ex) { //just for checking for the developer purpose Console.WriteLine("Error Occured:" + ex.Message); //using the custom exception to return the error message at the client side Custom_Exception custom_Exception = new Custom_Exception(); //Giving the appropriate values to the custom exception object custom_Exception.Title = "Voyage_Guide Custom Exception Message"; custom_Exception.ExceptionMessage = ex.Message; //throwthe fault exception throw new FaultException <Custom_Exception>(custom_Exception); } finally { //Closing the Database Connection con.Close(); } return(result); }
//Login Service Implmentation public AuthenticateReply authenticateUser(AuthenticateUser authUserData) { //Creating the instance of the Message Contract Typw which is required to send to the user AuthenticateReply authenticateReply = new AuthenticateReply(); authenticateReply.VoyageisAuthenticated = false; //creating the instance of the Conenction DbConnection connection = new DbConnection(); SqlConnection con = connection.connectToDatabase(); //Creating the instance of the Sql Command Object SqlCommand cmd = new SqlCommand(); cmd.Connection = con; //Parameterized query for the Database to authenticate the user cmd.CommandText = "Select Id,UserName,userPassword from VoyageUser where UserName=@username and userPassword=@password"; cmd.Parameters.AddWithValue("@username", authUserData.VoyageUserName); cmd.Parameters.AddWithValue("@password", authUserData.VoyagePassword); try { con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { authenticateReply.VoyageisAuthenticated = true; authenticateReply.VoyageUserId = Int32.Parse(rdr["Id"].ToString()); rdr.Close(); } //rdr.Close(); } catch (Exception ex) { //creating the custom exception so that the client can be awared about the exception occured on the service side. Console.WriteLine("Error Message is:" + ex.Message); Custom_Exception exception = new Custom_Exception(); exception.Title = "Error Occured While authenticating the Voyage User"; exception.ExceptionMessage = ex.Message; throw new FaultException <Custom_Exception>(exception); } finally { //finally closing the Database Connection con.Close(); } return(authenticateReply); }