All pastes #368182 Raw Edit

Miscellany

public text v1 · immutable
#368182 ·published 2007-02-22 22:05 UTC
rendered paste body
            string queryString = "";
            int insertId = 0;

            queryString = "INSERT INTO Addresses (UserId, Name, Attn, Address1, Address2, City, StateId, Zip) Values (@UserId, @Name, @Attn, @Address1, @Address2, @City, @StateId, @Zip); SELECT SCOPE_IDENTITY()";


            using (SqlConnection connection = new SqlConnection(this.connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Connection.Open();
                command.Parameters.Add(new SqlParameter("@Name", this.name));
                command.Parameters.Add(new SqlParameter("@Attn", this.attn));
                command.Parameters.Add(new SqlParameter("@Address1", this.address1));
                command.Parameters.Add(new SqlParameter("@Address2", this.address2));
                command.Parameters.Add(new SqlParameter("@City", this.city));
                command.Parameters.Add(new SqlParameter("@StateId", this.stateId));
                command.Parameters.Add(new SqlParameter("@Zip   ", this.zip));
                command.Parameters.Add(new SqlParameter("@UserId", userId));

                object result = command.ExecuteScalar();

                if(result != null)
                    insertId = Int32.Parse(result.ToString());
                else
                    throw(new Exception("Unable to create new Profile"));

            }

            return insertId;