Create New User C#


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public UserModel CreateUser(UserModel param)
  2. {
  3. try
  4. {
  5. var result = new UserModel { Operation = false };
  6. if (param != null)
  7. {
  8. using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString))
  9. {
  10. connection.Open();
  11. var cmd = connection.CreateCommand();
  12. cmd.CommandType = CommandType.StoredProcedure;
  13. cmd.CommandText = "TestApp_UserCreate";
  14. cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = param.Name;
  15. cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = param.DateOfBirth.Date;
  16. if (param.IdentityCard != null)
  17. cmd.Parameters.Add("@IdentityCard", SqlDbType.NVarChar).Value = param.IdentityCard;
  18. if (param.Nickname != null)
  19. cmd.Parameters.Add("@Nickname", SqlDbType.NVarChar).Value = param.IdentityCard;
  20. cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = param.Email;
  21. cmd.Parameters.Add("@Gender", SqlDbType.Bit).Value = param.Gender == Sex.Female ? 1 : 0;
  22. if (param.Marital != MaritalStatus.NotDefined)
  23. cmd.Parameters.Add("@MaritalStatusID", SqlDbType.SmallInt).Value = (short)param.Marital;
  24. if (param.NumberOfChildren != null)
  25. cmd.Parameters.Add("@NumberOfChildren", SqlDbType.SmallInt).Value = param.NumberOfChildren;
  26. cmd.Parameters.Add("@UserID", SqlDbType.Int).Direction = ParameterDirection.Output;
  27. var res = cmd.ExecuteNonQuery();
  28. if (res > 0)
  29. {
  30. result = param;
  31. result.ObjectID = (int)cmd.Parameters["@UserID"].Value;
  32. }
  33.  
  34. }
  35. }
  36. return result;
  37. }
  38. catch(Exception ex)
  39. {
  40. return new UserModel { Operation = false, Message = ex.Message};
  41. }
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.