Return to Snippet

Revision: 48275
at June 28, 2011 02:43 by wedgardigitalwkscom


Initial Code
public UserModel CreateUser(UserModel param) 
        {
            try
            {
                var result = new UserModel { Operation = false };   
                if (param != null) 
                {
                    using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings[""].ConnectionString)) 
                    {
                        connection.Open();
                        var cmd = connection.CreateCommand();
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "TestApp_UserCreate";
                        cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = param.Name;
                        cmd.Parameters.Add("@DateOfBirth", SqlDbType.Date).Value = param.DateOfBirth.Date;
                        if (param.IdentityCard != null)
                            cmd.Parameters.Add("@IdentityCard", SqlDbType.NVarChar).Value = param.IdentityCard;
                        if (param.Nickname != null)
                            cmd.Parameters.Add("@Nickname", SqlDbType.NVarChar).Value = param.IdentityCard;
                        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = param.Email;
                        cmd.Parameters.Add("@Gender", SqlDbType.Bit).Value = param.Gender == Sex.Female ? 1 : 0;
                        if (param.Marital != MaritalStatus.NotDefined)
                            cmd.Parameters.Add("@MaritalStatusID", SqlDbType.SmallInt).Value = (short)param.Marital;
                        if (param.NumberOfChildren != null)
                            cmd.Parameters.Add("@NumberOfChildren", SqlDbType.SmallInt).Value = param.NumberOfChildren;
                        cmd.Parameters.Add("@UserID", SqlDbType.Int).Direction = ParameterDirection.Output;
                        var res = cmd.ExecuteNonQuery();
                        if (res > 0) 
                        {
                            result = param;
                            result.ObjectID = (int)cmd.Parameters["@UserID"].Value;
                        }

                    }
                }
                return result;
            }
            catch(Exception ex)
            {
                return new UserModel { Operation = false, Message = ex.Message};
            }
        }

Initial URL

                                

Initial Description

                                

Initial Title
Create New User C#

Initial Tags
c#

Initial Language
C#